FileSystem   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rmdir() 0 14 6
A mkdir() 0 15 5
1
<?php
2
namespace TheCodingMachine\Funky\Utils;
3
4
use TheCodingMachine\Funky\IoException;
5
6
class FileSystem
7
{
8
    public static function mkdir(string $dir, int $mode = 0777): void
9
    {
10
        if (is_dir($dir)) {
11
            return;
12
        }
13
14
        if (true !== @mkdir($dir, $mode, true)) {
15
            $error = error_get_last();
16
            if (!is_dir($dir)) {
17
                // The directory was not created by a concurrent process.
18
                // Let's throw an exception with a developer friendly error message if we have one
19
                if ($error !== null) {
20
                    throw IoException::cannotCreateDirectory($dir, $error['message']);
21
                }
22
                throw IoException::cannotCreateDirectory($dir, 'unknown error');
23
            }
24
        }
25
    }
26
27
    public static function rmdir(string $dir): void
28
    {
29
        if (is_dir($dir)) {
30
            $objects = scandir($dir);
31
            foreach ($objects as $object) {
32
                if ($object !== "." && $object !== "..") {
33
                    if (is_dir($dir. '/' .$object)) {
34
                        self::rmdir($dir. '/' .$object);
35
                    } else {
36
                        unlink($dir. '/' .$object);
37
                    }
38
                }
39
            }
40
            rmdir($dir);
41
        }
42
    }
43
}
44