InteractsWithDirectories   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A needsParentDirectory() 0 3 1
A needsDirectory() 0 4 2
A needsDirectories() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Concerns;
6
7
use Hyde\Facades\Filesystem;
8
9
use function dirname;
10
11
trait InteractsWithDirectories
12
{
13
    /**
14
     * Ensure the supplied directory exist by creating it if it does not.
15
     *
16
     * @param  string  $directory  relative file path to the directory
17
     */
18
    protected static function needsDirectory(string $directory): void
19
    {
20
        if (! Filesystem::exists($directory)) {
21
            Filesystem::makeDirectory($directory, recursive: true);
22
        }
23
    }
24
25
    /**
26
     * Ensure the supplied directories exist by creating them if they don't.
27
     *
28
     * @param  array<string>  $directories  array with relative file paths to the directories
29
     */
30
    protected static function needsDirectories(array $directories): void
31
    {
32
        foreach ($directories as $directory) {
33
            static::needsDirectory($directory);
34
        }
35
    }
36
37
    /**
38
     * Ensure the supplied file's parent directory exists by creating it if it does not.
39
     */
40
    protected static function needsParentDirectory(string $file, int $levels = 1): void
41
    {
42
        static::needsDirectory(dirname($file, $levels));
43
    }
44
}
45