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

Google   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 23.53%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 83
ccs 8
cts 34
cp 0.2353
rs 10
c 0
b 0
f 0

6 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
B getIdentity() 0 32 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 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