GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Lives   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 96
c 1
b 0
f 0
dl 0
loc 238
rs 10

9 Methods

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