File   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A directorySize() 0 13 5
A makeDir() 0 3 1
A put() 0 8 4
A tmp() 0 16 3
A tmpDirPath() 0 4 1
A move() 0 4 1
A tmpDir() 0 4 1
A cleanTmpFiles() 0 3 1
A copy() 0 3 1
A remove() 0 3 1
A filesystem() 0 6 2
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
     * @param $path
57
     * @param $content
58
     * @param bool $force
59
     * @return void
60
     */
61
    public static function put($path, $content, $force = true): void
62
    {
63
        if (file_exists($path) && !$force) {
64
            throw new RuntimeException("File Already Exists");
65
        }
66
67
        if (false === @file_put_contents($path, $content)) {
68
            throw new RuntimeException("Unable to save the file");
69
        }
70
    }
71
72
    /**
73
     * @param string $prefix
74
     * @return string
75
     */
76
    public static function tmp($prefix = 'pfvs.file_'): string
77
    {
78
        for ($i = 0; $i < 10; ++$i) {
79
            $tmpFile = static::tmpDirPath() . '/' . basename($prefix) . uniqid(mt_rand());
80
            $handle = @fopen($tmpFile, 'x+');
81
82
            if (false === $handle) {
83
                continue;
84
            }
85
86
            @fclose($handle);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fclose(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

86
            /** @scrutinizer ignore-unhandled */ @fclose($handle);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
88
            return $tmpFile;
89
        }
90
91
        throw new RuntimeException("A temporary file could not be created.");
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public static function tmpDir(): string
98
    {
99
        static::makeDir($tmp_dir = static::tmpDirPath() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR);
100
        return $tmp_dir;
101
    }
102
103
    /**
104
     * clear all tmp files
105
     */
106
    public static function cleanTmpFiles(): void
107
    {
108
        static::remove(static::tmpDirPath());
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    private static function tmpDirPath(): string
115
    {
116
        static::makeDir($tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming");
117
        return $tmp_path;
118
    }
119
120
    /**
121
     * @param string $src
122
     * @param string $dst
123
     */
124
    public static function move(string $src, string $dst): void
125
    {
126
        static::filesystem('mirror', [$src, $dst]);
127
        static::remove($src);
128
    }
129
130
    /**
131
     * @param string $src
132
     * @param string $dst
133
     * @param bool $force
134
     */
135
    public static function copy(string $src, string $dst, bool $force = true): void
136
    {
137
        static::filesystem('copy', [$src, $dst, $force]);
138
    }
139
140
    /**
141
     * @param $dir
142
     */
143
    public static function remove(string $dir): void
144
    {
145
        static::filesystem('remove', [$dir]);
146
    }
147
148
    /**
149
     * @param string $method
150
     * @param array $params
151
     * @return mixed
152
     */
153
    private static function filesystem(string $method, array $params)
154
    {
155
        try {
156
            return \call_user_func_array([new Filesystem, $method], $params);
157
        } catch (IOExceptionInterface $e) {
158
            throw new RuntimeException("Failed action" . $e->getPath(), $e->getCode(), $e);
159
        }
160
    }
161
}