1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
4
|
|
|
|
5
|
|
|
use MovingImage\Client\VMPro\Entity\Channel; |
6
|
|
|
use MovingImage\Client\VMPro\Entity\Video; |
7
|
|
|
use MovingImage\Client\VMPro\Entity\VideoDownloadUrl; |
8
|
|
|
use MovingImage\Client\VMPro\Entity\VideosRequestParameters; |
9
|
|
|
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; |
10
|
|
|
use MovingImage\Util\Logging\Traits\LoggerAwareTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractApiClient. |
14
|
|
|
* |
15
|
|
|
* @author Ruben Knol <[email protected]> |
16
|
|
|
* @author Omid Rad <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
4 |
|
*/ |
25
|
|
|
public function getChannels($videoManagerId) |
26
|
4 |
|
{ |
27
|
4 |
|
$response = $this->makeRequest('GET', 'channels', [ |
28
|
4 |
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
29
|
|
|
]); |
30
|
4 |
|
|
31
|
|
|
return $this->deserialize($response->getBody(), Channel::class); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
10 |
|
*/ |
37
|
|
|
public function createVideo( |
38
|
|
|
$videoManagerId, |
39
|
|
|
$fileName, |
40
|
|
|
$title = '', |
41
|
|
|
$description = '', |
42
|
|
|
$channel = null, |
43
|
|
|
$group = null, |
44
|
|
|
array $keywords = [], |
45
|
|
|
$autoPublish = null |
46
|
10 |
|
) { |
47
|
10 |
|
$response = $this->makeRequest('POST', 'videos', [ |
48
|
10 |
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
49
|
10 |
|
'json' => $this->buildJsonParameters( |
50
|
10 |
|
compact('fileName'), // Required parameters |
51
|
10 |
|
compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters |
52
|
8 |
|
), |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
56
|
6 |
|
// wraps headers in arrays. |
57
|
6 |
|
$videoLocation = is_array($response->getHeader('location')) |
58
|
6 |
|
? $response->getHeader('location')[0] |
59
|
|
|
: $response->getHeader('location'); |
60
|
6 |
|
|
61
|
|
|
$pieces = explode('/', $videoLocation); |
62
|
6 |
|
|
63
|
|
|
return end($pieces); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getVideos($videoManagerId, VideosRequestParameters $parameters = null) |
70
|
|
|
{ |
71
|
|
|
$options = [ |
72
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
if ($parameters) { |
76
|
|
|
$options['query'] = $parameters->getContainer(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$response = $this->makeRequest('GET', 'videos', $options); |
80
|
|
|
$response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']); |
81
|
|
|
|
82
|
|
|
return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>'); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
4 |
|
*/ |
88
|
|
|
public function getVideoUploadUrl($videoManagerId, $videoId) |
89
|
4 |
|
{ |
90
|
4 |
|
$response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [ |
91
|
4 |
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
95
|
2 |
|
// wraps headers in arrays. |
96
|
2 |
|
return is_array($response->getHeader('location')) |
97
|
2 |
|
? $response->getHeader('location')[0] |
98
|
|
|
: $response->getHeader('location'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getVideoDownloadUrls($videoManagerId, $videoId) |
105
|
|
|
{ |
106
|
|
|
$response = $this->makeRequest('GET', sprintf('videos/%s/download-urls', $videoId), [ |
107
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
108
|
|
|
]); |
109
|
|
|
$response = $response->getBody()->getContents(); |
110
|
|
|
|
111
|
|
|
return $this->deserialize($response, 'ArrayCollection<'.VideoDownloadUrl::class.'>'); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function updateVideo($videoManagerId, $videoId, $title, $description) |
118
|
|
|
{ |
119
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [ |
120
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
121
|
|
|
'json' => $this->buildJsonParameters([], compact('title', 'description')), |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function addVideoToChannel($videoManagerId, $videoId, $channelId) |
129
|
|
|
{ |
130
|
|
|
$this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [ |
131
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function setCustomMetaData($videoManagerId, $videoId, $metadata) |
139
|
|
|
{ |
140
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [ |
141
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
142
|
|
|
'json' => $metadata, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html') |
150
|
|
|
{ |
151
|
|
|
$response = $this->makeRequest('GET', |
152
|
|
|
sprintf('videos/%s/embed-codes?player_definition_id=%s&embed_type=%s', |
153
|
|
|
$videoId, $playerDefinitionId, $embedType), [ |
154
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$data = \json_decode($response->getBody(), true); |
159
|
|
|
|
160
|
|
|
return $data['embedCode']; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.