Completed
Pull Request — master (#483)
by
unknown
03:10
created

Spotify   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 100
rs 10
c 3
b 1
f 0

6 Methods

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