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

Spotify::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class Spotify extends AbstractService
10
{
11
    /**
12
     * Scopes
13
     *
14
     * @var string
15
     */
16
    const SCOPE_PLAYLIST_MODIFY_PUBLIC = 'playlist-modify-public';
17
    const SCOPE_PLAYLIST_MODIFY_PRIVATE = 'playlist-modify-private';
18
    const SCOPE_PLAYLIST_READ_PRIVATE = 'playlist-read-private';
19
    const SCOPE_PLAYLIST_READ_COLABORATIVE = 'playlist-read-collaborative';
20
    const SCOPE_STREAMING = 'streaming';
21
    const SCOPE_USER_LIBRARY_MODIFY = 'user-library-modify';
22
    const SCOPE_USER_LIBRARY_READ = 'user-library-read';
23
    const SCOPE_USER_READ_PRIVATE = 'user-read-private';
24
    const SCOPE_USER_READ_EMAIL = 'user-read-email';
25
    const SCOPE_USER_READ_BIRTHDAY = 'user-read-birthdate';
26
    const SCOPE_USER_READ_FOLLOW = 'user-follow-read';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function init()
32
    {
33
        $this->stateParameterInAuthUrl = true;
34
35
        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...
36
            $this->baseApiUri = new Uri('https://api.spotify.com/v1/');
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getAuthorizationEndpoint()
44
    {
45
        return new Uri('https://accounts.spotify.com/authorize');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getAccessTokenEndpoint()
52
    {
53
        return new Uri('https://accounts.spotify.com/api/token');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function getAuthorizationMethod()
60
    {
61
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function parseAccessTokenResponse($responseBody)
68
    {
69
        $data = json_decode($responseBody, true);
70
71
        if (null === $data || !is_array($data)) {
72
            throw new TokenResponseException('Unable to parse response.');
73
        } elseif (isset($data['error'])) {
74
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
75
        }
76
77
78
        $token = new StdOAuth2Token();
79
        $token->setAccessToken($data['access_token']);
80
81
        if (isset($data['expires_in'])) {
82
            $token->setLifetime($data['expires_in']);
83
            unset($data['expires_in']);
84
        }
85
86
        if (isset($data['refresh_token'])) {
87
            $token->setRefreshToken($data['refresh_token']);
88
            unset($data['refresh_token']);
89
        }
90
91
        unset($data['access_token']);
92
93
        $token->setExtraParams($data);
94
95
        return $token;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function getExtraOAuthHeaders()
102
    {
103
        return array('Authorization' => 'Basic ' .
104
            base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
105
    }
106
}
107