Completed
Push — master ( ef2ae5...d18bda )
by Michael
02:29
created

TomTomMySports::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
crap 1
1
<?php
2
namespace League\OAuth2\Client\Provider;
3
4
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
5
use League\OAuth2\Client\Token\AccessToken;
6
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
7
use Psr\Http\Message\ResponseInterface;
8
9
class TomTomMySports extends AbstractProvider
10
{
11
    use BearerAuthorizationTrait;
12
13
    /**
14
     * @var string
15
     */
16
    const BASE_MYSPORTS_URL = 'https://api.tomtom.com/mysports';
17
18
    /**
19
     * @var string
20
     */
21
    protected $apiVersion = '1';
22
23
    /**
24
     * @var string
25
     */
26
    protected $apikey;
27
28
29
    /**
30
     * @inheritDoc
31 9
     */
32
    protected function getAuthorizationParameters(array $options)
33 9
    {
34 9
        $options = parent::getAuthorizationParameters($options);
35 3
        if (empty($options['api_key'])) {
36
            $options['Api-Key'] = $this->apikey;
37 9
        }
38 6
39 2
        return $options;
40
    }
41
42 9
    /**
43
     * @inheritDoc
44 3
     */
45
    public function getAccessToken($grant, array $options = [])
46 9
    {
47 9
        $grant = $this->verifyGrant($grant);
48 9
49 3
        $params = [
50
            'client_id'     => $this->clientId,
51
            'client_secret' => $this->clientSecret,
52 9
            'redirect_uri'  => $this->redirectUri,
53
            'Api-Key'       => $this->apikey
54
        ];
55
56 9
        $params   = $grant->prepareRequestParameters($params, $options);
57 9
        $request  = $this->getAccessTokenRequest($params);
58 3
        $response = $this->getParsedResponse($request);
59
        $prepared = $this->prepareAccessTokenResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getParsedResponse($request) on line 58 can also be of type null or string; however, League\OAuth2\Client\Pro...reAccessTokenResponse() does only seem to accept array, 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...
60 9
        $token    = $this->createAccessToken($prepared, $grant);
61
62 9
        return $token;
63 9
    }
64 3
65
    /**
66 9
     * Builds request options used for requesting an access token.
67
     *
68
     * @param  array $params
69
     * @return array
70
     */
71
    protected function getAccessTokenOptions(array $params)
72
    {
73
        $options = parent::getAccessTokenOptions($params);
74
75 12
        $options['headers']['Api-Key'] = $this->apikey;
76
        $options['headers']['Authorization'] = 'Basic ' . base64_encode(implode(':', [
77 12
                $this->clientId,
78
                $this->clientSecret,
79
            ]));
80
        return $options;
81
    }
82
83
84
    /**
85
     * Get authorization url to begin OAuth flow
86
     *
87 12
     * @return string
88
     */
89 12
    public function getBaseAuthorizationUrl()
90
    {
91
        return self::BASE_MYSPORTS_URL . '/oauth2/authorize';
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function getBaseAccessTokenUrl(array $params)
98
    {
99
        return self::BASE_MYSPORTS_URL . '/oauth2/token';
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function getResourceOwnerDetailsUrl(AccessToken $token)
106
    {
107
        return self::BASE_MYSPORTS_URL . '/' . $this->apiVersion . '/athlete';
108
    }
109
110
    /**
111
     * @link http://developer.tomtom.com/products/sports/mysportscloud/authorization/
112
     *
113
     * Get the default scopes used by this provider.
114 6
     *
115
     * This should not be a complete list of all scopes, but the minimum
116 6
     * required for the provider user interface!
117
     *
118
     * @return array
119
     */
120
    protected function getDefaultScopes()
121
    {
122
        return ['activities'];
123
    }
124
125
    /**
126
     * @inheritDoc
127 9
     */
128
    protected function checkResponse(ResponseInterface $response, $data)
129 9
    {
130 3
        if ($response->getStatusCode() >= 400) {
131 3
            throw new IdentityProviderException(
132 3
                'Forbidden',
133 1
                $response->getStatusCode(),
134 1
                $response
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a array|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
            );
136 6
        }
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    protected function createResourceOwner(array $response, AccessToken $token)
143
    {
144
        return null;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getBaseMySportsUrl()
151
    {
152
        return self::BASE_MYSPORTS_URL;
153 3
    }
154
155 3
    /**
156
     * @return string
157
     */
158
    public function getApiVersion()
159
    {
160
        return $this->apiVersion;
161 3
    }
162
163 3
    /**
164
     * @inheritDoc
165
     */
166
    protected function getDefaultHeaders()
167
    {
168
        return [
169
            'Accept'          => 'application/json',
170
            'Accept-Encoding' => 'gzip',
171
        ];
172
    }
173
}
174