Completed
Push — master ( 0095fb...15ea44 )
by Дмитрий
04:21
created

Google::getIdentity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.5748

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 10
cts 21
cp 0.4762
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 1
crap 2.5748
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 2
    public function getBaseUri()
29
    {
30 2
        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
    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 1
    public function getIdentity(AccessTokenInterface $accessToken)
74
    {
75 1
        $response = $this->httpClient->request(
76 1
            $this->getBaseUri() . 'oauth2/v1/userinfo',
77
            [
78 1
                'access_token' => $accessToken->getToken()
79 1
            ]
80 1
        );
81
82 1
        if (!$response->isSuccess()) {
83 1
            throw new InvalidResponse(
84 1
                'API response with error code',
85
                $response
86 1
            );
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