1 | <?php |
||
35 | class OkFileAwareFileResolver extends SimpleFileResolver implements OkFileAwareFileResolverInterface |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * Remove's the passed line from the file with the passed name. |
||
40 | * |
||
41 | * @param string $line The line to be removed |
||
42 | * @param string $filename The name of the file the line has to be removed |
||
43 | * |
||
44 | * @return void |
||
45 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
46 | */ |
||
47 | protected function removeLineFromFile($line, $filename) |
||
51 | |||
52 | /** |
||
53 | * Query whether or not the basename, without suffix, of the passed filenames are equal. |
||
54 | * |
||
55 | * @param string $filename1 The first filename to compare |
||
56 | * @param string $filename2 The second filename to compare |
||
57 | * |
||
58 | * @return boolean TRUE if the passed files basename are equal, else FALSE |
||
59 | */ |
||
60 | 1 | protected function isEqualFilename($filename1, $filename2) |
|
64 | |||
65 | /** |
||
66 | * Strips the passed suffix, including the (.), from the filename and returns it. |
||
67 | * |
||
68 | * @param string $filename The filename to return the suffix from |
||
69 | * @param string $suffix The suffix to return |
||
70 | * |
||
71 | * @return string The filname without the suffix |
||
72 | */ |
||
73 | 1 | protected function stripSuffix($filename, $suffix) |
|
77 | |||
78 | /** |
||
79 | * Prepares and returns an OK filename from the passed parts. |
||
80 | * |
||
81 | * @param array $parts The parts to concatenate the OK filename from |
||
82 | * |
||
83 | * @return string The OK filename |
||
84 | */ |
||
85 | 1 | protected function prepareOkFilename(array $parts) |
|
89 | |||
90 | /** |
||
91 | * Return's an array with the names of the expected OK files for the actual subject. |
||
92 | * |
||
93 | * @return array The array with the expected OK filenames |
||
94 | */ |
||
95 | 1 | protected function getOkFilenames() |
|
120 | |||
121 | /** |
||
122 | * Queries whether or not, the passed filename should be handled by the subject. |
||
123 | * |
||
124 | * @param string $filename The filename to query for |
||
125 | * |
||
126 | * @return boolean TRUE if the file should be handled, else FALSE |
||
127 | */ |
||
128 | 1 | public function shouldBeHandled($filename) |
|
180 | |||
181 | /** |
||
182 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
183 | * the OK file will be cleaned-up. |
||
184 | * |
||
185 | * @param string $filename The filename to be cleaned-up |
||
186 | * |
||
187 | * @return void |
||
188 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or the OK can not be cleaned-up |
||
189 | */ |
||
190 | public function cleanUpOkFile($filename) |
||
191 | { |
||
192 | |||
193 | // query whether or not the subject needs an OK file, if yes remove the filename from the file |
||
194 | if ($this->getSubjectConfiguration()->isOkFileNeeded() === false) { |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | try { |
||
199 | // try to load the expected OK filenames |
||
200 | if (sizeof($okFilenames = $this->getOkFilenames()) === 0) { |
||
201 | throw new MissingOkFileException(sprintf('Can\'t find a OK filename for file %s', $filename)); |
||
202 | } |
||
203 | |||
204 | // iterate over the found OK filenames (should usually be only one, but could be more) |
||
205 | foreach ($okFilenames as $okFilename) { |
||
206 | // clear the filecache |
||
207 | \clearstatcache(); |
||
208 | // if the OK filename matches the CSV filename AND the OK file is empty |
||
209 | if ($this->isEqualFilename($filename, $okFilename) && filesize($okFilename) === 0) { |
||
210 | unlink($okFilename); |
||
211 | return; |
||
212 | } |
||
213 | |||
214 | // else, remove the CSV filename from the OK file |
||
215 | $this->removeLineFromFile(basename($filename), $fh = fopen($okFilename, 'r+')); |
||
216 | fclose($fh); |
||
217 | |||
218 | // if the OK file is empty, delete the file |
||
219 | if (filesize($okFilename) === 0) { |
||
220 | unlink($okFilename); |
||
221 | } |
||
222 | |||
223 | // return immediately |
||
224 | return; |
||
225 | } |
||
226 | |||
227 | // throw an exception if either no OK file has been found, |
||
228 | // or the CSV file is not in one of the OK files |
||
229 | throw new \Exception( |
||
230 | sprintf( |
||
231 | 'Can\'t found filename %s in one of the expected OK files: %s', |
||
232 | $filename, |
||
233 | implode(', ', $okFilenames) |
||
234 | ) |
||
235 | ); |
||
236 | } catch (LineNotFoundException $lne) { |
||
237 | // wrap and re-throw the exception |
||
238 | throw new \Exception( |
||
239 | sprintf( |
||
240 | 'Can\'t remove filename %s from OK file: %s', |
||
241 | $filename, |
||
242 | $okFilename |
||
243 | ), |
||
244 | null, |
||
245 | $lne |
||
246 | ); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Loads the files from the source directory and return's them sorted. |
||
252 | * |
||
253 | * @param string $serial The unique identifier of the actual import process |
||
254 | * |
||
255 | * @return array The array with the files matching the subjects suffix |
||
256 | * @throws \Exception Is thrown, when the source directory is NOT available |
||
257 | * @throws \TechDivision\Import\Exceptions\MissingOkFileException Is thrown, if files to be processed are available but the mandatory OK file is missing |
||
258 | */ |
||
259 | public function loadFiles($serial) |
||
293 | } |
||
294 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: