Completed
Push — master ( 6e390f...5a6d30 )
by Dmitry
07:10 queued 04:24
created

Filesystem   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 89
ccs 46
cts 46
cp 1
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 22 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 15
    public function __construct(Application $app, $root)
12
    {
13 15
        $this->app = $app;
14 15
        $this->root = $root;
15 15
    }
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 15
    public function getPath()
24
    {
25 15
        if (func_get_args()) {
26 15
            $chain = func_get_args();
27 15
            array_unshift($chain, $this->root);
28 15
            foreach ($chain as $k => $v) {
29 15
                if (!strlen($v)) {
30 1
                    unset($chain[$k]);
31
                }
32
            }
33 15
            return implode(DIRECTORY_SEPARATOR, array_values($chain));
34
        }
35
36 15
        return $this->root;
37
    }
38
39 15
    public function listClasses($namespace = '', $location = 'php')
40
    {
41 15
        if ($namespace) {
42 15
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
43
        }
44
45 15
        $files = $this->listFiles($location);
46 15
        $classes = [];
47
48 15
        $namespace = $this->completeClassName($namespace);
49
50 15
        foreach ($files as $file) {
51 15
            $class = str_replace(['\\', '/'], '\\', $file);
52 15
            $class = substr($class, 0, -4);
53 15
            if ($namespace) {
54 15
                $class = $namespace.'\\'.$class;
55
            }
56 15
            $classes[] = $class;
57
        }
58
59 15
        return $classes;
60
    }
61
62 15
    public function listFiles($location)
63
    {
64 15
        $absolute = $this->getPath($location);
65 15
        if (!is_dir($absolute)) {
66 1
            return [];
67
        }
68
69 15
        $result = [];
70 15
        $relative = substr($absolute, strlen($this->getPath()));
71 15
        foreach (scandir($absolute) as $file) {
72 15
            if ($file != '.' && $file != '..') {
73 15
                if (is_file("$absolute/$file")) {
74 15
                    $result[] = $file;
75
                } else {
76 8
                    foreach ($this->listFiles("$relative/$file") as $child) {
77 8
                        $result[] = "$file/$child";
78
                    }
79
                }
80
            }
81
        }
82
83 15
        return $result;
84
    }
85
86 15
    public function completeClassName($classname)
87
    {
88 15
        if ($this->namespace && $classname) {
89 8
            return $this->namespace.'\\'.$classname;
90
        }
91 15
        return $classname;
92
    }
93
}
94