Strava::parseAccessTokenResponse()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 33
c 1
b 0
f 0
rs 8.6666
cc 7
nc 7
nop 1
1
<?php
2
/**
3
 * Strava service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 *
8
 * @see    http://strava.github.io/
9
 * @see    http://strava.github.io/api/v3/oauth/
10
 */
11
12
namespace OAuth\OAuth2\Service;
13
14
use OAuth\Common\Consumer\CredentialsInterface;
15
use OAuth\Common\Http\Client\ClientInterface;
16
use OAuth\Common\Http\Exception\TokenResponseException;
17
use OAuth\Common\Http\Uri\Uri;
18
use OAuth\Common\Http\Uri\UriInterface;
19
use OAuth\Common\Storage\TokenStorageInterface;
20
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
21
use OAuth\OAuth2\Token\StdOAuth2Token;
22
23
/**
24
 * Strava service.
25
 *
26
 * @author  Pedro Amorim <[email protected]>
27
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
28
 *
29
 * @see    http://strava.github.io/
30
 * @see    http://strava.github.io/api/v3/oauth/
31
 */
32
class Strava extends AbstractService
33
{
34
    /**
35
     * Scopes.
36
     */
37
    // default
38
    const SCOPE_PUBLIC = 'public';
39
    // Modify activities, upload on the user’s behalf
40
    const SCOPE_WRITE = 'write';
41
    // View private activities and data within privacy zones
42
    const SCOPE_VIEW_PRIVATE = 'view_private';
43
44
    protected $approvalPrompt = 'auto';
45
46
    public function __construct(
47
        CredentialsInterface $credentials,
48
        ClientInterface $httpClient,
49
        TokenStorageInterface $storage,
50
        $scopes = [],
51
        ?UriInterface $baseApiUri = null
52
    ) {
53
        if (empty($scopes)) {
54
            $scopes = [self::SCOPE_PUBLIC];
55
        }
56
57
        parent::__construct(
58
            $credentials,
59
            $httpClient,
60
            $storage,
61
            $scopes,
62
            $baseApiUri,
63
            true
64
        );
65
66
        if (null === $baseApiUri) {
67
            $this->baseApiUri = new Uri('https://www.strava.com/api/v3/');
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAuthorizationEndpoint()
75
    {
76
        return new Uri('https://www.strava.com/oauth/authorize?approval_prompt=' . $this->approvalPrompt);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getAccessTokenEndpoint()
83
    {
84
        return new Uri('https://www.strava.com/oauth/token');
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function getAuthorizationMethod()
91
    {
92
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function parseAccessTokenResponse($responseBody)
99
    {
100
        $data = json_decode($responseBody, true);
101
102
        if (null === $data || !is_array($data)) {
103
            throw new TokenResponseException('Unable to parse response.');
104
        } elseif (isset($data['error_description'])) {
105
            throw new TokenResponseException(
106
                'Error in retrieving token: "' . $data['error_description'] . '"'
107
            );
108
        } elseif (isset($data['error'])) {
109
            throw new TokenResponseException(
110
                'Error in retrieving token: "' . $data['error'] . '"'
111
            );
112
        }
113
114
        $token = new StdOAuth2Token();
115
        $token->setAccessToken($data['access_token']);
116
117
        if (isset($data['expires_in'])) {
118
            $token->setLifeTime($data['expires_in']);
119
            unset($data['expires_in']);
120
        }
121
        if (isset($data['refresh_token'])) {
122
            $token->setRefreshToken($data['refresh_token']);
123
            unset($data['refresh_token']);
124
        }
125
126
        unset($data['access_token']);
127
128
        $token->setExtraParams($data);
129
130
        return $token;
131
    }
132
133
    public function setApprouvalPrompt($prompt): void
134
    {
135
        if (!in_array($prompt, ['auto', 'force'], true)) {
136
            // @todo Maybe could we rename this exception
137
            throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
138
        }
139
        $this->approvalPrompt = $prompt;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    protected function getScopesDelimiter()
146
    {
147
        return ',';
148
    }
149
}
150