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