Passed
Push — master ( eb0dbe...5399ee )
by Caen
03:14 queued 11s
created

InteractsWithDirectories::needsDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
/**
6
 * @see \Hyde\Framework\Testing\Unit\InteractsWithDirectoriesConcernTest
7
 */
8
trait InteractsWithDirectories
9
{
10
    /**
11
     * Ensure the supplied directory exist by creating it if it does not.
12
     *
13
     * @param  string  $directory  absolute file path to the directory
14
     */
15
    public static function needsDirectory(string $directory): void
16
    {
17
        if (! file_exists($directory)) {
18
            mkdir($directory, recursive: true);
19
        }
20
    }
21
22
    /**
23
     * Ensure the supplied directories exist by creating them if they don't.
24
     *
25
     * @param  array  $directories  array with absolute file paths to the directories
26
     */
27
    public static function needsDirectories(array $directories): void
28
    {
29
        foreach ($directories as $directory) {
30
            static::needsDirectory($directory);
31
        }
32
    }
33
34
    /**
35
     * Ensure the supplied file's parent directory exists by creating it if it does not.
36
     *
37
     * @param  string  $file
38
     * @param  int  $levels
39
     * @return void
40
     */
41
    public static function needsParentDirectory(string $file, int $levels = 1): void
42
    {
43
        static::needsDirectory(dirname($file, $levels));
44
    }
45
}
46