Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Strava::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 24
rs 8.9713
c 1
b 0
f 1
cc 3
eloc 17
nc 4
nop 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Strava::getAuthorizationEndpoint() 0 4 1
A Strava::getAccessTokenEndpoint() 0 4 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
 * @link    http://strava.github.io/
8
 * @link    http://strava.github.io/api/v3/oauth/
9
 */
10
11
namespace OAuth\OAuth2\Service;
12
13
use OAuth\OAuth2\Token\StdOAuth2Token;
14
use OAuth\Common\Http\Exception\TokenResponseException;
15
use OAuth\Common\Http\Uri\Uri;
16
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
17
18
/**
19
 * Strava service.
20
 *
21
 * @author  Pedro Amorim <[email protected]>
22
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
23
 * @link    http://strava.github.io/
24
 * @link    http://strava.github.io/api/v3/oauth/
25
 */
26
class Strava extends AbstractService
27
{
28
    /**
29
     * Scopes
30
     */
31
    // default
32
    const SCOPE_PUBLIC       = 'public';
33
    // Modify activities, upload on the user’s behalf
34
    const SCOPE_WRITE        = 'write';
35
    // View private activities and data within privacy zones
36
    const SCOPE_VIEW_PRIVATE = 'view_private';
37
38
    protected $approvalPrompt = 'auto';
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function init()
44
    {
45
        $this->stateParameterInAuthUrl = true;
46
47
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
48
            $this->baseApiUri = new Uri('https://www.strava.com/api/v3/');
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAuthorizationEndpoint()
56
    {
57
        return new Uri('https://www.strava.com/oauth/authorize?approval_prompt=' . $this->approvalPrompt);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAccessTokenEndpoint()
64
    {
65
        return new Uri('https://www.strava.com/oauth/token');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getAuthorizationMethod()
72
    {
73
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function parseAccessTokenResponse($responseBody)
80
    {
81
        $data = json_decode($responseBody, true);
82
83
        if (null === $data || !is_array($data)) {
84
            throw new TokenResponseException('Unable to parse response.');
85
        } elseif (isset($data['error_description'])) {
86
            throw new TokenResponseException(
87
                'Error in retrieving token: "' . $data['error_description'] . '"'
88
            );
89
        } elseif (isset($data['error'])) {
90
            throw new TokenResponseException(
91
                'Error in retrieving token: "' . $data['error'] . '"'
92
            );
93
        }
94
95
        $token = new StdOAuth2Token();
96
        $token->setAccessToken($data['access_token']);
97
98
        if (isset($data['expires_in'])) {
99
            $token->setLifeTime($data['expires_in']);
100
            unset($data['expires_in']);
101
        }
102
        if (isset($data['refresh_token'])) {
103
            $token->setRefreshToken($data['refresh_token']);
104
            unset($data['refresh_token']);
105
        }
106
107
        unset($data['access_token']);
108
109
        $token->setExtraParams($data);
110
111
        return $token;
112
    }
113
114
    public function setApprouvalPrompt($prompt)
115
    {
116
        if (!in_array($prompt, array('auto', 'force'), true)) {
117
            // @todo Maybe could we rename this exception
118
            throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
119
        }
120
        $this->approvalPrompt = $prompt;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function getScopesDelimiter()
127
    {
128
        return ',';
129
    }
130
}
131