Passed
Push — master ( c37170...fb5355 )
by Amin
02:31
created

File::moveDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
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;
14
15
16
use Streaming\Exception\RuntimeException;
17
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
/**
21
 * File constructor.
22
 * It is all about files
23
*/
24
25
class File
26
{
27
    /**
28
     * @param $dirname
29
     * @param int $mode
30
     */
31
    public static function makeDir(string $dirname, int $mode = 0777): void
32
    {
33
        static::filesystem('mkdir', [$dirname, $mode]);
34
    }
35
36
    /**
37
     * @param $dir
38
     * @return int|null
39
     */
40
    public static function directorySize(string $dir): int
41
    {
42
        if (is_dir($dir)) {
43
            $size = 0;
44
            foreach (scandir($dir) as $file) {
45
                if (in_array($file, [".", ".."])) continue;
46
                $filename = $dir . DIRECTORY_SEPARATOR . $file;
47
                $size += is_file($filename) ? filesize($filename) : static::directorySize($filename);
48
            }
49
            return $size;
50
        }
51
52
        return 0;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public static function tmpFile(): string
59
    {
60
        return tempnam(static::tmpDirPath(), 'stream');
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public static function tmpDir(): string
67
    {
68
        static::makeDir($tmp_dir = static::tmpDirPath() . DIRECTORY_SEPARATOR . Utilities::randomString() . DIRECTORY_SEPARATOR);
69
        return $tmp_dir;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    private static function tmpDirPath(): string
76
    {
77
        static::makeDir($tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming");
78
        return $tmp_path;
79
    }
80
81
    /**
82
     * @param string $src
83
     * @param string $dst
84
     */
85
    public static function moveDir(string $src, string $dst): void
86
    {
87
        static::filesystem('mirror', [$src, $dst]);
88
        static::deleteDirectory($src);
89
    }
90
91
    /**
92
     * @param $dir
93
     */
94
    public static function deleteDirectory(string $dir): void
95
    {
96
        static::filesystem('remove', [$dir]);
97
    }
98
99
    /**
100
     * @param string $method
101
     * @param array $params
102
     */
103
    private static function filesystem(string $method, array $params): void
104
    {
105
        try {
106
            \call_user_func_array([new Filesystem, $method], $params);
107
        } catch (IOExceptionInterface $e) {
108
            throw new RuntimeException("Failed action" . $e->getPath(), $e->getCode(), $e);
109
        }
110
    }
111
}