Directory::create()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Hyperized\Benchmark\Generic;
5
6
7
/**
8
 * Class Directory
9
 * @package Hyperized\Benchmark\Generic
10
 */
11
class Directory
12
{
13
    /**
14
     * @var string
15
     */
16
    private static $rootPath = '.';
17
    /**
18
     * @var string
19
     */
20
    private static $parentPath = '..';
21
22
    /**
23
     * @param $path
24
     *
25
     * http://php.net/manual/en/function.rmdir.php#119949
26
     */
27
    public static function removeRecursively($path): void
28
    {
29
        if (\file_exists($path)) {
30
            $dir = \opendir($path);
31
            if (\is_resource($dir)) {
32
                while (false !== ($file = \readdir($dir))) {
33
                    if (($file !== self::$rootPath) && ($file !== self::$parentPath)) {
34
                        $full = $path . DIRECTORY_SEPARATOR . $file;
35
                        if (\is_dir($full)) {
36
                            self::removeRecursively($full);
37
                        } else {
38
                            \unlink($full);
39
                        }
40
                    }
41
                }
42
                \closedir($dir);
43
            }
44
            \rmdir($path);
45
        }
46
    }
47
48
    /**
49
     * @param $path
50
     * @param $permissions
51
     *
52
     * @return bool
53
     * @throws \Exception
54
     */
55
    public static function create($path, $permissions = 0755): bool
56
    {
57
        if (!\file_exists($path) && !mkdir($path, $permissions) && !is_dir($path)) {
58
            throw new \RuntimeException('Could not create directory: ' . $path);
59
        }
60
        return true;
61
    }
62
}