Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

Enumerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 102
ccs 0
cts 51
cp 0
rs 10
c 3
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enumerate() 0 14 1
D getFilter() 0 39 9
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\FileSystem;
12
13
/**
14
 * Enumerates files in the concrete directory, applying filtration logic
15
 */
16
class Enumerator
17
{
18
    /**
19
     * Path to the root directory, where enumeration should start
20
     *
21
     * @var string
22
     */
23
    private $rootDirectory;
24
25
    /**
26
     * List of additional include paths, should be below rootDirectory
27
     *
28
     * @var array
29
     */
30
    private $includePaths;
31
32
    /**
33
     * List of additional exclude paths, should be below rootDirectory
34
     *
35
     * @var array
36
     */
37
    private $excludePaths;
38
39
    /**
40
     * Initializes an enumerator
41
     *
42
     * @param string $rootDirectory Path to the root directory
43
     * @param array $includePaths List of additional include paths
44
     * @param array $excludePaths List of additional exclude paths
45
     */
46
    public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = [])
47
    {
48
        $this->rootDirectory = $rootDirectory;
49
        $this->includePaths  = $includePaths;
50
        $this->excludePaths  = $excludePaths;
51
    }
52
53
    /**
54
     * Returns an enumerator for files
55
     *
56
     * @return \CallbackFilterIterator|\RecursiveIteratorIterator|\SplFileInfo[]
57
     */
58
    public function enumerate()
59
    {
60
        $iterator = new \RecursiveIteratorIterator(
61
            new \RecursiveDirectoryIterator(
62
                $this->rootDirectory,
63
                \FilesystemIterator::SKIP_DOTS
64
            )
65
        );
66
67
        $callback = $this->getFilter();
68
        $iterator = new \CallbackFilterIterator($iterator, $callback);
69
70
        return $iterator;
71
    }
72
73
    /**
74
     * Returns a filter callback for enumerating files
75
     *
76
     * @return \Closure
77
     */
78
    public function getFilter()
79
    {
80
        $rootDirectory = $this->rootDirectory;
81
        $includePaths  = $this->includePaths;
82
        $excludePaths  = $this->excludePaths;
83
84
        return function(\SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
85
            if ($file->getExtension() !== 'php') {
86
                return false;
87
            };
88
89
            $realPath = $file->getRealPath();
90
            // Do not touch files that not under rootDirectory
91
            if (strpos($realPath, $rootDirectory) !== 0) {
92
                return false;
93
            }
94
95
            if (!empty($includePaths)) {
96
                $found = false;
97
                foreach ($includePaths as $includePath) {
98
                    if (strpos($realPath, $includePath) === 0) {
99
                        $found = true;
100
                        break;
101
                    }
102
                }
103
                if (!$found) {
104
                    return false;
105
                }
106
            }
107
108
            foreach ($excludePaths as $excludePath) {
109
                if (strpos($realPath, $excludePath) === 0) {
110
                    return false;
111
                }
112
            }
113
114
            return true;
115
        };
116
    }
117
}
118