Passed
Push — master ( d8d39b...c46f01 )
by Amin
03:19
created

Helper::downloadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 16
rs 9.9
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
namespace Streaming;
13
14
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\GuzzleException;
17
use Streaming\Exception\Exception;
18
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
class Helper
22
{
23
    /**
24
     * round a number to nearest even number
25
     *
26
     * @param float $number
27
     * @return int
28
     */
29
    public static function roundToEven(float $number): int
30
    {
31
        return (($number = intval($number)) % 2 == 0) ? $number : $number + 1;
32
    }
33
34
    /**
35
     * @param $dirname
36
     * @param int $mode
37
     * @throws Exception
38
     */
39
    public static function makeDir($dirname, $mode = 0777): void
40
    {
41
        $filesystem = new Filesystem();
42
43
        try {
44
            $filesystem->mkdir($dirname, $mode);
45
        } catch (IOExceptionInterface $exception) {
46
            throw new Exception("An error occurred while creating your directory at " . $exception->getPath());
47
        }
48
    }
49
50
    /**
51
     * @param int $length
52
     * @return bool|string
53
     */
54
    public static function randomString($length = 10)
55
    {
56
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
57
        return substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length);
0 ignored issues
show
Bug introduced by
ceil($length / strlen($chars)) of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

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

57
        return substr(str_shuffle(str_repeat($chars, /** @scrutinizer ignore-type */ ceil($length / strlen($chars)))), 1, $length);
Loading history...
58
59
    }
60
61
    /**
62
     * @param $dir
63
     * @return int|null
64
     */
65
    public static function directorySize($dir)
66
    {
67
        if (is_dir($dir)) {
68
            $size = 0;
69
            foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
70
                $size += is_file($each) ? filesize($each) : static::directorySize($each);
71
            }
72
            return $size;
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * @param string $ext
80
     * @return string
81
     * @throws Exception
82
     */
83
    public static function tmpFile(string $ext = ""): string
84
    {
85
        if ("" !== $ext) {
86
            $ext = "." . $ext;
87
        }
88
89
        $tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming";
90
        static::makeDir($tmp_path);
91
92
        return $tmp_path . DIRECTORY_SEPARATOR . static::randomString() . $ext;
93
    }
94
95
    /**
96
     * @return string
97
     * @throws Exception
98
     */
99
    public static function tmpDir(): string
100
    {
101
        return static::tmpFile() . DIRECTORY_SEPARATOR;
102
    }
103
104
105
    public static function moveDir(string $source, string $destination)
106
    {
107
        foreach (scandir($source) as $file) {
108
            if (in_array($file, [".", ".."])) continue;
109
            if (copy($source . $file, $destination . $file)) {
110
                unlink($source . $file);
111
            }
112
        }
113
    }
114
115
    /**
116
     * @param $dir
117
     * @return bool
118
     */
119
    public static function deleteDirectory($dir)
120
    {
121
        if (!file_exists($dir)) {
122
            return true;
123
        }
124
125
        if (!is_dir($dir)) {
126
            return @unlink($dir);
127
        }
128
129
        foreach (scandir($dir) as $item) {
130
            if (in_array($item, [".", ".."])) continue;
131
            if (!static::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
132
                return false;
133
            }
134
        }
135
136
        return @rmdir($dir);
137
    }
138
}