Completed
Push — master ( 2e21dc...b663e1 )
by Bill
04:37 queued 02:23
created

FileCollector::from()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Bmitch\Envsync\Collectors;
4
5
class FileCollector
6
{
7
    /**
8
     * Regular expression pattern used to find files.
9
     * @var string
10
     */
11
    protected $filePattern;
12
13
    /**
14
     * Sets the regex pattern to use to find files.
15
     * @param  string $regex Regular Expression.
16
     * @return self
17
     */
18
    public function get($regex)
19
    {
20
        $this->filePattern = $regex;
21
        return $this;
22
    }
23
24
    /**
25
     * Looks in the provided $folder for files that match
26
     * $this->filePattern and returns them.
27
     * @param  string $folder Folder path.
28
     * @return array
29
     */
30
    public function from($folder)
31
    {
32
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder));
33
        $files = [];
34
35
        foreach ($iterator as $file) {
36
            if ($file->isDir()) {
37
                continue;
38
            }
39
40
            if (! preg_match("/{$this->filePattern}/", $file->getFilename())) {
41
                continue;
42
            }
43
44
            $files[] = $file->getPathname();
45
        }
46
47
        return $files;
48
    }
49
}
50