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.

Players::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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