1 | <?php |
||
13 | class FindDirectoryService |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $pattern; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $directory; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $extension = ""; |
||
30 | |||
31 | /** |
||
32 | * @var FinderInterface |
||
33 | */ |
||
34 | private $finder; |
||
35 | |||
36 | /** |
||
37 | * @var ListerInterface |
||
38 | */ |
||
39 | private $lister; |
||
40 | |||
41 | /** |
||
42 | * FindDirectoryService constructor. |
||
43 | * @param FinderInterface $finder |
||
44 | * @param ListerInterface $lister |
||
45 | */ |
||
46 | 11 | public function __construct( |
|
53 | |||
54 | /** |
||
55 | * FindDirectory find |
||
56 | * @return array|bool |
||
57 | */ |
||
58 | 11 | public function find() |
|
59 | { |
||
60 | 11 | if ($this->getPattern() === '') { |
|
61 | 1 | throw new \InvalidArgumentException('Pattern cannot be empty.'); |
|
62 | } |
||
63 | |||
64 | 10 | if (empty($this->getDirectory())) { |
|
65 | 1 | throw new \InvalidArgumentException( |
|
66 | 'The target directory cannot be empty.' |
||
67 | 1 | ); |
|
68 | } |
||
69 | |||
70 | 9 | if (!is_dir($this->getDirectory())) { |
|
71 | 1 | throw new \InvalidArgumentException( |
|
72 | 1 | sprintf( |
|
73 | 1 | 'The target directory "%s" does not exist.', |
|
74 | 1 | $this->getDirectory() |
|
75 | 1 | ) |
|
76 | 1 | ); |
|
77 | } |
||
78 | |||
79 | 8 | $return = false; |
|
80 | |||
81 | 8 | $file_iterator = $this->lister->ls( |
|
82 | 8 | $this->getDirectory(), |
|
83 | 8 | $this->getExtension() |
|
84 | 8 | ); |
|
85 | 8 | $count = count(iterator_to_array($file_iterator)); |
|
86 | |||
87 | 8 | if ($count > 0) { |
|
88 | 6 | $return = []; |
|
89 | 6 | foreach ($file_iterator as $file) { |
|
90 | 6 | $found = $this->finder->find($this->getPattern(), $file); |
|
91 | 6 | if ($found !== false) { |
|
92 | 4 | $return[] = [ |
|
93 | 4 | 'filename' => $found->getFilename(), |
|
94 | 4 | 'pathname' => $found->getPathName() |
|
95 | 4 | ];; |
|
96 | 4 | } |
|
97 | 6 | } |
|
98 | 6 | } |
|
99 | |||
100 | 8 | return $return; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | 11 | public function getPattern() |
|
110 | |||
111 | /** |
||
112 | * @param $pattern |
||
113 | * @return $this |
||
114 | */ |
||
115 | 11 | public function setPattern($pattern) |
|
116 | { |
||
117 | 11 | $this->pattern = $pattern; |
|
118 | |||
119 | 11 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 10 | public function getDirectory() |
|
129 | |||
130 | /** |
||
131 | * @param $directory |
||
132 | * @return $this |
||
133 | */ |
||
134 | 11 | public function setDirectory($directory) |
|
135 | { |
||
136 | 11 | $this->directory = $directory; |
|
137 | |||
138 | 11 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 8 | public function getExtension() |
|
148 | |||
149 | /** |
||
150 | * @param $extension |
||
151 | * @return $this |
||
152 | */ |
||
153 | 5 | public function setExtension($extension) |
|
159 | |||
160 | } |
||
161 |