Deezer::parseAccessTokenResponse()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 30
c 1
b 0
f 0
rs 8.8333
cc 7
nc 5
nop 1
1
<?php
2
/**
3
 * Deezer service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 *
8
 * @see    http://developers.deezer.com/api/
9
 */
10
11
namespace OAuth\OAuth2\Service;
12
13
use OAuth\Common\Consumer\CredentialsInterface;
14
use OAuth\Common\Http\Client\ClientInterface;
15
use OAuth\Common\Http\Exception\TokenResponseException;
16
use OAuth\Common\Http\Uri\Uri;
17
use OAuth\Common\Http\Uri\UriInterface;
18
use OAuth\Common\Storage\TokenStorageInterface;
19
use OAuth\OAuth2\Token\StdOAuth2Token;
20
21
/**
22
 * Deezer service.
23
 *
24
 * @author  Pedro Amorim <[email protected]>
25
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
26
 *
27
 * @see    http://developers.deezer.com/api/
28
 */
29
class Deezer extends AbstractService
30
{
31
    /**
32
     * Defined scopes
33
     * http://developers.deezer.com/api/permissions.
34
     */
35
    const SCOPE_BASIC_ACCESS = 'basic_access';       // Access users basic information
36
    const SCOPE_EMAIL = 'email';              // Get the user's email
37
    const SCOPE_OFFLINE_ACCESS = 'offline_access';     // Access user data any time
38
    const SCOPE_MANAGE_LIBRARY = 'manage_library';     // Manage users' library
39
    const SCOPE_MANAGE_COMMUNITY = 'manage_community';   // Manage users' friends
40
    const SCOPE_DELETE_LIBRARY = 'delete_library';     // Delete library items
41
    const SCOPE_LISTENING_HISTORY = 'listening_history';  // Access the user's listening history
42
43
    public function __construct(
44
        CredentialsInterface $credentials,
45
        ClientInterface $httpClient,
46
        TokenStorageInterface $storage,
47
        $scopes = [],
48
        ?UriInterface $baseApiUri = null
49
    ) {
50
        parent::__construct(
51
            $credentials,
52
            $httpClient,
53
            $storage,
54
            $scopes,
55
            $baseApiUri,
56
            true
57
        );
58
59
        if (null === $baseApiUri) {
60
            $this->baseApiUri = new Uri('https://api.deezer.com/');
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getAuthorizationEndpoint()
68
    {
69
        return new Uri('https://connect.deezer.com/oauth/auth.php');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getAccessTokenEndpoint()
76
    {
77
        return new Uri('https://connect.deezer.com/oauth/access_token.php');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function getAuthorizationMethod()
84
    {
85
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function parseAccessTokenResponse($responseBody)
92
    {
93
        parse_str($responseBody, $data);
94
        if (null === $data || !is_array($data) || empty($data)) {
95
            throw new TokenResponseException('Unable to parse response.');
96
        } elseif (isset($data['error'])) {
97
            throw new TokenResponseException(
98
                'Error in retrieving token: "' . $data['error'] . '"'
99
            );
100
        } elseif (isset($data['error_reason'])) {
101
            throw new TokenResponseException(
102
                'Error in retrieving token: "' . $data['error_reason'] . '"'
103
            );
104
        }
105
106
        $token = new StdOAuth2Token();
107
        $token->setAccessToken($data['access_token']);
108
        $token->setLifeTime($data['expires']);
109
110
        // I hope one day Deezer add a refresh token :)
111
        if (isset($data['refresh_token'])) {
112
            $token->setRefreshToken($data['refresh_token']);
113
            unset($data['refresh_token']);
114
        }
115
116
        unset($data['access_token'], $data['expires']);
117
118
        $token->setExtraParams($data);
119
120
        return $token;
121
    }
122
}
123