Completed
Push — master ( a0b3b9...8a68c3 )
by Andreas
04:01
created

FileFinder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
cbo 2
dl 0
loc 114
ccs 33
cts 33
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addFilter() 0 6 1
A addDirectory() 0 6 1
A setFileList() 0 6 1
A getFileList() 0 7 2
B find() 0 18 5
A filter() 0 9 3
1
<?php
2
/**
3
 * Copyright (c)2014-2014 heiglandreas
4
 * 
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 * 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category 
24
 * @author    Andreas Heigl<[email protected]>
25
 * @copyright ©2014-2014 Andreas Heigl
26
 * @license   http://www.opesource.org/licenses/mit-license.php MIT-License
27
 * @version   0.0
28
 * @since     05.11.14
29
 * @link      https://github.com/heiglandreas/
30
 */
31
32
namespace Org_Heigl\FileFinder;
33
34
35
class FileFinder 
36
{
37
38
    /**
39
     * @var array $filterlist
40
     */
41
    protected $filterlist = array();
42
43
    /**
44
     * @var array $searchLocation
45
     */
46
    protected $searchLocations = array();
47
48
    /**
49
     * @var FileListInterface $fileList
50
     */
51
    protected $fileList = null;
52
53
    /**
54
     * Add a further filter
55
     *
56
     * @param FilterInterface $filter
57
     *
58
     * @return self
59
     */
60 4
    public function addFilter(FilterInterface $filter)
61
    {
62 4
        $this->filterlist[] = $filter;
63
64 4
        return $this;
65
    }
66
67
    /**
68
     * Add a further directory to search for files in
69
     *
70
     * @param string $dir
71
     *
72
     * @return self
73
     */
74 4
    public function addDirectory($dir)
75
    {
76 4
        $this->searchLocations[] = $dir;
77
78 4
        return $this;
79
    }
80
81
    /**
82
     * Set the file list
83
     *
84
     * @param FileListInterface $fileList
85
     *
86
     * @return self
87
     */
88 3
    public function setFileList(FileListInterface $fileList)
89
    {
90 3
        $this->fileList = $fileList;
91
92 3
        return $this;
93
    }
94
95
    /**
96
     * Get the file list
97
     *
98
     * @return FileListInterface
99
     */
100 4
    public function getFileList()
101
    {
102 4
        if (! $this->fileList instanceof FileListInterface) {
103 2
            $this->fileList = new FileList();
104 2
        }
105 4
        return $this->fileList;
106
    }
107
108
    /**
109
     * Do the actual searching
110
     *
111
     * @return FileListInterface[]
112
     */
113 3
    public function find()
114
    {
115 3
        $this->getFileList()->clear();
116 3
        foreach ($this->searchLocations as $location) {
117 3
            if (! is_dir($location)) {
118 1
                continue;
119
            }
120 3
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($location));
121 3
            foreach ($iterator as $file) {
122 3
                if (! $this->filter($file)) {
123 3
                    continue;
124
                }
125 3
                $this->getFileList()->add($file);
126 3
            }
127 3
        }
128
129 3
        return $this->getFileList();
130
    }
131
132
    /**
133
     * Filter the file
134
     *
135
     * @param SPLFileInfo $file
136
     *
137
     * @return bool
138
     */
139 3
    public function filter(\SPLFileInfo $file)
140
    {
141 3
        foreach ($this->filterlist as $filter) {
142 3
            if (! $filter->filter($file)) {
143 3
                return false;
144
            }
145 3
        }
146 3
        return true;
147
    }
148
}
149