Completed
Push — master ( ad69a5...d98011 )
by Дмитрий
03:25
created

Provider::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Google;
10
11
use SocialConnect\Auth\Provider\OAuth2\AbstractProvider;
12
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Http\Client\Client;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
use SocialConnect\Auth\Exception\InvalidAccessToken;
17
18
/**
19
 * Class Provider
20
 * @package SocialConnect\Google
21
 */
22
class Provider extends AbstractProvider
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $requestHttpMethod = Client::GET;
28
29
    /**
30
     * @return string
31
     */
32
    public function getAuthorizeUri()
33
    {
34
        return 'https://accounts.google.com/o/oauth2/auth';
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getName()
41
    {
42
        return 'google';
43
    }
44
45
    /**
46
     * @param string $code
47
     * @return AccessToken
48
     * @throws \InvalidArgumentException
49
     */
50
    public function getAccessToken($code)
51
    {
52
        if (!is_string($code)) {
53
            throw new \InvalidArgumentException('Parameter $code must be a string');
54
        }
55
56
        $parameters = array(
57
            'client_id' => $this->consumer->getKey(),
58
            'client_secret' => $this->consumer->getSecret(),
59
            'grant_type' => 'authorization_code',
60
            'code' => $code,
61
            'redirect_uri' => $this->getRedirectUrl()
62
        );
63
64
        $response = $this->service->getHttpClient()->request($this->getRequestTokenUri(), $parameters, Client::POST);
65
        $body = $response->getBody();
66
67
        return $this->parseToken($body);
0 ignored issues
show
Bug introduced by
It seems like $body defined by $response->getBody() on line 65 can also be of type boolean; however, SocialConnect\Google\Provider::parseToken() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getRequestTokenUri()
74
    {
75
        return 'https://accounts.google.com/o/oauth2/token';
76
    }
77
78
    /**
79
     * @param string $body
80
     * @return AccessToken
81
     * @throws InvalidAccessToken
82
     */
83
    public function parseToken($body)
84
    {
85
        $result = json_decode($body);
86
87
        if (!isset($result->access_token) || empty($result->access_token)) {
88
            throw new InvalidAccessToken;
89
        }
90
91
        return new AccessToken($result->access_token);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getIdentity(AccessToken $accessToken)
98
    {
99
        $response = $this->service->getHttpClient()->request(
100
            $this->getBaseUri() . 'oauth2/v1/userinfo',
101
            ['access_token' => $accessToken->getToken()]
102
        );
103
104
        $body = $response->getBody();
105
        $result = json_decode($body);
106
107
        $hydrator = new ObjectMap(array(
108
            'id' => 'id',
109
            'given_name' => 'firstname',
110
            'family_name' => 'lastname',
111
            'email' => 'email',
112
            'name' => 'fullname',
113
            'gender' => 'sex',
114
        ));
115
116
        return $hydrator->hydrate(new User(), $result);
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getBaseUri()
123
    {
124
        return 'https://www.googleapis.com/';
125
    }
126
}
127