Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Google
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 getAuthorizeUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAccessTokenUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScope
n/a
0 / 0
n/a
0 / 0
1
 getUserProfile
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 getLogoSource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getThemeColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Miniframe\SocialLogin\Provider;
4
5use Miniframe\SocialLogin\Model\User;
6
7class Google extends AbstractOAuth2Provider
8{
9    /**
10     * Returns the Authorize URL
11     *
12     * @return string
13     */
14    protected function getAuthorizeUrl(): string
15    {
16        return 'https://accounts.google.com/o/oauth2/v2/auth';
17    }
18
19    /**
20     * Returns the Access Token URL
21     *
22     * @return string
23     */
24    protected function getAccessTokenUrl(): string
25    {
26        return 'https://oauth2.googleapis.com/token';
27    }
28
29    /**
30     * Returns the requested scope
31     *
32     * @return string|null
33     */
34    protected function getScope(): ?string
35    {
36        return
37            'https://www.googleapis.com/auth/userinfo.email'
38            . ' https://www.googleapis.com/auth/userinfo.profile'
39            . ' openid'
40        ;
41    }
42
43    /**
44     * Returns the user profile
45     *
46     * @param array $accessToken The access token.
47     *
48     * @return User
49     */
50    protected function getUserProfile(array $accessToken): User
51    {
52        $data = $this->curlRequest('https://www.googleapis.com/oauth2/v1/userinfo?alt=json', 'GET', null, [
53            'Authorization: Bearer ' . $accessToken['access_token'],
54        ]);
55
56        if (isset($data['verified_email']) && $data['verified_email'] === false) {
57            throw new \RuntimeException('Email address not verified');
58        }
59
60        return new User(
61            $data['id'],
62            $data['email'],
63            $data['name'],
64            $data['picture'],
65            static::class,
66            ['accessToken' => $accessToken, 'userData' => $data]
67        );
68    }
69
70    /**
71     * Returns the image source for the logo of this provider.
72     *
73     * @return string
74     */
75    public static function getLogoSource(): string
76    {
77        return 'data:image/svg+xml;base64,'
78            . base64_encode(file_get_contents(__DIR__ . '/../../templates/logos/Google.svg'));
79    }
80
81    /**
82     * Returns the theme color for this provider.
83     *
84     * @return string
85     */
86    public static function getThemeColor(): string
87    {
88        return '#d3a300';
89    }
90}