Passed
Push — master ( 8e077c...e6cc46 )
by Amin
03:50
created

CloudManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 15
c 1
b 1
f 0
dl 0
loc 47
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transfer() 0 7 5
A download() 0 6 2
A uploadDirectory() 0 12 4
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
13
namespace Streaming\Clouds;
14
15
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\FileManager;
18
19
class CloudManager
20
{
21
    /**
22
     * @param array $clouds
23
     * @param string $tmp_dir
24
     */
25
    public static function uploadDirectory(?array $clouds, ?string $tmp_dir): void
26
    {
27
        if (!$clouds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $clouds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
28
            return;
29
        }
30
31
        if (!is_array(current($clouds))) {
32
            $clouds = [$clouds];
33
        }
34
35
        foreach ($clouds as $cloud) {
36
            static::transfer($cloud, __FUNCTION__, $tmp_dir);
37
        }
38
    }
39
40
    /**
41
     * @param array $cloud
42
     * @param string|null $save_to
43
     * @return array
44
     * @throws \Streaming\Exception\Exception
45
     */
46
    public static function download(array $cloud, string $save_to = null): array
47
    {
48
        list($save_to, $is_tmp) = $save_to ? [$save_to, false] : [FileManager::tmpFile(), true];
49
        static::transfer($cloud, __FUNCTION__, $save_to);
50
51
        return [$save_to, $is_tmp];
52
    }
53
54
    /**
55
     * @param $cloud
56
     * @param $method
57
     * @param $path
58
     */
59
    private static function transfer($cloud, $method, $path): void
60
    {
61
        if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
62
            $options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
63
            call_user_func_array([$cloud['cloud'], $method], [$path, $options]);
64
        } else {
65
            throw new InvalidArgumentException('You must pass an array of clouds to the save method. 
66
                and the cloud must be instance of CloudInterface');
67
        }
68
    }
69
}