|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use MovingImage\Client\VMPro\Collection\ChannelCollection; |
|
7
|
|
|
use MovingImage\Client\VMPro\Collection\VideoCollection; |
|
8
|
|
|
use MovingImage\Client\VMPro\Entity\Channel; |
|
9
|
|
|
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters; |
|
10
|
|
|
use MovingImage\Client\VMPro\Entity\EmbedCode; |
|
11
|
|
|
use MovingImage\Client\VMPro\Entity\Video; |
|
12
|
|
|
use MovingImage\Client\VMPro\Entity\Attachment; |
|
13
|
|
|
use MovingImage\Client\VMPro\Entity\VideoManager; |
|
14
|
|
|
use MovingImage\Client\VMPro\Entity\VideoRequestParameters; |
|
15
|
|
|
use MovingImage\Client\VMPro\Entity\VideosRequestParameters; |
|
16
|
|
|
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; |
|
17
|
|
|
use MovingImage\Client\VMPro\Util\ChannelTrait; |
|
18
|
|
|
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait; |
|
19
|
|
|
use MovingImage\Client\VMPro\Util\SearchEndpointTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractApiClient. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ruben Knol <[email protected]> |
|
25
|
|
|
* @author Omid Rad <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use LoggerAwareTrait; |
|
30
|
|
|
use SearchEndpointTrait; |
|
31
|
|
|
use ChannelTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getChannels($videoManagerId) |
|
37
|
|
|
{ |
|
38
|
|
|
$response = $this->makeRequest('GET', 'channels', [ |
|
39
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
$rootChannel = $this->deserialize($response->getBody(), Channel::class); |
|
43
|
|
|
$rootChannel->setChildren($this->sortChannels($rootChannel->getChildren())); |
|
44
|
|
|
|
|
45
|
|
|
return $rootChannel; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ArrayCollection $channels |
|
52
|
|
|
* |
|
53
|
|
|
* @return ArrayCollection |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function sortChannels(ArrayCollection $channels) |
|
56
|
|
|
{ |
|
57
|
|
|
$channels->map(function ($channel) { |
|
58
|
|
|
$channel->setChildren($this->sortChannels($channel->getChildren())); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$iterator = $channels->getIterator(); |
|
62
|
|
|
$iterator->uasort(function ($a, $b) { |
|
63
|
|
|
return $a->getName() > $b->getName(); |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
|
return new ArrayCollection(iterator_to_array($iterator)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function createVideo( |
|
73
|
|
|
$videoManagerId, |
|
74
|
|
|
$fileName, |
|
75
|
|
|
$title = '', |
|
76
|
|
|
$description = '', |
|
77
|
|
|
$channel = null, |
|
78
|
|
|
$group = null, |
|
79
|
|
|
array $keywords = [], |
|
80
|
|
|
$autoPublish = null |
|
81
|
|
|
) { |
|
82
|
|
|
$response = $this->makeRequest('POST', 'videos', [ |
|
83
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
84
|
|
|
'json' => $this->buildJsonParameters( |
|
85
|
|
|
compact('fileName'), // Required parameters |
|
86
|
|
|
compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters |
|
87
|
|
|
), |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
|
91
|
|
|
// wraps headers in arrays. |
|
92
|
|
|
$videoLocation = is_array($response->getHeader('location')) |
|
93
|
|
|
? $response->getHeader('location')[0] |
|
94
|
|
|
: $response->getHeader('location'); |
|
95
|
|
|
|
|
96
|
|
|
$pieces = explode('/', $videoLocation); |
|
97
|
|
|
|
|
98
|
|
|
return end($pieces); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getVideos($videoManagerId, VideosRequestParameters $parameters = null) |
|
105
|
|
|
{ |
|
106
|
|
|
$options = [ |
|
107
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
if ($parameters) { |
|
111
|
|
|
$options['query'] = $parameters->getContainer(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$response = $this->makeRequest('GET', 'videos', $options); |
|
115
|
|
|
$response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']); |
|
116
|
|
|
|
|
117
|
|
|
return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>'); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function getCount($videoManagerId, VideosRequestParameters $parameters = null) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$options = [ |
|
126
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
if ($parameters) { |
|
130
|
|
|
$options['query'] = $parameters->getContainer(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$response = $this->makeRequest('GET', 'videos', $options); |
|
134
|
|
|
|
|
135
|
|
|
return json_decode($response->getBody()->getContents(), true)['total']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getVideoUploadUrl($videoManagerId, $videoId) |
|
142
|
|
|
{ |
|
143
|
|
|
$response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [ |
|
144
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
145
|
|
|
]); |
|
146
|
|
|
|
|
147
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
|
148
|
|
|
// wraps headers in arrays. |
|
149
|
|
|
return is_array($response->getHeader('location')) |
|
150
|
|
|
? $response->getHeader('location')[0] |
|
151
|
|
|
: $response->getHeader('location'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function updateVideo($videoManagerId, $videoId, $title, $description) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [ |
|
160
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
161
|
|
|
'json' => $this->buildJsonParameters([], compact('title', 'description')), |
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function addVideoToChannel($videoManagerId, $videoId, $channelId) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [ |
|
171
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setCustomMetaData($videoManagerId, $videoId, $metadata) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [ |
|
181
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
182
|
|
|
'json' => $metadata, |
|
183
|
|
|
]); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html') |
|
190
|
|
|
{ |
|
191
|
|
|
$url = sprintf( |
|
192
|
|
|
'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s', |
|
193
|
|
|
$videoId, |
|
194
|
|
|
$playerDefinitionId, |
|
195
|
|
|
$embedType |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
if ($this->cacheTtl) { |
|
199
|
|
|
$url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]); |
|
203
|
|
|
|
|
204
|
|
|
$data = \json_decode($response->getBody(), true); |
|
205
|
|
|
$embedCode = new EmbedCode(); |
|
206
|
|
|
$embedCode->setCode($data['embedCode']); |
|
207
|
|
|
|
|
208
|
|
|
return $embedCode; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* {@inheritdoc} |
|
213
|
|
|
*/ |
|
214
|
|
|
public function deleteVideo($videoManagerId, $videoId) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [ |
|
217
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
218
|
|
|
]); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
*/ |
|
224
|
|
View Code Duplication |
public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
$options = [ |
|
227
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
|
228
|
|
|
]; |
|
229
|
|
|
|
|
230
|
|
|
if ($parameters) { |
|
231
|
|
|
$options['query'] = $parameters->getContainer(); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$response = $this->makeRequest( |
|
235
|
|
|
'GET', |
|
236
|
|
|
sprintf('videos/%s', $videoId), |
|
237
|
|
|
$options |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
return $this->deserialize($response->getBody()->getContents(), Video::class); |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* {@inheritdoc} |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getAttachments($videoManagerId, $videoId) |
|
247
|
|
|
{ |
|
248
|
|
|
$response = $this->makeRequest( |
|
249
|
|
|
'GET', |
|
250
|
|
|
sprintf('videos/%s/attachments', $videoId), |
|
251
|
|
|
[self::OPT_VIDEO_MANAGER_ID => $videoManagerId] |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>'); |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* {@inheritdoc} |
|
259
|
|
|
*/ |
|
260
|
|
|
public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null) |
|
261
|
|
|
{ |
|
262
|
|
|
$options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters); |
|
263
|
|
|
$response = $this->makeRequest('POST', 'search', ['json' => $options]); |
|
264
|
|
|
$response = $this->normalizeSearchVideosResponse($response->getBody()->getContents()); |
|
265
|
|
|
|
|
266
|
|
|
$collection = $this->deserialize($response, VideoCollection::class); |
|
267
|
|
|
|
|
268
|
|
|
return $collection; |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* {@inheritdoc} |
|
273
|
|
|
*/ |
|
274
|
|
|
public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null) |
|
275
|
|
|
{ |
|
276
|
|
|
$options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters); |
|
277
|
|
|
$response = $this->makeRequest('POST', 'search', ['json' => $options]); |
|
278
|
|
|
$response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents()); |
|
279
|
|
|
/** @var ChannelCollection $collection */ |
|
280
|
|
|
$collection = $this->deserialize($response, ChannelCollection::class); |
|
281
|
|
|
|
|
282
|
|
|
//builds parent/children relations on all channels |
|
283
|
|
|
$channels = $this->setChannelRelations($collection->getChannels()); |
|
|
|
|
|
|
284
|
|
|
$collection->setChannels($channels); |
|
285
|
|
|
|
|
286
|
|
|
return $collection; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* {@inheritdoc} |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getVideoManagers() |
|
293
|
|
|
{ |
|
294
|
|
|
$response = $this->makeRequest('GET', '', []); |
|
295
|
|
|
|
|
296
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.VideoManager::class.'>'); |
|
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.