Passed
Push — master ( d7ad3b...186424 )
by Pascal
02:42
created

TemporaryDirectories::__construct()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Filesystem\Filesystem;
6
7
class TemporaryDirectories
8
{
9
    /**
10
     * Root of the temporary directories.
11
     *
12
     * @var string
13
     */
14
    private $root;
15
16
    /**
17
     * Array of all directories
18
     *
19
     * @var array
20
     */
21
    private $directories = [];
22
23
    /**
24
     * Sets the root and removes the trailing slash.
25
     *
26
     * @param string $root
27
     */
28
    public function __construct(string $root)
29
    {
30
        $this->root = rtrim($root, '/');
31
    }
32
33
    /**
34
     * Returns the full path a of new temporary directory.
35
     *
36
     * @return string
37
     */
38
    public function create(): string
39
    {
40
        $directory = $this->root . '/' . bin2hex(random_bytes(8));
41
42
        mkdir($directory);
43
44
        return $this->directories[] = $directory;
45
    }
46
47
    /**
48
     * Loop through all directories and delete them.
49
     */
50
    public function deleteAll(): void
51
    {
52
        $filesystem = new Filesystem;
53
54
        foreach ($this->directories as $directory) {
55
            $filesystem->deleteDirectory($directory);
56
        }
57
58
        $this->directories = [];
59
    }
60
}
61