Completed
Push — master ( 7b2d5d...97c76c )
by Дмитрий
03:38 queued 02:02
created

Google::parseToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
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
    /**
26
     * {@inheritdoc}
27
     */
28 3
    public function getBaseUri()
29
    {
30 3
        return 'https://www.googleapis.com/';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function getAuthorizeUri()
37
    {
38 2
        return 'https://accounts.google.com/o/oauth2/auth';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function getRequestTokenUri()
45
    {
46 2
        return 'https://accounts.google.com/o/oauth2/token';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function getName()
53
    {
54 3
        return 'google';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function parseToken($body)
61
    {
62 3
        $result = json_decode($body, true);
63 3
        if ($result) {
64 1
            return new AccessToken($result);
65
        }
66
67 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function getIdentity(AccessTokenInterface $accessToken)
74
    {
75 2
        $response = $this->httpClient->request(
76 2
            $this->getBaseUri() . 'oauth2/v1/userinfo',
77
            [
78 2
                'access_token' => $accessToken->getToken()
79 2
            ]
80 2
        );
81
82 2
        if (!$response->isSuccess()) {
83 1
            throw new InvalidResponse(
84 1
                'API response with error code',
85
                $response
86 1
            );
87
        }
88
89 1
        $result = $response->json();
90 1
        if (!$result) {
91 1
            throw new InvalidResponse(
92 1
                'API response is not a valid JSON object',
93
                $response
94 1
            );
95
        }
96
97
        $hydrator = new ObjectMap(
98
            [
99
                'id' => 'id',
100
                'given_name' => 'firstname',
101
                'family_name' => 'lastname',
102
                'email' => 'email',
103
                'verified_email' => 'emailVerified',
104
                'name' => 'fullname',
105
                'gender' => 'sex',
106
                'picture' => 'pictureURL'
107
            ]
108
        );
109
110
        return $hydrator->hydrate(new User(), $result);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getScopeInline()
117
    {
118
        return implode(' ', $this->scope);
119
    }
120
}
121