Completed
Push — master ( 1dea25...e4805f )
by Michael
02:38
created

TomTomMySports::getAuthorizationParameters()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 22
cts 22
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 32
nop 1
crap 6
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
     * @inheritDoc
30
     */
31 9
    protected function getAuthorizationParameters(array $options)
32
    {
33 9
        if (empty($options['state'])) {
34 9
            $options['state'] = $this->getRandomState();
35 3
        }
36
37 9
        if (empty($options['scope'])) {
38 6
            $options['scope'] = $this->getDefaultScopes();
39 2
        }
40
41
        $options += [
42 9
            'response_type'   => 'code',
43
            'approval_prompt' => 'auto'
44 3
        ];
45
46 9
        if (is_array($options['scope'])) {
47 9
            $separator = $this->getScopeSeparator();
48 9
            $options['scope'] = implode($separator, $options['scope']);
49 3
        }
50
51
        // Store the state as it may need to be accessed later on.
52 9
        $this->state = $options['state'];
53
54
        // Business code layer might set a different redirect_uri parameter
55
        // depending on the context, leave it as-is
56 9
        if (!isset($options['redirect_uri'])) {
57 9
            $options['redirect_uri'] = $this->redirectUri;
58 3
        }
59
60 9
        $options['client_id'] = $this->clientId;
61
62 9
        if (empty($options['api_key'])) {
63 9
            $options['api-key'] = $this->apikey;
64 3
        }
65
66 9
        return $options;
67
    }
68
69
70
    /**
71
     * Get authorization url to begin OAuth flow
72
     *
73
     * @return string
74
     */
75 12
    public function getBaseAuthorizationUrl()
76
    {
77 12
        return self::BASE_MYSPORTS_URL . '/oauth2/authorize';
78
    }
79
80
    /**
81
     * Get access token url to retrieve token
82
     *
83
     * @param  array $params
84
     *
85
     * @return string
86
     */
87 12
    public function getBaseAccessTokenUrl(array $params)
88
    {
89 12
        return self::BASE_MYSPORTS_URL . '/oauth2/token';
90
    }
91
92
    /**
93
     * Get provider url to fetch user details
94
     *
95
     * @param  AccessToken $token
96
     *
97
     * @return string
98
     */
99
    public function getResourceOwnerDetailsUrl(AccessToken $token)
100
    {
101
        return self::BASE_MYSPORTS_URL . '/' . $this->apiVersion . '/athlete';
102
    }
103
104
    /**
105
     * @link http://developer.tomtom.com/products/sports/mysportscloud/authorization/
106
     *
107
     * Get the default scopes used by this provider.
108
     *
109
     * This should not be a complete list of all scopes, but the minimum
110
     * required for the provider user interface!
111
     *
112
     * @return array
113
     */
114 6
    protected function getDefaultScopes()
115
    {
116 6
        return ['activities'];
117
    }
118
119
    /**
120
     * Check a provider response for errors.
121
     *
122
     * @throws IdentityProviderException
123
     * @param  ResponseInterface $response
124
     * @param  string $data Parsed response data
125
     * @return void
126
     */
127 9
    protected function checkResponse(ResponseInterface $response, $data)
128
    {
129 9
        if ($response->getStatusCode() >= 400) {
130 3
            throw new IdentityProviderException(
131 3
                $data['message'] ?: $response->getReasonPhrase(),
132 3
                $response->getStatusCode(),
133 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...
134 1
            );
135
        }
136 6
    }
137
138
    /**
139
     * Generate a user object from a successful user details request.
140
     *
141
     * @param array $response
142
     * @param AccessToken $token
143
     * @return null
144
     */
145
    protected function createResourceOwner(array $response, AccessToken $token)
146
    {
147
        return null;
148
    }
149
150
    /**
151
     * @return string
152
     */
153 3
    public function getBaseMySportsUrl()
154
    {
155 3
        return self::BASE_MYSPORTS_URL;
156
    }
157
158
    /**
159
     * @return string
160
     */
161 3
    public function getApiVersion()
162
    {
163 3
        return $this->apiVersion;
164
    }
165
166
    /**
167
     * Returns the default headers used by this provider.
168
     *
169
     * Typically this is used to set 'Accept' or 'Content-Type' headers.
170
     *
171
     * @return array
172
     */
173 9
    protected function getDefaultHeaders()
174
    {
175
        return [
176 9
            'Accept'          => 'application/json',
177 3
            'Accept-Encoding' => 'gzip',
178 3
        ];
179
    }
180
}
181