Passed
Push — master ( 0ad23c...4c970b )
by Amin
03:26
created

CloudManager::saveToClouds()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 12
nc 13
nop 2
dl 0
loc 20
rs 8.4444
c 1
b 1
f 0
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 saveToClouds(?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
        sleep(1);
36
37
        foreach ($clouds as $cloud) {
38
            if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
39
                $cloud_obj = $cloud['cloud'];
40
                $options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
41
42
                $cloud_obj->uploadDirectory($tmp_dir, $options);
0 ignored issues
show
Bug introduced by
It seems like $tmp_dir can also be of type null; however, parameter $dir of Streaming\Clouds\CloudInterface::uploadDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                $cloud_obj->uploadDirectory(/** @scrutinizer ignore-type */ $tmp_dir, $options);
Loading history...
43
            } else {
44
                throw new InvalidArgumentException('You must pass an array of clouds to the save method. 
45
                and the cloud must be instance of CloudInterface');
46
            }
47
        }
48
49
    }
50
51
    /**
52
     * @param array $cloud
53
     * @param string|null $save_to
54
     * @return array
55
     * @throws \Streaming\Exception\Exception
56
     */
57
    public static function downloadFromCloud(array $cloud, string $save_to = null): array
58
    {
59
        list($is_tmp, $save_to) = static::isTmp($save_to);
60
61
        if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
62
            $cloud_obj = $cloud['cloud'];
63
            $options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
64
65
            $cloud_obj->download($save_to, $options);
66
        } else {
67
            throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method. 
68
                    and the cloud must be instance of CloudInterface');
69
        }
70
71
        return [$save_to, $is_tmp];
72
    }
73
74
    /**
75
     * @param $path
76
     * @return array
77
     * @throws \Streaming\Exception\Exception
78
     */
79
    private static function isTmp($path)
80
    {
81
        $is_tmp = false;
82
83
        if (null === $path) {
84
            $is_tmp = true;
85
            $path = FileManager::tmpFile();
86
        }
87
88
        return [$is_tmp, $path];
89
    }
90
91
}