Dailymotion::parseAccessTokenResponse()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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