Completed
Push — master ( baa637...9b813d )
by Amin
04:08 queued 11s
created

FileManager::makeDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
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\Exception;
17
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
/**
21
 * FileManager constructor.
22
 * It is all about files
23
*/
24
25
class FileManager
26
{
27
    /**
28
     * @param $dirname
29
     * @param int $mode
30
     * @throws Exception
31
     */
32
    public static function makeDir($dirname, $mode = 0777): void
33
    {
34
        $filesystem = new Filesystem();
35
36
        try {
37
            $filesystem->mkdir($dirname, $mode);
38
        } catch (IOExceptionInterface $exception) {
39
            throw new Exception("An error occurred while creating your directory at " . $exception->getPath(), $exception->getCode(), $exception);
40
        }
41
    }
42
43
    /**
44
     * @param $dir
45
     * @return int|null
46
     */
47
    public static function directorySize($dir)
48
    {
49
        if (is_dir($dir)) {
50
            $size = 0;
51
            foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
52
                $size += is_file($each) ? filesize($each) : static::directorySize($each);
53
            }
54
            return $size;
55
        }
56
57
        return null;
58
    }
59
60
    /**
61
     * @return string
62
     * @throws Exception
63
     */
64
    public static function tmpFile(): string
65
    {
66
        return tempnam(static::tmpDirPath(), 'stream');
67
    }
68
69
    /**
70
     * @return string
71
     * @throws Exception
72
     */
73
    public static function tmpDir(): string
74
    {
75
        $tmp_dir = static::tmpDirPath() . DIRECTORY_SEPARATOR . Helper::randomString() . DIRECTORY_SEPARATOR;
76
        static::makeDir($tmp_dir);
77
78
        return $tmp_dir;
79
    }
80
81
    /**
82
     * @return string
83
     * @throws Exception
84
     */
85
    private static function tmpDirPath(): string
86
    {
87
        $tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming";
88
        static::makeDir($tmp_path);
89
90
        return $tmp_path;
91
    }
92
93
    /**
94
     * @param string $source
95
     * @param string $destination
96
     * @throws Exception
97
     */
98
    public static function moveDir(string $source, string $destination)
99
    {
100
        static::makeDir($destination);
101
        foreach (scandir($source) as $file) {
102
            if (in_array($file, [".", ".."])) continue;
103
            if (copy($source . $file, $destination . $file)) {
104
                unlink($source . $file);
105
            }
106
        }
107
108
        static::deleteDirectory($source);
109
    }
110
111
    /**
112
     * @param $dir
113
     * @return bool
114
     */
115
    public static function deleteDirectory($dir)
116
    {
117
        if (!file_exists($dir)) {
118
            return true;
119
        }
120
121
        if (!is_dir($dir)) {
122
            return @unlink($dir);
123
        }
124
125
        foreach (scandir($dir) as $item) {
126
            if (in_array($item, [".", ".."])) continue;
127
            if (!static::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
128
                return false;
129
            }
130
        }
131
132
        return @rmdir($dir);
133
    }
134
}