Passed
Push — master ( 7d6389...7f8ede )
by Amin
03:20
created

File::tmp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 tmp(): 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 . uniqid() . DIRECTORY_SEPARATOR);
69
        return $tmp_dir;
70
    }
71
72
    /**
73
     * clear all tmp files
74
     */
75
    public static function cleanTmpFiles(): void
76
    {
77
        static::remove(static::tmpDirPath());
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    private static function tmpDirPath(): string
84
    {
85
        static::makeDir($tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming");
86
        return $tmp_path;
87
    }
88
89
    /**
90
     * @param string $src
91
     * @param string $dst
92
     */
93
    public static function move(string $src, string $dst): void
94
    {
95
        static::filesystem('mirror', [$src, $dst]);
96
        static::remove($src);
97
    }
98
99
    /**
100
     * @param $dir
101
     */
102
    public static function remove(string $dir): void
103
    {
104
        static::filesystem('remove', [$dir]);
105
    }
106
107
    /**
108
     * @param string $method
109
     * @param array $params
110
     */
111
    private static function filesystem(string $method, array $params): void
112
    {
113
        try {
114
            \call_user_func_array([new Filesystem, $method], $params);
115
        } catch (IOExceptionInterface $e) {
116
            throw new RuntimeException("Failed action" . $e->getPath(), $e->getCode(), $e);
117
        }
118
    }
119
}