Code Duplication    Length = 59-59 lines in 2 locations

src/Handlers/OkFileHandler.php 1 location

@@ 317-375 (lines=59) @@
314
     * @return void
315
     * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or the OK can not be cleaned-up
316
     */
317
    public function cleanUpOkFile(string $filename) : void
318
    {
319
320
        // query whether or not the subject needs an OK file, if yes remove the filename from the file
321
        if ($this->getSubjectConfiguration()->isOkFileNeeded() === false) {
322
            return;
323
        }
324
325
        try {
326
            // try to load the expected OK filenames
327
            if (sizeof($okFilenames = $this->getOkFilenames()) === 0) {
328
                throw new MissingOkFileException(sprintf('Can\'t find a OK filename for file %s', $filename));
329
            }
330
331
            // iterate over the found OK filenames (should usually be only one, but could be more)
332
            foreach ($okFilenames as $okFilename) {
333
                // clear the filecache
334
                \clearstatcache();
335
                // if the OK filename matches the CSV filename AND the OK file is empty
336
                if ($this->isEqualFilename($filename, $okFilename) && filesize($okFilename) === 0) {
337
                    unlink($okFilename);
338
                    return;
339
                }
340
341
                // else, remove the CSV filename from the OK file
342
                $this->removeLineFromFile(basename($filename), $fh = fopen($okFilename, 'r+'));
343
                fclose($fh);
344
345
                // if the OK file is empty, delete the file
346
                if (filesize($okFilename) === 0) {
347
                    unlink($okFilename);
348
                }
349
350
                // return immediately
351
                return;
352
            }
353
354
            // throw an exception if either no OK file has been found,
355
            // or the CSV file is not in one of the OK files
356
            throw new \Exception(
357
                sprintf(
358
                    'Can\'t found filename %s in one of the expected OK files: %s',
359
                    $filename,
360
                    implode(', ', $okFilenames)
361
                )
362
            );
363
        } catch (LineNotFoundException $lne) {
364
            // wrap and re-throw the exception
365
            throw new \Exception(
366
                sprintf(
367
                    'Can\'t remove filename %s from OK file: %s',
368
                    $filename,
369
                    $okFilename
370
                ),
371
                null,
372
                $lne
373
            );
374
        }
375
    }
376
377
    /**
378
     * Create's the .OK files for all .CSV files that matches the passed pattern.

src/Subjects/FileResolver/OkFileAwareFileResolver.php 1 location

@@ 212-270 (lines=59) @@
209
     * @return void
210
     * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or the OK can not be cleaned-up
211
     */
212
    public function cleanUpOkFile(string $filename) : void
213
    {
214
215
        // query whether or not the subject needs an OK file, if yes remove the filename from the file
216
        if ($this->getSubjectConfiguration()->isOkFileNeeded() === false) {
217
            return;
218
        }
219
220
        try {
221
            // try to load the expected OK filenames
222
            if (sizeof($okFilenames = $this->getOkFilenames()) === 0) {
223
                throw new MissingOkFileException(sprintf('Can\'t find a OK filename for file %s', $filename));
224
            }
225
226
            // iterate over the found OK filenames (should usually be only one, but could be more)
227
            foreach ($okFilenames as $okFilename) {
228
                // clear the filecache
229
                \clearstatcache();
230
                // if the OK filename matches the CSV filename AND the OK file is empty
231
                if ($this->isEqualFilename($filename, $okFilename) && filesize($okFilename) === 0) {
232
                    unlink($okFilename);
233
                    return;
234
                }
235
236
                // else, remove the CSV filename from the OK file
237
                $this->removeLineFromFile(basename($filename), $fh = fopen($okFilename, 'r+'));
238
                fclose($fh);
239
240
                // if the OK file is empty, delete the file
241
                if (filesize($okFilename) === 0) {
242
                    unlink($okFilename);
243
                }
244
245
                // return immediately
246
                return;
247
            }
248
249
            // throw an exception if either no OK file has been found,
250
            // or the CSV file is not in one of the OK files
251
            throw new \Exception(
252
                sprintf(
253
                    'Can\'t found filename %s in one of the expected OK files: %s',
254
                    $filename,
255
                    implode(', ', $okFilenames)
256
                )
257
            );
258
        } catch (LineNotFoundException $lne) {
259
            // wrap and re-throw the exception
260
            throw new \Exception(
261
                sprintf(
262
                    'Can\'t remove filename %s from OK file: %s',
263
                    $filename,
264
                    $okFilename
265
                ),
266
                null,
267
                $lne
268
            );
269
        }
270
    }
271
272
    /**
273
     * Loads the files from the source directory and return's them sorted.