Google   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 9
dl 0
loc 100
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
A parseToken() 0 9 2
A getIdentity() 0 39 3
A getScopeInline() 0 4 1
1
<?php
2
3
/**
4
 * SocialConnect project
5
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
6
 * @author Alexander Fedyashov <[email protected]>
7
 */
8
9
namespace SocialConnect\OAuth2\Provider;
10
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Provider\Exception\InvalidResponse;
14
use SocialConnect\OAuth2\AbstractProvider;
15
use SocialConnect\OAuth2\AccessToken;
16
use SocialConnect\Common\Entity\User;
17
use SocialConnect\Common\Hydrator\ObjectMap;
18
19
/**
20
 * Class Provider
21
 * @package SocialConnect\Google
22
 */
23
class Google extends AbstractProvider
24
{
25
    const NAME = 'google';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public function getBaseUri()
31
    {
32 3
        return 'https://www.googleapis.com/';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function getAuthorizeUri()
39
    {
40 2
        return 'https://accounts.google.com/o/oauth2/auth';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function getRequestTokenUri()
47
    {
48 2
        return 'https://accounts.google.com/o/oauth2/token';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getName()
55
    {
56 3
        return self::NAME;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function parseToken($body)
63
    {
64 3
        $result = json_decode($body, true);
65 3
        if ($result) {
66 1
            return new AccessToken($result);
67
        }
68
69 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function getIdentity(AccessTokenInterface $accessToken)
76
    {
77 2
        $response = $this->httpClient->request(
78 2
            $this->getBaseUri() . 'oauth2/v1/userinfo',
79
            [
80 2
                'access_token' => $accessToken->getToken()
81
            ]
82
        );
83
84 2
        if (!$response->isSuccess()) {
85 1
            throw new InvalidResponse(
86 1
                'API response with error code',
87 1
                $response
88
            );
89
        }
90
91 1
        $result = $response->json();
92 1
        if (!$result) {
93 1
            throw new InvalidResponse(
94 1
                'API response is not a valid JSON object',
95 1
                $response
96
            );
97
        }
98
99
        $hydrator = new ObjectMap(
100
            [
101
                'id' => 'id',
102
                'given_name' => 'firstname',
103
                'family_name' => 'lastname',
104
                'email' => 'email',
105
                'verified_email' => 'emailVerified',
106
                'name' => 'fullname',
107
                'gender' => 'sex',
108
                'picture' => 'pictureURL'
109
            ]
110
        );
111
112
        return $hydrator->hydrate(new User(), $result);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getScopeInline()
119
    {
120
        return implode(' ', $this->scope);
121
    }
122
}
123