|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Libcast\AssetDistributor\Dailymotion; |
|
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\Dailymotion\Dailymotion; |
|
10
|
|
|
use Libcast\AssetDistributor\Request; |
|
11
|
|
|
|
|
12
|
|
|
class DailymotionAdapter extends AbstractAdapter implements Adapter |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $resource; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
* @throws \Exception |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getVendor() |
|
26
|
|
|
{ |
|
27
|
|
|
return 'Dailymotion'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @return Dailymotion |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getClient() |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->client) { |
|
37
|
|
|
return $this->client; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->client = new Dailymotion; |
|
41
|
|
|
$this->client->setGrantType( |
|
42
|
|
|
Dailymotion::GRANT_TYPE_AUTHORIZATION, |
|
43
|
|
|
$this->getConfiguration('key'), |
|
44
|
|
|
$this->getConfiguration('secret'), |
|
45
|
|
|
$this->getConfiguration('scopes') |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
if ($session = $this->getCredentials()) { |
|
49
|
|
|
$this->client->setSession($session); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!$this->isAuthenticated()) { |
|
53
|
|
|
$this->authenticate(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->client; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function authenticate() |
|
63
|
|
|
{ |
|
64
|
|
|
$client = $this->getClient(); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$client->getAccessToken(); |
|
68
|
|
|
$this->setCredentials($client->getSession()); |
|
69
|
|
|
|
|
70
|
|
|
} catch (\DailymotionAuthRequiredException $e) { |
|
71
|
|
|
$this->debug('Missing Dailymotion token, try to authenticate...'); |
|
72
|
|
|
$this->redirect($client->getAuthorizationUrl()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->setCredentials($client->getSession()); |
|
76
|
|
|
|
|
77
|
|
|
// Clean query parameters as they may cause error |
|
78
|
|
|
// on other Adapter's authentication process |
|
79
|
|
|
$request = Request::get(); |
|
80
|
|
|
$request->query->set('code', null); |
|
81
|
|
|
|
|
82
|
|
|
$this->debug('Dailymotion account is authenticated'); |
|
83
|
|
|
|
|
84
|
|
|
$this->isAuthenticated = true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function support(Asset $asset) |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$asset instanceof Video) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function upload(Asset $asset) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!self::support($asset)) { |
|
105
|
|
|
throw new \Exception('Dailymotion adapter only handles video assets'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$dailymotion = $this->getClient(); |
|
109
|
|
|
|
|
110
|
|
|
// Upload video |
|
111
|
|
|
$url = $dailymotion->uploadFile($asset->getPath()); |
|
112
|
|
|
|
|
113
|
|
|
// Publish video |
|
114
|
|
|
$request = $dailymotion->post('/me/videos', $this->getResource($asset, $url)); |
|
115
|
|
|
|
|
116
|
|
|
$this->remember($asset, $request['id']); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function update(Asset $asset) |
|
123
|
|
|
{ |
|
124
|
|
|
if (!$id = $this->retrieve($asset)) { |
|
125
|
|
|
throw new \Exception('Asset is unknown to Dailymotion'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$dailymotion = $this->getClient(); |
|
129
|
|
|
|
|
130
|
|
|
// Update video |
|
131
|
|
|
$dailymotion->post("/video/$id", $this->getResource($asset)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
View Code Duplication |
public function remove(Asset $asset) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!$id = $this->retrieve($asset)) { |
|
140
|
|
|
throw new \Exception('Asset is unknown to Dailymotion'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$dailymotion = $this->getClient(); |
|
144
|
|
|
|
|
145
|
|
|
// Delete video |
|
146
|
|
|
$dailymotion->delete("/video/$id"); |
|
147
|
|
|
|
|
148
|
|
|
$this->forget($asset); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* |
|
153
|
|
|
* @param Asset $asset |
|
154
|
|
|
* @param string $url |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getResource(Asset $asset, $url = null) |
|
158
|
|
|
{ |
|
159
|
|
|
$resource = [ |
|
160
|
|
|
'title' => $asset->getTitle(), |
|
161
|
|
|
'description' => $asset->getDescription(), |
|
162
|
|
|
'tags' => $asset->getTags(), |
|
163
|
|
|
'published' => !$asset->isHidden(), |
|
164
|
|
|
'private' => $asset->isPrivate(), |
|
165
|
|
|
]; |
|
166
|
|
|
|
|
167
|
|
|
if ($url) { |
|
168
|
|
|
$resource['url'] = $url; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return array_filter($resource); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|