1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MovingImage\Client\VMPro\Util; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters; |
9
|
|
|
use MovingImage\Client\VMPro\Entity\VideosRequestParameters; |
10
|
|
|
use MovingImage\Client\VMPro\Exception; |
11
|
|
|
use MovingImage\Meta\Enums\PublicationState; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Helper methods for dealing with search endpoint. |
15
|
|
|
*/ |
16
|
|
|
trait SearchEndpointTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Creates an elastic search query from the provided array od parameters. |
20
|
|
|
*/ |
21
|
|
|
private function createElasticSearchQuery(array $params, ?string $operator = 'AND'): string |
22
|
|
|
{ |
23
|
|
|
$filteredParams = []; |
24
|
|
|
foreach ($params as $name => $value) { |
25
|
|
|
if (empty($name) || empty($value)) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$filteredParams[] = "$name:$value"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return implode(" $operator ", $filteredParams); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adjust the response from the `search` endpoint for video data type, |
37
|
|
|
* so that it is compatible with the response of `videos` endpoint. |
38
|
|
|
* Namely, it converts the date to a timestamp, adjusts the format of channels array |
39
|
|
|
* and re-maps some renamed properties (such as duration). |
40
|
|
|
* It also renames root-level properties so that they can be correctly unserialized. |
41
|
|
|
* |
42
|
|
|
* @throws Exception |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
private function normalizeSearchVideosResponse(string $response): string |
46
|
|
|
{ |
47
|
|
|
$response = json_decode($response, true); |
48
|
|
|
if (!is_array($response) || !array_key_exists('result', $response) || !array_key_exists('total', $response)) { |
49
|
|
|
throw new Exception('Invalid response from search endpoint'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$response['totalCount'] = $response['total']; |
53
|
|
|
$response['videos'] = $response['result']; |
54
|
|
|
unset($response['result'], $response['total']); |
55
|
|
|
|
56
|
|
|
foreach ($response['videos'] as &$video) { |
57
|
|
|
$video['uploadDate'] = $video['createdDate']; |
58
|
|
|
$video['length'] = $video['duration']; |
59
|
|
|
foreach ($video as $prop => $value) { |
60
|
|
|
if (in_array($prop, ['createdDate', 'modifiedDate', 'uploadDate'])) { |
61
|
|
|
$video[$prop] = (new DateTime($value))->getTimestamp(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ('channels' === $prop) { |
65
|
|
|
foreach ($value as $channelIndex => $channelId) { |
66
|
|
|
$video[$prop][$channelIndex] = [ |
67
|
|
|
'id' => $channelId, |
68
|
|
|
'name' => '', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return json_encode($response); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Adjust the response from the `search` endpoint for channel data type. |
80
|
|
|
* Namely, it renames the root-level properties, so they can be correctly unserialized. |
81
|
|
|
* |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
private function normalizeSearchChannelsResponse(string $response): string |
85
|
|
|
{ |
86
|
|
|
$response = json_decode($response, true); |
87
|
|
|
if (!is_array($response) || !array_key_exists('result', $response) || !array_key_exists('total', $response)) { |
88
|
|
|
throw new Exception('Invalid response from search endpoint'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$response['totalCount'] = $response['total']; |
92
|
|
|
$response['channels'] = $response['result']; |
93
|
|
|
unset($response['result'], $response['total']); |
94
|
|
|
|
95
|
|
|
return json_encode($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adjust the response from the `search` endpoint for transcodingStatus data type. |
100
|
|
|
* Namely, it renames the root-level properties, so they can be correctly unserialized. |
101
|
|
|
* |
102
|
|
|
* @throws Exception |
103
|
|
|
*/ |
104
|
|
|
private function normalizeSearchTranscodingStatusResponse(string $response): string |
105
|
|
|
{ |
106
|
|
|
$response = json_decode($response, true); |
107
|
|
|
if (!is_array($response)) { |
108
|
|
|
throw new Exception('Invalid response from search endpoint'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$response = [ |
112
|
|
|
'transcodes' => $response, |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
return json_encode($response); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @throws Exception |
120
|
|
|
*/ |
121
|
|
|
private function getTotalCountFromSearchVideosResponse(string $response): int |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$response = json_decode($response, true); |
124
|
|
|
if (!is_array($response) || !array_key_exists('total', $response)) { |
125
|
|
|
throw new Exception('Response from search endpoint is missing the "total" key'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return (int) $response['total']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function getRequestOptionsForSearchVideosEndpoint( |
132
|
|
|
int $videoManagerId, |
133
|
|
|
?VideosRequestParameters $parameters = null |
134
|
|
|
): array { |
135
|
|
|
$options = [ |
136
|
|
|
'documentType' => 'video', |
137
|
|
|
'videoManagerIds' => [$videoManagerId], |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$queryParams = []; |
141
|
|
|
|
142
|
|
|
if ($parameters) { |
143
|
|
|
$queryParams += [ |
144
|
|
|
'channels' => $parameters->getChannelId(), |
145
|
|
|
'id' => $parameters->getVideoId(), |
146
|
|
|
$parameters->getSearchInField() => $parameters->getSearchTerm(), |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
switch ($parameters->getPublicationState()) { |
150
|
|
|
case PublicationState::PUBLISHED: |
151
|
|
|
$queryParams['published'] = 'true'; |
152
|
|
|
break; |
153
|
|
|
case PublicationState::NOT_PUBLISHED: |
154
|
|
|
$queryParams['published'] = 'false'; |
155
|
|
|
break; |
156
|
|
|
//case 'all': do nothing |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$options += [ |
160
|
|
|
'size' => $parameters->getLimit(), |
161
|
|
|
'from' => $parameters->getOffset(), |
162
|
|
|
'orderBy' => $parameters->getOrderProperty(), |
163
|
|
|
'order' => $parameters->getOrder(), |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
if ($parameters->getMetadataSetKey()) { |
167
|
|
|
$options['metaDataSetKey'] = $parameters->getMetadataSetKey(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$options['query'] = $this->createElasticSearchQuery($queryParams); |
172
|
|
|
|
173
|
|
|
return $options; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function getRequestOptionsForSearchChannelsEndpoint( |
177
|
|
|
int $videoManagerId, |
178
|
|
|
ChannelsRequestParameters $parameters = null |
179
|
|
|
): array { |
180
|
|
|
$options = [ |
181
|
|
|
'documentType' => 'channel', |
182
|
|
|
'videoManagerIds' => [$videoManagerId], |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
$queryParams = [ |
186
|
|
|
'videoManagerId' => $videoManagerId, |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
if ($parameters) { |
190
|
|
|
$queryParams += [ |
191
|
|
|
$parameters->getSearchInField() => $parameters->getSearchTerm(), |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
$options += [ |
195
|
|
|
'size' => $parameters->getLimit(), |
196
|
|
|
'from' => $parameters->getOffset(), |
197
|
|
|
'orderBy' => $parameters->getOrderProperty(), |
198
|
|
|
'order' => $parameters->getOrder(), |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
if ($parameters->getMetadataSetKey()) { |
202
|
|
|
$options['metaDataSetKey'] = $parameters->getMetadataSetKey(); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$options['query'] = $this->createElasticSearchQuery($queryParams); |
207
|
|
|
|
208
|
|
|
return $options; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|