Completed
Push — master ( e1e96b...bf666a )
by Дмитрий
03:27 queued 02:16
created

Google::getScopeInline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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