FileDeleter::perform()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\FileOperations;
6
7
/**
8
 * Gets the contents of our previously declared iterator
9
 */
10
class FileDeleter extends FileSelection
11
{
12
    /**
13
     * Contains the full path of all files that were deleted
14
     * @var array
15
     */
16
    public $deletedFiles = [];
17
18
    /**
19
     * Performs the actual deletion of the previous delete selection
20
     *
21
     * @return FileDeleter Returns same object for easy method concatenation
22
     */
23
    public function perform(): FileActionInterface
24
    {
25
        foreach ($this->iterator as $file) {
26
            $fullFile = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename();
27
            $this->deletedFiles[] = $fullFile;
28
            $this->logger->info('Removing', ['file' => $fullFile, 'dry-run' => $this->isTestMode]);
29
            if ($this->isTestMode === false) {
30
                if ($file->isDir()) {
31
                    rmdir($fullFile);
32
                } else {
33
                    unlink($fullFile);
34
                }
35
            }
36
        }
37
38
        return $this;
39
    }
40
}
41