Passed
Push — master ( e6f852...fe1bfe )
by Giuliano
09:42
created

FolderReader::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Maestriam\FileSystem\Foundation;
4
5
abstract class FolderReader
6
{
7
    static private array $result = [];
8
9
    static public function read(string $path, int $level) : array
10
    {        
11
        if (! is_dir($path)) return [];
12
        
13
        self::scan($path, 1, $level);
14
        
15
        $result = self::result();
16
        
17
        self::reset();
18
19
        return $result;
20
    }
21
22
    static private function scan(string $path, int $step, int $level) : void
23
    {
24
        $folder = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $folder is dead and can be removed.
Loading history...
25
        $items  = array_diff(scandir($path), array('.', '..'));        
26
        
27
        foreach($items as $item) {
28
29
            $folder = "$path/$item"; 
30
31
            if (! is_dir($folder)) {
32
                continue;
33
            }
34
            
35
            if (is_dir($folder) && $step < $level) {
36
                self::scan($folder, $step+1, $level);
37
            }
38
39
            if ($step == $level) {
40
                self::add($folder, $level);
41
            }
42
        }
43
    }
44
    
45
    static private function add(string $path, int $level) : void
46
    {
47
        $folder = self::prepare($path, $level);
48
        
49
        array_push(self::$result, $folder);
50
    }
51
52
    static private function prepare(string $path, int $level) : string
53
    {
54
        $path = str_replace(DS, '/', $path);
55
56
        $pieces = explode('/', $path);
57
        $pieces = array_splice($pieces, - $level);
58
59
        return implode('/', $pieces);
60
    }
61
62
    static private function result() : array
63
    {
64
        return self::$result;
65
    }
66
67
    static private function reset()
68
    {
69
        self::$result = [];
70
    }
71
}