Cloud::encodeVideoByUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaClient package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaClient\Api;
13
14
use Xabbuh\PandaClient\Model\Encoding;
15
use Xabbuh\PandaClient\Model\Notifications;
16
use Xabbuh\PandaClient\Model\Profile;
17
use Xabbuh\PandaClient\Model\Video;
18
use Xabbuh\PandaClient\Transformer\TransformerRegistryInterface;
19
20
/**
21
 * Object-oriented interface to easily access a Panda cloud.
22
 *
23
 * The implementation provides methods for accessing all endpoints of the Panda
24
 * encoding REST webservice. Each method is mapped to a corresponding HTTP
25
 * request.
26
 *
27
 * @author Christian Flothmann <[email protected]>
28
 */
29
class Cloud implements CloudInterface
30
{
31
    /**
32
     * The client which is used to perform the requests to the REST api
33
     *
34
     * @var HttpClientInterface
35
     */
36
    private $httpClient;
37
38
    /**
39
     * @var TransformerRegistryInterface
40
     */
41
    private $transformers;
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 58
    public function setHttpClient(HttpClientInterface $httpClient)
47
    {
48 58
        $this->httpClient = $httpClient;
49 58
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 3
    public function getHttpClient()
55
    {
56 3
        return $this->httpClient;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 58
    public function setTransformers(TransformerRegistryInterface $transformers)
63
    {
64 58
        $this->transformers = $transformers;
65 58
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 1
    public function getTransformers()
71
    {
72 1
        return $this->transformers;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 1
    public function getVideos()
79
    {
80 1
        $response = $this->httpClient->get('/videos.json');
81 1
        $transformer = $this->transformers->getVideoTransformer();
82
83 1
        return $transformer->stringToVideoCollection($response);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 3
    public function getVideosForPagination($page = 1, $per_page = 100)
90
    {
91 3
        $response = $this->httpClient->get(
92 3
            '/videos.json',
93
            array(
94 3
                'include_root' => true,
95 3
                'page' => $page,
96 3
                'per_page' => $per_page
97
            )
98
        );
99 3
        $transformer = $this->transformers->getVideoTransformer();
100 3
        $result = json_decode($response);
101 3
        $result->videos = $transformer->stringToVideoCollection(json_encode($result->videos));
102
103 3
        return $result;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 1
    public function getVideo($videoId)
110
    {
111 1
        $response = $this->httpClient->get('/videos/'.$videoId.'.json');
112 1
        $transformer = $this->transformers->getVideoTransformer();
113
114 1
        return $transformer->stringToVideo($response);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 1
    public function getVideoMetadata($videoId)
121
    {
122 1
        $response = $this->httpClient->get('/videos/'.$videoId.'/metadata.json');
123
124 1
        return get_object_vars(json_decode($response));
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 1
    public function deleteVideo(Video $video)
131
    {
132 1
        return $this->httpClient->delete('/videos/'.$video->getId().'.json');
133
    }
134
135
    /**
136
     * Deletes the source of a video from the storage.
137
     *
138
     * @param Video $video The video whose source will be deleted
139
     *
140
     * @return string The server response
141
     */
142 1
    public function deleteVideoSource(Video $video)
143
    {
144 1
        return $this->httpClient->delete('/videos/'.$video->getId().'/source.json');
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 5
    public function encodeVideoByUrl($url, array $profiles = array(), $pathFormat = null, $payload = null)
151
    {
152 5
        return $this->doEncodeVideo(array('source_url' => $url), $profiles, $pathFormat, $payload);
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 5
    public function encodeVideoFile($localPath, array $profiles = array(), $pathFormat = null, $payload = null)
159
    {
160 5
        return $this->doEncodeVideo(array('file' => '@'.$localPath), $profiles, $pathFormat, $payload);
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 3
    public function registerUpload($filename, $fileSize, array $profiles = null, $useAllProfiles = false)
167
    {
168 3
        if (null !== $profiles) {
169
            $options = array(
170 1
                'file_name' => $filename,
171 1
                'file_size' => $fileSize,
172 1
                'profiles' => implode(',', $profiles)
173
            );
174
        } else {
175
            $options = array(
176 2
                'file_name' => $filename,
177 2
                'file_size' => $fileSize,
178 2
                'use_all_profiles' => $useAllProfiles
179
            );
180
        }
181
182 3
        return json_decode($this->httpClient->post('/videos/upload.json', $options));
183
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188 12
    public function getEncodings(array $filter = null)
189
    {
190 12
        $response = $this->httpClient->get('/encodings.json', null === $filter ? array() : $filter);
191 12
        $transformer = $this->transformers->getEncodingTransformer();
192
193 12
        return $transformer->stringToEncodingCollection($response);
194
    }
195
196
    /**
197
     * {@inheritDoc}
198
     */
199 2
    public function getEncodingsWithStatus($status, array $filter = null)
200
    {
201 2
        if (null === $filter) {
202 1
            $filter = array();
203
        }
204
205 2
        $filter['status'] = $status;
206
207 2
        return $this->getEncodings($filter);
208
    }
209
210
    /**
211
     * {@inheritDoc}
212
     */
213 2
    public function getEncodingsForProfile(Profile $profile, array $filter = null)
214
    {
215 2
        if (null === $filter) {
216 1
            $filter = array();
217
        }
218
219 2
        $filter['profile_id'] = $profile->getId();
220
221 2
        return $this->getEncodings($filter);
222
    }
223
224
    /**
225
     * {@inheritDoc}
226
     */
227 2
    public function getEncodingsForProfileById($profileId, array $filter = null)
228
    {
229 2
        if (null === $filter) {
230 1
            $filter = array();
231
        }
232
233 2
        $filter['profile_id'] = $profileId;
234
235 2
        return $this->getEncodings($filter);
236
    }
237
238
    /**
239
     * {@inheritDoc}
240
     */
241 2
    public function getEncodingsForProfileByName($profileName, array $filter = null)
242
    {
243 2
        if (null === $filter) {
244 1
            $filter = array();
245
        }
246
247 2
        $filter['profile_name'] = $profileName;
248
249 2
        return $this->getEncodings($filter);
250
    }
251
252
    /**
253
     * {@inheritDoc}
254
     */
255 2
    public function getEncodingsForVideo(Video $video, array $filter = null)
256
    {
257 2
        if (null === $filter) {
258 1
            $filter = array();
259
        }
260
261 2
        $filter['video_id'] = $video->getId();
262
263 2
        return $this->getEncodings($filter);
264
    }
265
266
    /**
267
     * {@inheritDoc}
268
     */
269 1
    public function getEncoding($encodingId)
270
    {
271 1
        $response = $this->httpClient->get('/encodings/'.$encodingId.'.json');
272 1
        $transformer = $this->transformers->getEncodingTransformer();
273
274 1
        return $transformer->stringToEncoding($response);
275
    }
276
277
    /**
278
     * {@inheritDoc}
279
     */
280 1
    public function createEncoding(Video $video, Profile $profile)
281
    {
282 1
        return $this->createEncodingWithProfileId($video, $profile->getId());
283
    }
284
285
    /**
286
     * {@inheritDoc}
287
     */
288 2
    public function createEncodingWithProfileId(Video $video, $profileId)
289
    {
290 2
        return $this->doCreateEncoding(array(
291 2
            'video_id' => $video->getId(),
292 2
            'profile_id' => $profileId,
293
        ));
294
    }
295
296
    /**
297
     * {@inheritDoc}
298
     */
299 1
    public function createEncodingWithProfileName(Video $video, $profileName)
300
    {
301 1
        return $this->doCreateEncoding(array(
302 1
            'video_id' => $video->getId(),
303 1
            'profile_name' => $profileName,
304
        ));
305
    }
306
307
    /**
308
     * {@inheritDoc}
309
     */
310 1
    public function cancelEncoding(Encoding $encoding)
311
    {
312 1
        return $this->httpClient->post('/encodings/'.$encoding->getId().'/cancel.json');
313
    }
314
315
    /**
316
     * {@inheritDoc}
317
     */
318 1
    public function retryEncoding(Encoding $encoding)
319
    {
320 1
        return $this->httpClient->post('/encodings/'.$encoding->getId().'/retry.json');
321
    }
322
323
    /**
324
     * {@inheritDoc}
325
     */
326 1
    public function deleteEncoding(Encoding $encoding)
327
    {
328 1
        return $this->httpClient->delete('/encodings/'.$encoding->getId().'.json');
329
    }
330
331
    /**
332
     * {@inheritDoc}
333
     */
334 1
    public function getProfiles()
335
    {
336 1
        $response = $this->httpClient->get('/profiles.json');
337 1
        $transformer = $this->transformers->getProfileTransformer();
338
339 1
        return $transformer->stringToProfileCollection($response);
340
    }
341
342
    /**
343
     * {@inheritDoc}
344
     */
345 1
    public function getProfile($profileId)
346
    {
347 1
        $response = $this->httpClient->get('/profiles/'.$profileId.'.json');
348 1
        $transformer = $this->transformers->getProfileTransformer();
349
350 1
        return $transformer->stringToProfile($response);
351
    }
352
353
    /**
354
     * {@inheritDoc}
355
     */
356 1
    public function addProfile(Profile $profile)
357
    {
358 1
        $transformer = $this->transformers->getProfileTransformer();
359 1
        $requestParameters = $transformer->toRequestParams($profile);
360 1
        $response = $this->httpClient->post('/profiles.json', $requestParameters->all());
361
362 1
        return $transformer->stringToProfile($response);
363
    }
364
365
    /**
366
     * {@inheritDoc}
367
     */
368 1
    public function addProfileFromPreset($presetName)
369
    {
370 1
        $response = $this->httpClient->post(
371 1
            '/profiles.json',
372 1
            array('preset_name' => $presetName)
373
        );
374 1
        $transformer = $this->transformers->getProfileTransformer();
375
376 1
        return $transformer->stringToProfile($response);
377
    }
378
379
    /**
380
     * {@inheritDoc}
381
     */
382 1
    public function setProfile(Profile $profile)
383
    {
384 1
        $transformer = $this->transformers->getProfileTransformer();
385 1
        $response = $this->httpClient->put(
386 1
            '/profiles/'.$profile->getId().'.json',
387 1
            $transformer->toRequestParams($profile)->all()
388
        );
389
390 1
        return $transformer->stringToProfile($response);
391
    }
392
393
    /**
394
     * {@inheritDoc}
395
     */
396 1
    public function deleteProfile(Profile $profile)
397
    {
398 1
        return $this->httpClient->delete('/profiles/'.$profile->getId().'.json');
399
    }
400
401
    /**
402
     * {@inheritDoc}
403
     */
404 2
    public function getCloud($cloudId = null)
405
    {
406 2
        if (null === $cloudId) {
407 1
            $cloudId = $this->httpClient->getCloudId();
408
        }
409
410 2
        $response = $this->httpClient->get('/clouds/'.$cloudId.'.json');
411 2
        $transformer = $this->transformers->getCloudTransformer();
412
413 2
        return $transformer->stringToCloud($response);
414
    }
415
416
    /**
417
     * {@inheritDoc}
418
     */
419 2
    public function setCloud(array $data, $cloudId = null)
420
    {
421 2
        if (null === $cloudId) {
422 1
            $cloudId = $this->httpClient->getCloudId();
423
        }
424
425 2
        $response = $this->httpClient->put('/clouds/'.$cloudId.'.json', $data);
426 2
        $transformer = $this->transformers->getCloudTransformer();
427
428 2
        return $transformer->stringToCloud($response);
429
    }
430
431
    /**
432
     * {@inheritDoc}
433
     */
434 1
    public function getNotifications()
435
    {
436 1
        $response = $this->httpClient->get('/notifications.json');
437 1
        $transformer = $this->transformers->getNotificationsTransformer();
438
439 1
        return $transformer->stringToNotifications($response);
440
    }
441
442
    /**
443
     * {@inheritDoc}
444
     */
445 1
    public function setNotifications(Notifications $notifications)
446
    {
447 1
        $transformer = $this->transformers->getNotificationsTransformer();
448 1
        $data = $transformer->toRequestParams($notifications)->all();
449 1
        $response = $this->httpClient->put('/notifications.json', $data);
450
451 1
        return $transformer->stringToNotifications($response);
452
    }
453
454 10
    private function doEncodeVideo(array $parameters, array $profiles, $pathFormat, $payload)
455
    {
456 10
        if (count($profiles) > 0) {
457 4
            $parameters['profiles'] = implode(',', $profiles);
458
        }
459
460 10
        if (null !== $pathFormat) {
461 4
            $parameters['path_format'] = $pathFormat;
462
        }
463
464 10
        if (null !== $payload) {
465 4
            $parameters['payload'] = $payload;
466
        }
467
468 10
        $response = $this->httpClient->post('/videos.json', $parameters);
469 10
        $transformer = $this->transformers->getVideoTransformer();
470
471 10
        return $transformer->stringToVideo($response);
472
    }
473
474 3
    private function doCreateEncoding(array $params)
475
    {
476 3
        $response = $this->httpClient->post('/encodings.json', $params);
477 3
        $transformer = $this->transformers->getEncodingTransformer();
478
479 3
        return $transformer->stringToEncoding($response);
480
    }
481
}
482