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