Completed
Push — master ( 9d71dc...200e3f )
by Дмитрий
03:55
created

Google   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 78
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getOpenIdUrl() 0 4 1
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
B getIdentity() 0 32 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author Alexander Fedyashov <[email protected]>
6
 */
7
8
namespace SocialConnect\OpenIDConnect\Provider;
9
10
use SocialConnect\Provider\AccessTokenInterface;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OpenIDConnect\AbstractProvider;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
/**
17
 * Class Provider
18
 * @package SocialConnect\Google
19
 */
20
class Google extends AbstractProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getOpenIdUrl()
26
    {
27
        return 'https://accounts.google.com/.well-known/openid-configuration';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getBaseUri()
34
    {
35
        return 'https://www.googleapis.com/';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAuthorizeUri()
42
    {
43
        return 'https://accounts.google.com/o/oauth2/auth';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getRequestTokenUri()
50
    {
51
        return 'https://accounts.google.com/o/oauth2/token';
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getName()
58
    {
59
        return 'google';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getIdentity(AccessTokenInterface $accessToken)
66
    {
67
        $response = $this->httpClient->request(
68
            $this->getBaseUri() . 'oauth2/v1/userinfo',
69
            [
70
                'access_token' => $accessToken->getToken()
71
            ]
72
        );
73
74
        if (!$response->isSuccess()) {
75
            throw new InvalidResponse(
76
                'API response with error code',
77
                $response
78
            );
79
        }
80
81
        $body = $response->getBody();
82
        $result = json_decode($body);
83
84
        $hydrator = new ObjectMap(
85
            [
86
                'id' => 'id',
87
                'given_name' => 'firstname',
88
                'family_name' => 'lastname',
89
                'email' => 'email',
90
                'name' => 'fullname',
91
                'gender' => 'sex',
92
            ]
93
        );
94
95
        return $hydrator->hydrate(new User(), $result);
96
    }
97
}
98