TemporaryDirectories   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deleteAll() 0 9 2
A create() 0 7 1
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, 0777, true);
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