1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Libcast\AssetDistributor\Vimeo; |
4
|
|
|
|
5
|
|
|
use Libcast\AssetDistributor\Adapter\AbstractAdapter; |
6
|
|
|
use Libcast\AssetDistributor\Adapter\Adapter; |
7
|
|
|
use Libcast\AssetDistributor\Asset\Asset; |
8
|
|
|
use Libcast\AssetDistributor\Asset\Video; |
9
|
|
|
use Libcast\AssetDistributor\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
11
|
|
|
use Vimeo\Vimeo; |
12
|
|
|
|
13
|
|
|
class VimeoAdapter extends AbstractAdapter implements Adapter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
|
|
public function getVendor() |
21
|
|
|
{ |
22
|
|
|
return 'Vimeo'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @return Vimeo |
28
|
|
|
*/ |
29
|
|
|
public function getClient() |
30
|
|
|
{ |
31
|
|
|
if ($this->client) { |
32
|
|
|
return $this->client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$client = new Vimeo( |
36
|
|
|
$this->getConfiguration('id'), |
37
|
|
|
$this->getConfiguration('secret') |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
if ($token = $this->getCredentials()) { |
41
|
|
|
$client->setToken($token); |
42
|
|
|
$this->isAuthenticated = true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->client = $client; // Now getClient() returns \Vimeo\Vimeo |
46
|
|
|
|
47
|
|
|
if (!$this->isAuthenticated()) { |
48
|
|
|
$this->authenticate(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->client; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function authenticate() |
58
|
|
|
{ |
59
|
|
|
$client = $this->getClient(); |
60
|
|
|
$request = Request::get(); |
61
|
|
|
$session = new Session; |
62
|
|
|
|
63
|
|
|
if ($code = $request->query->get('code')) { |
64
|
|
|
if (!$requestState = $request->query->get('state')) { |
65
|
|
|
throw new \Exception('Missing state from Vimeo request'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$sessionState = $session->get('vimeo_state')) { |
69
|
|
|
throw new \Exception('Missing state from Vimeo session'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (strval($requestState) !== strval($sessionState)) { |
73
|
|
|
throw new \Exception('Vimeo session state and request state don\'t match'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->debug('Found Vimeo oAuth code', ['code' => $code]); |
77
|
|
|
$response = $client->accessToken($code, $this->getConfiguration('redirectUri')); |
78
|
|
|
|
79
|
|
|
if (200 === $response['status'] and $token = $response['body']['access_token']) { |
80
|
|
|
$this->debug('Found Vimeo oAuth token', ['token' => $token]); |
81
|
|
|
$client->setToken($token); |
82
|
|
|
$this->setCredentials($token); |
83
|
|
|
} else { |
84
|
|
|
$this->error('Vimeo authentication failed', $response); |
85
|
|
|
throw new \Exception('Vimeo authentication failed'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$client->getToken()) { |
90
|
|
|
$this->debug('Missing Vimeo token, try to authenticate...'); |
91
|
|
|
|
92
|
|
|
$state = mt_rand(); |
93
|
|
|
$session->set('vimeo_state', $state); |
94
|
|
|
|
95
|
|
|
$this->redirect($client->buildAuthorizationEndpoint( |
96
|
|
|
$this->getConfiguration('redirectUri'), |
97
|
|
|
$this->getConfiguration('scopes'), |
98
|
|
|
$state |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Clean query parameters as they may cause error |
103
|
|
|
// on other Adapter's authentication process |
104
|
|
|
$request->query->set('code', null); |
105
|
|
|
$request->query->set('state', null); |
106
|
|
|
|
107
|
|
|
$this->debug('Vimeo account is authenticated'); |
108
|
|
|
|
109
|
|
|
$this->isAuthenticated = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public static function support(Asset $asset) |
116
|
|
|
{ |
117
|
|
|
if (!$asset instanceof Video) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function upload(Asset $asset) |
128
|
|
|
{ |
129
|
|
|
if (!self::support($asset)) { |
130
|
|
|
throw new \Exception('Vimeo adapter only handles video assets'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!is_null($this->retrieve($asset))) { |
134
|
|
|
$this->update($asset); |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$uri = $this->getClient()->upload($asset->getPath()); |
139
|
|
|
|
140
|
|
|
$this->remember($asset, $uri); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function update(Asset $asset) |
147
|
|
|
{ |
148
|
|
|
/** @todo implement update */ |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function remove(Asset $asset) |
155
|
|
|
{ |
156
|
|
|
/** @todo implement remove */ |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|