Completed
Push — master ( 717b1b...de6dfe )
by Dmitry
06:37
created

Filesystem   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 92
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

6 Methods

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