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.
Completed
Push — master ( 190bf2...86e13d )
by Mozammil
01:11
created

Files::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mozammil\Putio\Endpoints;
4
5
use Mozammil\Putio\Http\Client;
6
use Mozammil\Putio\Exceptions\FileNotFoundException;
7
8
class Files
9
{
10
    /**
11
     * The Http Client.
12
     *
13
     * @var \Mozammil\Putio\Http\Client
14
     */
15
    protected $client;
16
17
    public function __construct(Client $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Get a list of files/folders within a parent folder.
24
     *
25
     * @param  int $parent_id
26
     *
27
     * @throws \GuzzleHttp\Exception\GuzzleException
28
     *
29
     * @see https://api.put.io/v2/docs/files.html#list
30
     *
31
     * @return string
32
     */
33
    public function list(int $parent_id = 0)
34
    {
35
        return $this->client->get('files/list', compact('parent_id'));
36
    }
37
38
    /**
39
     * Searches for a list of files/folders matching the keyword,
40
     * path, type and extension.
41
     *
42
     * @param string $keyword
43
     * @param array $from
44
     * @param array $type
45
     * @param array $ext
46
     * @param int $page
47
     *
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     *
50
     * @see https://api.put.io/v2/docs/files.html#search
51
     *
52
     * @return string
53
     */
54
    public function search(string $keyword, array $from = [], array $type = [], array $ext = [], int $page = 1)
55
    {
56
        return $this->client->get(
57
            sprintf('files/search/%s/page/%d', $this->buildSearchQuery($keyword, $from, $type, $ext), $page)
58
        );
59
    }
60
61
    /**
62
     * Uploads a file.
63
     *
64
     * @param string $file
65
     * @param string $filename
66
     * @param int $parent_id
67
     *
68
     * @throws \GuzzleHttp\Exception\GuzzleException
69
     *
70
     * @see https://api.put.io/v2/docs/files.html#upload
71
     *
72
     * @return string
73
     */
74
    public function upload(string $file, string $filename = null, int $parent_id = 0)
75
    {
76
        if (! realpath($file)) {
77
            throw new FileNotFoundException("File {$file} could not be found");
78
        }
79
80
        $uri = sprintf('https://upload.put.io/%s/files/upload?parent_id=%d', $this->client::VERSION, $parent_id);
81
82
        return $this->client->post($uri, [
83
            'multipart' => [
84
                [
85
                    'name' => 'file',
86
                    'filename' => $filename ?: basename($file),
87
                    'contents' => realpath($file),
88
                ],
89
            ],
90
        ]);
91
    }
92
93
    /**
94
     * Creates a folder.
95
     *
96
     * @param string $name
97
     * @param int $parent_id
98
     *
99
     * @throws \GuzzleHttp\Exception\GuzzleException
100
     *
101
     * @see https://api.put.io/v2/docs/files.html#create-folder
102
     *
103
     * @return string
104
     */
105
    public function createFolder(string $name, int $parent_id = 0)
106
    {
107
        return $this->client->post('files/create-folder', [
108
            'form_params' => compact('name', 'parent_id'),
109
        ]);
110
    }
111
112
    /**
113
     * Returns a file’s properties.
114
     *
115
     * @param int $id
116
     *
117
     * @throws \GuzzleHttp\Exception\GuzzleException
118
     *
119
     * @see https://api.put.io/v2/docs/files.html#get
120
     *
121
     * @return string
122
     */
123
    public function get(int $id)
124
    {
125
        return $this->client->get(sprintf('files/%s', $id));
126
    }
127
128
    /**
129
     * Deletes a list of files/folders with the specified ids.
130
     *
131
     * @param  array $file_ids
132
     *
133
     * @throws \GuzzleHttp\Exception\GuzzleException
134
     *
135
     * @see https://api.put.io/v2/docs/files.html#delete
136
     *
137
     * @return string
138
     */
139 View Code Duplication
    public function delete(array $file_ids = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $file_ids = implode(',', $file_ids);
142
143
        return $this->client->post('files/delete', [
144
            'form_params' => compact('file_ids')
145
        ]);
146
    }
147
148
    /**
149
     * Renames given file/folder.
150
     *
151
     * @param int $file_id
152
     * @param string $name
153
     *
154
     * @throws \GuzzleHttp\Exception\GuzzleException
155
     *
156
     * @see https://api.put.io/v2/docs/files.html#rename
157
     *
158
     * @return string
159
     */
160
    public function rename(int $file_id, string $name)
161
    {
162
        return $this->client->post('files/rename', [
163
            'form_params' => compact('file_id', 'name')
164
        ]);
165
    }
166
167
    /**
168
     * Moves given file/folder.
169
     *
170
     * @param array $file_ids
171
     * @param int $parent_id
172
     *
173
     * @throws \GuzzleHttp\Exception\GuzzleException
174
     *
175
     * @see https://api.put.io/v2/docs/files.html#move
176
     *
177
     * @return string
178
     */
179
    public function move(array $file_ids = [], int $parent_id = 0)
180
    {
181
        $file_ids = implode(',', $file_ids);
182
183
        return $this->client->post('files/move', [
184
            'form_params' => compact('file_ids', 'parent_id')
185
        ]);
186
    }
187
188
    /**
189
     * Converts given file to mp4.
190
     *
191
     * @param int $id
192
     *
193
     * @throws \GuzzleHttp\Exception\GuzzleException
194
     *
195
     * @see https://api.put.io/v2/docs/files.html#convert-to-mp4
196
     *
197
     * @return string
198
     */
199
    public function convertToMp4(int $id)
200
    {
201
        return $this->client->post(sprintf('files/%d/mp4', $id));
202
    }
203
204
    /**
205
     * Returns the status of mp4 conversion of the given file.
206
     *
207
     * @param int $id
208
     *
209
     * @throws \GuzzleHttp\Exception\GuzzleException
210
     *
211
     * @see https://api.put.io/v2/docs/files.html#get-mp4-status
212
     *
213
     * @return string
214
     */
215
    public function getMp4Status(int $id)
216
    {
217
        return $this->client->get(sprintf('files/%d/mp4', $id));
218
    }
219
220
    /**
221
     * Donwloads a file from put.io locally.
222
     *
223
     * @param int $id
224
     * @param string $path
225
     *
226
     * @throws \GuzzleHttp\Exception\GuzzleException
227
     *
228
     * @see https://api.put.io/v2/docs/files.html#download
229
     *
230
     * @return string
231
     */
232
    public function download(int $id, string $path, string $filename = null)
233
    {
234
        if (! $filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
235
            $response = $this->get($id);
236
            $filename = json_decode($response, true)['file']['name'];
237
        }
238
239
        $response = $this->client->get(sprintf('files/%d/url', $id));
240
        $download = json_decode($response, true)['url'];
241
242
        if (! $download) {
243
            throw new FileNotFoundException('This file could not be found on the server');
244
        }
245
246
        return $this->client->get($download, [], [
247
            'sink' => "{$path}/{$filename}",
248
            'allow_redirects' => false,
249
        ]);
250
    }
251
252
    /**
253
     * Share given files with friends.
254
     *
255
     * @param array $file_ids
256
     * @param array $friends
257
     *
258
     * @throws \GuzzleHttp\Exception\GuzzleException
259
     *
260
     * @see https://api.put.io/v2/docs/files.html#sharing
261
     *
262
     * @return string
263
     */
264 View Code Duplication
    public function share(array $file_ids = [], array $friends = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
    {
266
        return $this->client->post('files/share', [
267
            'form_params' => [
268
                'file_ids' => implode(',', $file_ids),
269
                'friends' => implode(',', $friends),
270
            ],
271
        ]);
272
    }
273
274
    /**
275
     * Returns list of shared files and share information.
276
     *
277
     * @throws \GuzzleHttp\Exception\GuzzleException
278
     *
279
     * @see https://api.put.io/v2/docs/files.html#get--files-shared
280
     *
281
     * @return string
282
     */
283
    public function shared()
284
    {
285
        return $this->client->get('files/shared');
286
    }
287
288
    /**
289
     * Returns list of users file is shared with.
290
     * Each result item contains a share id which can be used for unsharing.
291
     *
292
     * @param int $id
293
     *
294
     * @throws \GuzzleHttp\Exception\GuzzleException
295
     *
296
     * @see https://api.put.io/v2/docs/files.html#get--files--id--shared-with
297
     *
298
     * @return string
299
     */
300
    public function sharedWith(int $id)
301
    {
302
        return $this->client->get(sprintf('files/%d/shared-with', $id));
303
    }
304
305
    /**
306
     * Unshares given file from given friends or from everyone.
307
     *
308
     * @param int $id
309
     * @param array $friends
310
     *
311
     * @throws \GuzzleHttp\Exception\GuzzleException
312
     *
313
     * @see https://api.put.io/v2/docs/files.html#post--files--id--unshare
314
     *
315
     * @return string
316
     */
317 View Code Duplication
    public function unshare(int $id, array $friends = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
318
    {
319
        return $this->client->post(sprintf('files/%d/unshare', $id), [
320
            'form_params' => [
321
                'shares' => count($friends) ? implode(',', $friends) : 'everyone',
322
            ],
323
        ]);
324
    }
325
326
    /**
327
     * Lists available subtitles for user’s preferred language.
328
     * User must select “Default Subtitle Language” from settings page.
329
     *
330
     * @param int $id
331
     *
332
     * @throws \GuzzleHttp\Exception\GuzzleException
333
     *
334
     * @see https://api.put.io/v2/docs/files.html#subtitles
335
     *
336
     * @return string
337
     */
338
    public function subtitles(int $id)
339
    {
340
        return $this->client->get(sprintf('files/%d/subtitles', $id));
341
    }
342
343
    /**
344
     * Downloads a subtitle.
345
     *
346
     * @param int $id
347
     * @param string $format
348
     * @param string $path
349
     * @param string $filename
350
     * @param string $key
351
     *
352
     * @throws \GuzzleHttp\Exception\GuzzleException
353
     *
354
     * @see https://api.put.io/v2/docs/files.html#download-subtitle
355
     *
356
     * @return string
357
     */
358 View Code Duplication
    public function downloadSubtitle(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
359
        int $id,
360
        string $format,
361
        string $path,
362
        string $filename = null,
363
        string $key = 'default'
364
    ) {
365
        if (! $filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
366
            $response = $this->get($id);
367
            $filename = pathinfo(json_decode($response, true)['file']['name'], PATHINFO_FILENAME).".{$format}";
368
        }
369
370
        return $this->client->get(
371
            sprintf('files/%d/subtitles/%s', $id, $key),
372
            ['format' => $format],
373
            [
374
                'sink' => "{$path}/{$filename}",
375
                'allow_redirects' => false,
376
            ]
377
        );
378
    }
379
380
    /**
381
     * Serves a HLS playlist for a video file.
382
     *
383
     * @param int $id
384
     * @param string $path
385
     * @param string $filename
386
     * @param string $subtitle_key
387
     *
388
     * @throws \GuzzleHttp\Exception\GuzzleException
389
     *
390
     * @see https://api.put.io/v2/docs/files.html#hls-playlist
391
     *
392
     * @return string
393
     */
394 View Code Duplication
    public function hlsPlaylist(int $id, string $path, string $filename = null, string $subtitle_key = 'all')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
395
    {
396
        if (! $filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
397
            $response = $this->get($id);
398
            $filename = pathinfo(json_decode($response, true)['file']['name'], PATHINFO_FILENAME);
399
        }
400
401
        return $this->client->get(
402
            sprintf('files/%d/hls/media.m3u8', $id),
403
            ['subtitle_key' => $subtitle_key],
404
            [
405
                'sink' => "{$path}/{$filename}.m3u8",
406
                'allow_redirects' => false,
407
            ]
408
        );
409
    }
410
411
    /**
412
     * List of dashboard events. Includes download and share events.
413
     *
414
     * @throws \GuzzleHttp\Exception\GuzzleException
415
     *
416
     * @see https://api.put.io/v2/docs/files.html#get--events-list
417
     *
418
     * @return string
419
     */
420
    public function events()
421
    {
422
        return $this->client->get('events/list');
423
    }
424
425
    /**
426
     * Clear all dashboard events.
427
     * User’s home screen (dashboard) which uses same data will also be cleared at Put.io website.
428
     *
429
     * @throws \GuzzleHttp\Exception\GuzzleException
430
     *
431
     * @see https://api.put.io/v2/docs/files.html#post--events-delete
432
     *
433
     * @return string
434
     */
435
    public function deleteEvents()
436
    {
437
        return $this->client->post('events/delete');
438
    }
439
440
    /**
441
     * Sets default video position for a video file.
442
     *
443
     * @param int $id
444
     * @param int $time in seconds
445
     *
446
     * @throws \GuzzleHttp\Exception\GuzzleException
447
     *
448
     * @see https://api.put.io/v2/docs/files.html#set-video-position
449
     *
450
     * @return string
451
     */
452
    public function setVideoPosition(int $id, int $time)
453
    {
454
        return $this->client->post(sprintf('files/%d/start-from', $id), [
455
            'form_params' => compact('time')
456
        ]);
457
    }
458
459
    /**
460
     * Delete video position for a video file.
461
     *
462
     * @param int $id
463
     *
464
     * @throws \GuzzleHttp\Exception\GuzzleException
465
     *
466
     * @see https://api.put.io/v2/docs/files.html#delete-video-position
467
     *
468
     * @return string
469
     */
470
    public function deleteVideoPosition(int $id)
471
    {
472
        return $this->client->post(sprintf('files/%d/start-from/delete', $id));
473
    }
474
475
    /**
476
     * Builds a search query.
477
     *
478
     * @param string $keyword
479
     * @param array $from
480
     * @param array $type
481
     * @param array $ext
482
     *
483
     * @see https://api.put.io/v2/docs/files.html#search
484
     *
485
     * @return string
486
     */
487
    private function buildSearchQuery(string $keyword, array $from = [], array $type = [], array $ext = [])
488
    {
489
        $query = '/'.rawurlencode($keyword);
490
491
        if (count($from)) {
492
            $query .= ' from: "'.implode(',', $from).'"';
493
        }
494
495
        if (count($type)) {
496
            $query .= ' type:'.implode(',', $type);
497
        }
498
499
        if (count($ext)) {
500
            $query .= ' ext:'.implode(',', $ext);
501
        }
502
503
        return $query;
504
    }
505
}
506