Completed
Push — master ( d3f389...b7db91 )
by Дмитрий
03:39
created

Provider::getRequestTokenUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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\Google;
10
11
use SocialConnect\Auth\Exception\InvalidResponse;
12
use SocialConnect\Auth\Provider\OAuth2\AbstractProvider;
13
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Http\Client\Client;
16
use SocialConnect\Common\Hydrator\ObjectMap;
17
use SocialConnect\Auth\Exception\InvalidAccessToken;
18
19
/**
20
 * Class Provider
21
 * @package SocialConnect\Google
22
 */
23
class Provider extends AbstractProvider
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $requestHttpMethod = Client::GET;
29
30
    /**
31
     * @return string
32
     */
33
    public function getAuthorizeUri()
34
    {
35
        return 'https://accounts.google.com/o/oauth2/auth';
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return 'google';
44
    }
45
46
    /**
47
     * @param string $code
48
     * @return AccessToken
49
     * @throws \InvalidArgumentException
50
     */
51
    public function getAccessToken($code)
52
    {
53
        if (!is_string($code)) {
54
            throw new \InvalidArgumentException('Parameter $code must be a string');
55
        }
56
57
        $parameters = array(
58
            'client_id' => $this->consumer->getKey(),
59
            'client_secret' => $this->consumer->getSecret(),
60
            'grant_type' => 'authorization_code',
61
            'code' => $code,
62
            'redirect_uri' => $this->getRedirectUrl()
63
        );
64
65
        $response = $this->service->getHttpClient()->request($this->getRequestTokenUri(), $parameters, Client::POST);
66
        $body = $response->getBody();
67
68
        return $this->parseToken($body);
0 ignored issues
show
Bug introduced by
It seems like $body defined by $response->getBody() on line 66 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...
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getRequestTokenUri()
75
    {
76
        return 'https://accounts.google.com/o/oauth2/token';
77
    }
78
79
    /**
80
     * @param string $body
81
     * @return AccessToken
82
     * @throws InvalidAccessToken
83
     */
84
    public function parseToken($body)
85
    {
86
        $result = json_decode($body);
87
88
        if (!isset($result->access_token) || empty($result->access_token)) {
89
            throw new InvalidAccessToken;
90
        }
91
92
        return new AccessToken($result->access_token);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getIdentity(AccessToken $accessToken)
99
    {
100
        $response = $this->service->getHttpClient()->request(
101
            $this->getBaseUri() . 'oauth2/v1/userinfo',
102
            ['access_token' => $accessToken->getToken()]
103
        );
104
105
        if (!$response->isSuccess()) {
106
            throw new InvalidResponse(
107
                'API response with error code',
108
                $response
109
            );
110
        }
111
112
        $body = $response->getBody();
113
        $result = json_decode($body);
114
115
        $hydrator = new ObjectMap(array(
116
            'id' => 'id',
117
            'given_name' => 'firstname',
118
            'family_name' => 'lastname',
119
            'email' => 'email',
120
            'name' => 'fullname',
121
            'gender' => 'sex',
122
        ));
123
124
        return $hydrator->hydrate(new User(), $result);
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getBaseUri()
131
    {
132
        return 'https://www.googleapis.com/';
133
    }
134
}
135