1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApiVideo\Client\Api; |
4
|
|
|
|
5
|
|
|
use ApiVideo\Client\Model\Live; |
6
|
|
|
use Buzz\Message\Form\FormUpload; |
7
|
|
|
|
8
|
|
|
class Lives extends BaseApi |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param string $liveStreamId |
12
|
|
|
* @return Live|null |
13
|
|
|
*/ |
14
|
|
|
public function get($liveStreamId) |
15
|
|
|
{ |
16
|
|
|
$response = $this->browser->get("/live-streams/$liveStreamId"); |
17
|
|
|
if (!$response->isSuccessful()) { |
18
|
|
|
$this->registerLastError($response); |
19
|
|
|
|
20
|
|
|
return null; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $this->unmarshal($response); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Incrementally iterate over a collection of elements. |
28
|
|
|
* By default the elements are returned in an array, unless you pass a |
29
|
|
|
* $callback which will be called for each instance of Video. |
30
|
|
|
* Available parameters: |
31
|
|
|
* - currentPage (int) current pagination page |
32
|
|
|
* - pageSize (int) number of elements per page |
33
|
|
|
* - liveIds (array) liveIds to limit the search to |
34
|
|
|
* If currentPage and pageSize are not given, the method iterates over all |
35
|
|
|
* pages of results and return an array containing all the results. |
36
|
|
|
* |
37
|
|
|
* @param array $parameters |
38
|
|
|
* @param callable $callback |
39
|
|
|
* @return Live[]|null |
40
|
|
|
*/ |
41
|
|
|
public function search(array $parameters = array(), $callback = null) |
42
|
|
|
{ |
43
|
|
|
$params = $parameters; |
44
|
|
|
$currentPage = isset($parameters['currentPage']) ? $parameters['currentPage'] : 1; |
45
|
|
|
$params['pageSize'] = isset($parameters['pageSize']) ? $parameters['pageSize'] : 100; |
46
|
|
|
$allLives = array(); |
47
|
|
|
|
48
|
|
|
do { |
49
|
|
|
$params['currentPage'] = $currentPage; |
50
|
|
|
$response = $this->browser->get('/live-streams?'.http_build_query($parameters)); |
51
|
|
|
|
52
|
|
|
if (!$response->isSuccessful()) { |
53
|
|
|
$this->registerLastError($response); |
54
|
|
|
|
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$json = json_decode($response->getContent(), true); |
59
|
|
|
$lives = $json['data']; |
60
|
|
|
|
61
|
|
|
$allLives[] = $this->castAll($lives); |
62
|
|
|
if (null !== $callback) { |
63
|
|
|
foreach (current($allLives) as $live) { |
64
|
|
|
call_user_func($callback, $live); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset($parameters['currentPage'])) { |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$pagination = $json['pagination']; |
73
|
|
|
$pagination['currentPage']++; |
74
|
|
|
} while ($pagination['pagesTotal'] > $pagination['currentPage']); |
75
|
|
|
$allLives = call_user_func_array('array_merge', $allLives); |
76
|
|
|
|
77
|
|
|
if (null === $callback) { |
78
|
|
|
return $allLives; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $name |
86
|
|
|
* @param array $properties |
87
|
|
|
* @return Live|null |
88
|
|
|
*/ |
89
|
|
|
public function create($name, array $properties = array()) |
90
|
|
|
{ |
91
|
|
|
$response = $this->browser->post( |
92
|
|
|
'/live-streams', |
93
|
|
|
array(), |
94
|
|
|
json_encode( |
95
|
|
|
array_merge( |
96
|
|
|
$properties, |
97
|
|
|
array('name' => $name) |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
if (!$response->isSuccessful()) { |
102
|
|
|
$this->registerLastError($response); |
103
|
|
|
|
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->unmarshal($response); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $source Path to the file to upload |
112
|
|
|
* @param string $liveStreamId |
113
|
|
|
* @return Live|null |
114
|
|
|
* @throws \Buzz\Exception\RequestException |
115
|
|
|
* @throws \UnexpectedValueException |
116
|
|
|
*/ |
117
|
|
|
public function uploadThumbnail($source, $liveStreamId) |
118
|
|
|
{ |
119
|
|
|
if (!is_readable($source)) { |
120
|
|
|
throw new \UnexpectedValueException("'$source' must be a readable source file."); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$resource = fopen($source, 'rb'); |
124
|
|
|
|
125
|
|
|
$stats = fstat($resource); |
|
|
|
|
126
|
|
|
$length = $stats['size']; |
127
|
|
|
if (0 >= $length) { |
128
|
|
|
throw new \UnexpectedValueException("'$source' is empty."); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$response = $this->browser->submit( |
132
|
|
|
"/live-streams/$liveStreamId/thumbnail", |
133
|
|
|
array('file' => new FormUpload($source)) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
if (!$response->isSuccessful()) { |
137
|
|
|
$this->registerLastError($response); |
138
|
|
|
|
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->unmarshal($response); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $liveStreamId |
147
|
|
|
* @param array $properties |
148
|
|
|
* @return Live|null |
149
|
|
|
*/ |
150
|
|
|
public function update($liveStreamId, array $properties) |
151
|
|
|
{ |
152
|
|
|
$response = $this->browser->patch( |
153
|
|
|
"/live-streams/$liveStreamId", |
154
|
|
|
array(), |
155
|
|
|
json_encode($properties) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
if (!$response->isSuccessful()) { |
159
|
|
|
$this->registerLastError($response); |
160
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->unmarshal($response); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $liveStreamId |
169
|
|
|
* @return int|null |
170
|
|
|
*/ |
171
|
|
|
public function delete($liveStreamId) |
172
|
|
|
{ |
173
|
|
|
$response = $this->browser->delete("/live-streams/$liveStreamId"); |
174
|
|
|
|
175
|
|
|
if (!$response->isSuccessful()) { |
176
|
|
|
$this->registerLastError($response); |
177
|
|
|
|
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $response->getStatusCode(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $data |
186
|
|
|
* @return Live |
187
|
|
|
*/ |
188
|
|
|
protected function cast(array $data) |
189
|
|
|
{ |
190
|
|
|
$live = new Live(); |
191
|
|
|
$live->liveStreamId = $data['liveStreamId']; |
192
|
|
|
$live->name = $data['name']; |
193
|
|
|
$live->streamKey = $data['streamKey']; |
194
|
|
|
$live->record = $data['record']; |
195
|
|
|
$live->broadcasting = $data['broadcasting']; |
196
|
|
|
$live->assets = $data['assets']; |
197
|
|
|
|
198
|
|
|
return $live; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|