Completed
Push — 3.x ( 7d1111...e04e11 )
by Дмитрий
03:24
created

Google   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 33
c 6
b 1
f 0
dl 0
loc 101
ccs 21
cts 33
cp 0.6364
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getScopeInline() 0 3 1
A getBaseUri() 0 3 1
A getAuthorizeUri() 0 3 1
A getOpenIdUrl() 0 3 1
A extractIdentity() 0 24 2
A getName() 0 3 1
A getIdentity() 0 17 1
A getRequestTokenUri() 0 3 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author Alexander Fedyashov <[email protected]>
6
 */
7
declare(strict_types=1);
8
9
namespace SocialConnect\OpenIDConnect\Provider;
10
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\Common\Exception\InvalidArgumentException;
13
use SocialConnect\OpenIDConnect\AccessToken;
14
use SocialConnect\Provider\AccessTokenInterface;
15
use SocialConnect\OpenIDConnect\AbstractProvider;
16
use SocialConnect\Common\Entity\User;
17
18
class Google extends AbstractProvider
19
{
20
    const NAME = 'google';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 1
    public function getOpenIdUrl()
26
    {
27 1
        return 'https://accounts.google.com/.well-known/openid-configuration';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function getBaseUri()
34
    {
35 2
        return 'https://www.googleapis.com/';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function getAuthorizeUri()
42
    {
43 1
        return 'https://accounts.google.com/o/oauth2/auth';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function getRequestTokenUri()
50
    {
51 1
        return 'https://accounts.google.com/o/oauth2/token';
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function getName()
58
    {
59 1
        return self::NAME;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function extractIdentity(AccessTokenInterface $accessToken)
66
    {
67
        if (!$accessToken instanceof AccessToken) {
68
            throw new InvalidArgumentException(
69
                '$accessToken must be instance AccessToken'
70
            );
71
        }
72
73
        $jwt = $accessToken->getJwt();
74
75
        $hydrator = new ArrayHydrator([
76
            'sub' => 'id',
77
            'email' => 'email',
78
            'email_verified' => 'emailVerified',
79
            'name' => 'fullname',
80
            'picture' => 'pictureURL',
81
            'given_name' => 'firstname',
82
            'family_name' => 'lastname',
83
        ]);
84
85
        /** @var User $user */
86
        $user = $hydrator->hydrate(new User(), $jwt->getPayload());
87
88
        return $user;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function getIdentity(AccessTokenInterface $accessToken)
95
    {
96 1
        $response = $this->request('GET', 'oauth2/v1/userinfo', [], $accessToken);
97
98 1
        $hydrator = new ArrayHydrator([
99 1
            'id' => 'id',
100 1
            'given_name' => 'firstname',
101 1
            'family_name' => 'lastname',
102 1
            'email' => 'email',
103 1
            'verified_email' => 'emailVerified',
104 1
            'name' => 'fullname',
105
            'gender' => static function ($value, User $user) {
106
                $user->setSex($value);
107 1
            },
108
        ]);
109
110 1
        return $hydrator->hydrate(new User(), $response);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getScopeInline()
117
    {
118
        return implode(' ', $this->scope);
119
    }
120
}
121