Completed
Pull Request — master (#498)
by Dragonqos
04:00
created

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