FileFinderBuilder   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 30
c 3
b 0
f 2
lcom 1
cbo 1
dl 0
loc 153
ccs 80
cts 80
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A includeDotFiles() 0 6 1
A isRecursive() 0 10 2
C sortBy() 0 35 12
D withFilters() 0 31 9
A getFinder() 0 4 1
A getIterator() 0 4 1
1
<?php
2
/**
3
 * FileFinderBuilder.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained on one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
18
namespace Pvra\Console\Services;
19
20
21
use RuntimeException;
22
use Symfony\Component\Finder\Finder;
23
24
/**
25
 * Class FileFinderBuilder
26
 *
27
 * @package Pvra\Console\Services
28
 */
29
class FileFinderBuilder implements \IteratorAggregate
30
{
31
    const SORT_BY_NAME = 'n',
32
        SORT_BY_CHANGED_TIME = 'c',
33
        SORT_BY_TYPE = 't',
34
        SORT_BY_ACCESSED_TIME = 'a',
35
        SORT_BY_MODIFIED_TIME = 'm';
36
37
    /**
38
     * @var \Symfony\Component\Finder\Finder
39
     */
40
    private $finder;
41
42
    /**
43
     * @param string $dir The base directory
44
     * @param \Symfony\Component\Finder\Finder $finder The finder instance to be used. If none is given a new will be
45
     *     created
46
     */
47 46
    public function __construct($dir, Finder $finder = null)
48
    {
49 46
        if (!is_dir($dir) || !is_readable($dir)) {
50 2
            throw new RuntimeException(sprintf('"%s" is not a valid directory', $dir));
51
        }
52
53 44
        if ($finder === null) {
54 28
            $this->finder = new Finder();
55 14
        } else {
56 16
            $this->finder = $finder;
57
        }
58 44
        $this->finder->files()
59 44
            ->in($dir)
60 44
            ->ignoreDotFiles(true)
61 44
            ->ignoreVCS(true);
62 44
    }
63
64
    /**
65
     * @param bool $includeDotFiles
66
     * @return $this
67
     */
68 2
    public function includeDotFiles($includeDotFiles = true)
69
    {
70 2
        $this->finder->ignoreDotFiles(!$includeDotFiles);
71
72 2
        return $this;
73
    }
74
75
    /**
76
     * @param bool $isRecursive
77
     * @return $this
78
     */
79 24
    public function isRecursive($isRecursive = true)
80
    {
81 24
        if ($isRecursive === false) {
82 24
            $this->finder->depth(0);
83 12
        } else {
84 2
            $this->finder->depth('>= 0');
85
        }
86
87 24
        return $this;
88
    }
89
90
    /**
91
     * @param string|callable $by
92
     * @return $this
93
     */
94 28
    public function sortBy($by = self::SORT_BY_NAME)
95
    {
96 28
        if (is_callable($by)) {
97 2
            $this->finder->sort($by);
98 2
            return $this;
99
        }
100
101 26
        switch (strtolower($by)) {
102 26
            case self::SORT_BY_NAME:
103 15
            case 'name':
104 24
                $this->finder->sortByName();
105 24
                break;
106 4
            case self::SORT_BY_CHANGED_TIME:
107 4
            case 'ctime':
108 2
                $this->finder->sortByChangedTime();
109 2
                break;
110 4
            case self::SORT_BY_ACCESSED_TIME:
111 4
            case 'atime':
112 2
                $this->finder->sortByAccessedTime();
113 2
                break;
114 4
            case self::SORT_BY_TYPE:
115 4
            case 'type':
116 2
                $this->finder->sortByType();
117 2
                break;
118 4
            case self::SORT_BY_MODIFIED_TIME:
119 4
            case 'mtime':
120 2
                $this->finder->sortByModifiedTime();
121 2
                break;
122 1
            default:
123 3
                throw new \InvalidArgumentException($by . ' is not a supported argument for sorting.');
124 13
        }
125
126
127 24
        return $this;
128
    }
129
130
    /**
131
     * @param array $filters
132
     * @return $this
133
     */
134 26
    public function withFilters(array $filters)
135
    {
136 26
        if (!empty($filters)) {
137 26
            foreach ($filters as $currentFilter) {
138 26
                if (!strpos($currentFilter, ':')) {
139 2
                    throw new \InvalidArgumentException(sprintf('The filter "%s" is not a valid filter. A valid filter has the format <name>:<value>.',
140 1
                        $currentFilter));
141
                }
142
143 24
                $currentFilterElements = explode(':', $currentFilter, 2);
144
145 24
                switch (trim($currentFilterElements[0])) {
146 24
                    case 'exclude':
147 2
                        $this->finder->exclude($currentFilterElements[1]);
148 2
                        break;
149 24
                    case 'name':
150 24
                        $this->finder->name($currentFilterElements[1]);
151 24
                        break;
152 2
                    case 'notName':
153 2
                        $this->finder->notName($currentFilterElements[1]);
154 2
                        break;
155 2
                    case 'path':
156 2
                        $this->finder->path($currentFilterElements[1]);
157 2
                        break;
158 2
                    case 'size':
159 13
                        $this->finder->size($currentFilterElements[1]);
160 12
                }
161 12
            }
162 12
        }
163 24
        return $this;
164
    }
165
166
    /**
167
     * @return \Symfony\Component\Finder\Finder
168
     */
169 26
    public function getFinder()
170
    {
171 26
        return $this->finder;
172
    }
173
174
    /**
175
     * @return \Iterator|\SplFileInfo[]
176
     */
177 2
    public function getIterator()
178
    {
179 2
        return $this->finder->getIterator();
180
    }
181
}
182