ReadDirectory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 119
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIgnore() 0 3 1
A getList() 0 3 1
A itemAction() 0 7 2
A __construct() 0 3 1
A dirAction() 0 4 1
A run() 0 27 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace bultonFr\Utils\Files;
6
7
use Exception;
8
9
/**
10
 * Read a directory and sub-directory, and do the choice action on each
11
 * item in the readed directory.
12
 *
13
 * @author bulton-fr <[email protected]>
14
 */
15
class ReadDirectory
16
{
17
    /**
18
     * Exception code if the opendir function fail
19
     *
20
     * @const EXCEP_RUN_OPENDIR
21
     */
22
    const EXCEP_RUN_OPENDIR = 201001;
23
    
24
    /**
25
     * A list of path. The system not add all path found automaticaly, you need
26
     * to add it in a override of itemAction method.
27
     *
28
     * @var array $list
29
     */
30
    protected $list;
31
32
    /**
33
     * Item to ignore during the reading of directories
34
     *
35
     * @var array $ignore
36
     */
37
    protected $ignore = ['.', '..'];
38
39
    /**
40
     * Constructor
41
     *
42
     * @param array &$listFiles : List of file(s) found
43
     */
44
    public function __construct(array &$listFiles)
45
    {
46 1
        $this->list = &$listFiles;
47 1
    }
48
49
    /**
50
     * Getter accessor to the property list
51
     *
52
     * @return array
53
     */
54
    public function getList(): array
55
    {
56 1
        return $this->list;
57
    }
58
59
    /**
60
     * Getter accessor to the property ignore
61
     *
62
     * @return array
63
     */
64
    public function getIgnore(): array
65
    {
66 1
        return $this->ignore;
67
    }
68
    
69
    /**
70
     * Read all the directories
71
     *
72
     * @param string $path : Path to read
73
     *
74
     * @return void
75
     */
76
    public function run(string $path)
77
    {
78 1
        $dir = opendir($path);
79 1
        if ($dir === false) {
80 1
            throw new Exception(
81
                'The directory can not be open. '
82 1
                .'See php error log for more informations.',
83 1
                self::EXCEP_RUN_OPENDIR
84
            );
85
        }
86
87 1
        while (($file = readdir($dir)) !== false) {
88 1
            $action = $this->itemAction($file, $path);
89
90 1
            if ($action === 'continue') {
91 1
                continue;
92 1
            } elseif ($action === 'break') {
93 1
                break;
94
            }
95
            
96 1
            if (is_dir($path.'/'.$file)) {
97 1
                $this->dirAction($path.'/'.$file);
98 1
                continue;
99
            }
100
        }
101
102 1
        closedir($dir);
103 1
    }
104
105
    /**
106
     * Action to do when an item (file or directory) is found.
107
     *
108
     * @param string $fileName The file's name
109
     * @param string $pathToFile The file's path
110
     *
111
     * @return string
112
     */
113
    protected function itemAction(string $fileName, string $pathToFile): string
114
    {
115 1
        if (in_array($fileName, $this->ignore)) {
116 1
            return 'continue';
117
        }
118
        
119 1
        return '';
120
    }
121
    
122
    /**
123
     * Recall ReadDirectory to read this directory
124
     * This is to avoid having the recursion error
125
     *
126
     * @param string $dirPath
127
     *
128
     * @return void
129
     */
130
    protected function dirAction(string $dirPath)
131
    {
132
        $read = new static($this->list);
133
        $read->run($dirPath);
134
    }
135
}
136