Passed
Push — master ( fec7a7...e6f852 )
by Giuliano
03:19
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 $list = [];
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
        return self::$list;
16
    }
17
18
    static private function scan(string $path, int $step, int $level) : void
19
    {
20
        $folder = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $folder is dead and can be removed.
Loading history...
21
        $items  = array_diff(scandir($path), array('.', '..'));        
22
        
23
        foreach($items as $item) {
24
25
            $folder = "$path/$item"; 
26
27
            if (! is_dir($folder)) {
28
                continue;
29
            }
30
            
31
            if (is_dir($folder) && $step < $level) {
32
                self::scan($folder, $step+1, $level);
33
            }
34
35
            if ($step == $level) {
36
                self::add($folder, $level);
37
            }
38
        }
39
    }
40
    
41
    static private function add(string $path, int $level) : void
42
    {
43
        $folder = self::prepare($path, $level);
44
        
45
        array_push(self::$list, $folder);
46
    }
47
48
    static private function prepare(string $path, int $level) : string
49
    {
50
        $path = str_replace(DS, '/', $path);
51
52
        $pieces = explode('/', $path);
53
        $pieces = array_splice($pieces, - $level);
54
55
        return implode('/', $pieces);
56
    }
57
}