1<?php
2
3namespace OAuth\OAuth2\Service;
4
5use OAuth\OAuth2\Token\StdOAuth2Token;
6use OAuth\Common\Http\Exception\TokenResponseException;
7use OAuth\Common\Http\Uri\Uri;
8use OAuth\Common\Consumer\CredentialsInterface;
9use OAuth\Common\Http\Client\ClientInterface;
10use OAuth\Common\Storage\TokenStorageInterface;
11use OAuth\Common\Http\Uri\UriInterface;
12
13/**
14 * Dailymotion service.
15 *
16 * @author Mouhamed SEYE <mouhamed@seye.pro>
17 * @link http://www.dailymotion.com/doc/api/authentication.html
18 */
19class Dailymotion extends AbstractService
20{
21    /**
22     * Scopes
23     *
24     * @var string
25     */
26    const SCOPE_EMAIL         = 'email',
27          SCOPE_PROFILE       = 'userinfo',
28          SCOPE_VIDEOS        = 'manage_videos',
29          SCOPE_COMMENTS      = 'manage_comments',
30          SCOPE_PLAYLIST      = 'manage_playlists',
31          SCOPE_TILES         = 'manage_tiles',
32          SCOPE_SUBSCRIPTIONS = 'manage_subscriptions',
33          SCOPE_FRIENDS       = 'manage_friends',
34          SCOPE_FAVORITES     = 'manage_favorites',
35          SCOPE_GROUPS        = 'manage_groups';
36
37    /**
38     * Dialog form factors
39     *
40     * @var string
41     */
42    const DISPLAY_PAGE   = 'page',
43          DISPLAY_POPUP  = 'popup',
44          DISPLAY_MOBILE = 'mobile';
45
46    /**
47    * {@inheritdoc}
48    */
49    public function __construct(
50        CredentialsInterface $credentials,
51        ClientInterface $httpClient,
52        TokenStorageInterface $storage,
53        $scopes = array(),
54        UriInterface $baseApiUri = null
55    ) {
56        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
57
58        if (null === $baseApiUri) {
59            $this->baseApiUri = new Uri('https://api.dailymotion.com/');
60        }
61    }
62
63    /**
64     * {@inheritdoc}
65     */
66    public function getAuthorizationEndpoint()
67    {
68        return new Uri('https://api.dailymotion.com/oauth/authorize');
69    }
70
71    /**
72     * {@inheritdoc}
73     */
74    public function getAccessTokenEndpoint()
75    {
76        return new Uri('https://api.dailymotion.com/oauth/token');
77    }
78
79    /**
80     * {@inheritdoc}
81     */
82    protected function getAuthorizationMethod()
83    {
84        return static::AUTHORIZATION_METHOD_HEADER_OAUTH;
85    }
86
87    /**
88     * {@inheritdoc}
89     */
90    protected function parseAccessTokenResponse($responseBody)
91    {
92        $data = json_decode($responseBody, true);
93
94        if (null === $data || !is_array($data)) {
95            throw new TokenResponseException('Unable to parse response.');
96        } elseif (isset($data['error_description']) || isset($data['error'])) {
97            throw new TokenResponseException(
98                sprintf(
99                    'Error in retrieving token: "%s"',
100                    isset($data['error_description']) ? $data['error_description'] : $data['error']
101                )
102            );
103        }
104
105        $token = new StdOAuth2Token();
106        $token->setAccessToken($data['access_token']);
107        $token->setLifeTime($data['expires_in']);
108
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_in']);
116
117        $token->setExtraParams($data);
118
119        return $token;
120    }
121
122    /**
123     * {@inheritdoc}
124     */
125    protected function getExtraOAuthHeaders()
126    {
127        return array('Accept' => 'application/json');
128    }
129}
130