Vimeo::parseAccessTokenResponse()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 33
c 1
b 0
f 0
rs 8.6666
cc 7
nc 7
nop 1
1
<?php
2
/**
3
 * Vimeo service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 *
8
 * @see    https://developer.vimeo.com/
9
 * @see    https://developer.vimeo.com/api/authentication
10
 */
11
12
namespace OAuth\OAuth2\Service;
13
14
use OAuth\Common\Consumer\CredentialsInterface;
15
use OAuth\Common\Http\Client\ClientInterface;
16
use OAuth\Common\Http\Exception\TokenResponseException;
17
use OAuth\Common\Http\Uri\Uri;
18
use OAuth\Common\Http\Uri\UriInterface;
19
use OAuth\Common\Storage\TokenStorageInterface;
20
use OAuth\OAuth2\Token\StdOAuth2Token;
21
22
/**
23
 * Vimeo service.
24
 *
25
 * @author  Pedro Amorim <[email protected]>
26
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
27
 *
28
 * @see    https://developer.vimeo.com/
29
 * @see    https://developer.vimeo.com/api/authentication
30
 */
31
class Vimeo extends AbstractService
32
{
33
    // API version
34
    const VERSION = '3.2';
35
    // API Header Accept
36
    const HEADER_ACCEPT = 'application/vnd.vimeo.*+json;version=3.2';
37
38
    /**
39
     * Scopes.
40
     *
41
     * @see  https://developer.vimeo.com/api/authentication#scope
42
     */
43
    // View public videos
44
    const SCOPE_PUBLIC = 'public';
45
    // View private videos
46
    const SCOPE_PRIVATE = 'private';
47
    // View Vimeo On Demand purchase history
48
    const SCOPE_PURCHASED = 'purchased';
49
    // Create new videos, groups, albums, etc.
50
    const SCOPE_CREATE = 'create';
51
    // Edit videos, groups, albums, etc.
52
    const SCOPE_EDIT = 'edit';
53
    // Delete videos, groups, albums, etc.
54
    const SCOPE_DELETE = 'delete';
55
    // Interact with a video on behalf of a user, such as liking
56
    // a video or adding it to your watch later queue
57
    const SCOPE_INTERACT = 'interact';
58
    // Upload a video
59
    const SCOPE_UPLOAD = 'upload';
60
61
    public function __construct(
62
        CredentialsInterface $credentials,
63
        ClientInterface $httpClient,
64
        TokenStorageInterface $storage,
65
        $scopes = [],
66
        ?UriInterface $baseApiUri = null
67
    ) {
68
        parent::__construct(
69
            $credentials,
70
            $httpClient,
71
            $storage,
72
            $scopes,
73
            $baseApiUri,
74
            true
75
        );
76
77
        if (null === $baseApiUri) {
78
            $this->baseApiUri = new Uri('https://api.vimeo.com/');
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getAuthorizationEndpoint()
86
    {
87
        return new Uri('https://api.vimeo.com/oauth/authorize');
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getAccessTokenEndpoint()
94
    {
95
        return new Uri('https://api.vimeo.com/oauth/access_token');
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function getAuthorizationMethod()
102
    {
103
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function parseAccessTokenResponse($responseBody)
110
    {
111
        $data = json_decode($responseBody, true);
112
113
        if (null === $data || !is_array($data)) {
114
            throw new TokenResponseException('Unable to parse response.');
115
        } elseif (isset($data['error_description'])) {
116
            throw new TokenResponseException(
117
                'Error in retrieving token: "' . $data['error_description'] . '"'
118
            );
119
        } elseif (isset($data['error'])) {
120
            throw new TokenResponseException(
121
                'Error in retrieving token: "' . $data['error'] . '"'
122
            );
123
        }
124
125
        $token = new StdOAuth2Token();
126
        $token->setAccessToken($data['access_token']);
127
128
        if (isset($data['expires_in'])) {
129
            $token->setLifeTime($data['expires_in']);
130
            unset($data['expires_in']);
131
        }
132
        if (isset($data['refresh_token'])) {
133
            $token->setRefreshToken($data['refresh_token']);
134
            unset($data['refresh_token']);
135
        }
136
137
        unset($data['access_token']);
138
139
        $token->setExtraParams($data);
140
141
        return $token;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    protected function getExtraOAuthHeaders()
148
    {
149
        return ['Accept' => self::HEADER_ACCEPT];
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    protected function getExtraApiHeaders()
156
    {
157
        return ['Accept' => self::HEADER_ACCEPT];
158
    }
159
}
160