Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Vimeo::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 15
nc 2
nop 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Vimeo::getAuthorizationEndpoint() 0 4 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
 * @link    https://developer.vimeo.com/
8
 * @link    https://developer.vimeo.com/api/authentication
9
 */
10
11
namespace OAuth\OAuth2\Service;
12
13
use OAuth\OAuth2\Token\StdOAuth2Token;
14
use OAuth\Common\Http\Exception\TokenResponseException;
15
use OAuth\Common\Http\Uri\Uri;
16
17
/**
18
 * Vimeo service.
19
 *
20
 * @author  Pedro Amorim <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
22
 * @link    https://developer.vimeo.com/
23
 * @link    https://developer.vimeo.com/api/authentication
24
 */
25
class Vimeo extends AbstractService
26
{
27
    // API version
28
    const VERSION = '3.2';
29
    // API Header Accept
30
    const HEADER_ACCEPT = 'application/vnd.vimeo.*+json;version=3.2';
31
32
    /**
33
     * Scopes
34
     * @see  https://developer.vimeo.com/api/authentication#scope
35
     */
36
    // View public videos
37
    const SCOPE_PUBLIC    = 'public';
38
    // View private videos
39
    const SCOPE_PRIVATE   = 'private';
40
    // View Vimeo On Demand purchase history
41
    const SCOPE_PURCHASED = 'purchased';
42
    // Create new videos, groups, albums, etc.
43
    const SCOPE_CREATE    = 'create';
44
    // Edit videos, groups, albums, etc.
45
    const SCOPE_EDIT      = 'edit';
46
    // Delete videos, groups, albums, etc.
47
    const SCOPE_DELETE    = 'delete';
48
    // Interact with a video on behalf of a user, such as liking
49
    // a video or adding it to your watch later queue
50
    const SCOPE_INTERACT  = 'interact';
51
    // Upload a video
52
    const SCOPE_UPLOAD    = 'upload';
53
    
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function init()
58
    {
59
        $this->stateParameterInAuthUrl = true;
60
61
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
62
            $this->baseApiUri = new Uri('https://api.vimeo.com/');
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getAuthorizationEndpoint()
70
    {
71
        return new Uri('https://api.vimeo.com/oauth/authorize');
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getAccessTokenEndpoint()
78
    {
79
        return new Uri('https://api.vimeo.com/oauth/access_token');
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function getAuthorizationMethod()
86
    {
87
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function parseAccessTokenResponse($responseBody)
94
    {
95
        $data = json_decode($responseBody, true);
96
97
        if (null === $data || !is_array($data)) {
98
            throw new TokenResponseException('Unable to parse response.');
99
        } elseif (isset($data['error_description'])) {
100
            throw new TokenResponseException(
101
                'Error in retrieving token: "' . $data['error_description'] . '"'
102
            );
103
        } elseif (isset($data['error'])) {
104
            throw new TokenResponseException(
105
                'Error in retrieving token: "' . $data['error'] . '"'
106
            );
107
        }
108
109
        $token = new StdOAuth2Token();
110
        $token->setAccessToken($data['access_token']);
111
112
        if (isset($data['expires_in'])) {
113
            $token->setLifeTime($data['expires_in']);
114
            unset($data['expires_in']);
115
        }
116
        if (isset($data['refresh_token'])) {
117
            $token->setRefreshToken($data['refresh_token']);
118
            unset($data['refresh_token']);
119
        }
120
121
        unset($data['access_token']);
122
123
        $token->setExtraParams($data);
124
125
        return $token;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    protected function getExtraOAuthHeaders()
132
    {
133
        return array('Accept' => self::HEADER_ACCEPT);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    protected function getExtraApiHeaders()
140
    {
141
        return array('Accept' => self::HEADER_ACCEPT);
142
    }
143
}
144