Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

Filesystem   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.11%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 88
ccs 41
cts 45
cp 0.9111
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 5 2
A getPath() 0 14 4
B listClasses() 0 22 4
C listFiles() 0 23 7
A completeClassName() 0 7 3
A __construct() 0 5 1
1
<?php
2
3
namespace Basis;
4
5
class Filesystem
6
{
7
    private $app;
8
    protected $root;
9
    protected $namespace;
10
11
    public function __construct(Application $app, $root)
12
    {
13
        $this->app = $app;
14
        $this->root = $root;
15
    }
16
17 6
    public function exists(...$args) : bool
18
    {
19 6
        $path = $this->getPath(...$args);
20 6
        return is_dir($path) || file_exists($path);
21
    }
22
23 45
    public function getPath(...$args) : string
24
    {
25 45
        if (count($args)) {
26 45
            array_unshift($args, $this->root);
27 45
            foreach ($args as $k => $v) {
28 45
                if (!strlen($v)) {
29 45
                    unset($args[$k]);
30
                }
31
            }
32 45
            return implode(DIRECTORY_SEPARATOR, array_values($args));
33
        }
34
35 45
        return $this->root;
36
    }
37
38 45
    public function listClasses(string $namespace = '', string $location = 'php') : array
39
    {
40 45
        if ($namespace) {
41 45
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
42
        }
43
44 45
        $files = $this->listFiles($location);
45 45
        $classes = [];
46
47 45
        $namespace = $this->completeClassName($namespace);
48
49 45
        foreach ($files as $file) {
50 45
            $class = str_replace(['\\', '/'], '\\', $file);
51 45
            $class = substr($class, 0, -4);
52 45
            if ($namespace) {
53 45
                $class = $namespace.'\\'.$class;
54
            }
55 45
            $classes[] = $class;
56
        }
57
58 45
        return $classes;
59
    }
60
61 45
    public function listFiles(...$args) : array
62
    {
63 45
        $absolute = $this->getPath(...$args);
64 45
        if (!is_dir($absolute)) {
65 35
            return [];
66
        }
67
68 45
        $result = [];
69 45
        $relative = substr($absolute, strlen($this->getPath()));
70 45
        foreach (scandir($absolute) as $file) {
71 45
            if ($file != '.' && $file != '..') {
72 45
                if (is_file("$absolute/$file")) {
73 45
                    $result[] = $file;
74
                } else {
75 45
                    foreach ($this->listFiles("$relative/$file") as $child) {
76 45
                        $result[] = "$file/$child";
77
                    }
78
                }
79
            }
80
        }
81
82 45
        return $result;
83
    }
84
85 45
    public function completeClassName(string $classname) : string
86
    {
87 45
        if ($this->namespace && $classname) {
88 45
            return $this->namespace.'\\'.$classname;
89
        }
90 45
        return $classname;
91
    }
92
}
93