Completed
Pull Request — master (#479)
by Andrey
02:39
created

Dailymotion::parseAccessTokenResponse()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 4
nop 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
/**
14
 * Dailymotion service.
15
 *
16
 * @author Mouhamed SEYE <[email protected]>
17
 * @link http://www.dailymotion.com/doc/api/authentication.html
18
 */
19
class 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