Completed
Push — master ( 4c303e...0095fb )
by Дмитрий
03:38
created

Google::parseToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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 1
    public function getBaseUri()
29
    {
30 1
        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 1
    public function getRequestTokenUri()
45
    {
46 1
        return 'https://accounts.google.com/o/oauth2/token';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function getName()
53
    {
54 2
        return 'google';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function parseToken($body)
61
    {
62
        $result = json_decode($body, true);
63
        if ($result) {
64
            return new AccessToken($result);
65
        }
66
67
        throw new InvalidAccessToken('Provider response with not valid JSON');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getIdentity(AccessTokenInterface $accessToken)
74
    {
75
        $response = $this->httpClient->request(
76
            $this->getBaseUri() . 'oauth2/v1/userinfo',
77
            [
78
                'access_token' => $accessToken->getToken()
79
            ]
80
        );
81
82
        if (!$response->isSuccess()) {
83
            throw new InvalidResponse(
84
                'API response with error code',
85
                $response
86
            );
87
        }
88
89
        $body = $response->getBody();
90
        $result = json_decode($body);
91
92
        $hydrator = new ObjectMap(
93
            [
94
                'id' => 'id',
95
                'given_name' => 'firstname',
96
                'family_name' => 'lastname',
97
                'email' => 'email',
98
                'name' => 'fullname',
99
                'gender' => 'sex',
100
            ]
101
        );
102
103
        return $hydrator->hydrate(new User(), $result);
104
    }
105
}
106