TransferCleaner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Transfer;
6
7
use Jellyfish\Filesystem\FilesystemInterface;
8
use Jellyfish\Finder\FinderFactoryInterface;
9
use SplFileInfo;
10
11
class TransferCleaner implements TransferCleanerInterface
12
{
13
    protected const EXCLUDED_FILE = 'factory-registry.php';
14
15
    /**
16
     * @var string
17
     */
18
    protected $targetDirectory;
19
20
    /**
21
     * @var \Jellyfish\Filesystem\FilesystemInterface
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * @var \Jellyfish\Finder\FinderFactoryInterface
27
     */
28
    protected $finderFactory;
29
30
    /**
31
     * @param \Jellyfish\Finder\FinderFactoryInterface $finderFactory
32
     * @param \Jellyfish\Filesystem\FilesystemInterface $filesystem
33
     * @param string $targetDirectory
34
     */
35
    public function __construct(
36
        FinderFactoryInterface $finderFactory,
37
        FilesystemInterface $filesystem,
38
        string $targetDirectory
39
    ) {
40
        $this->finderFactory = $finderFactory;
41
        $this->targetDirectory = $targetDirectory;
42
        $this->filesystem = $filesystem;
43
    }
44
45
    /**
46
     * @return \Jellyfish\Transfer\TransferCleanerInterface
47
     */
48
    public function clean(): TransferCleanerInterface
49
    {
50
        if (!$this->canClean()) {
51
            return $this;
52
        }
53
54
        return $this->cleanDirectory($this->targetDirectory);
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    protected function canClean(): bool
61
    {
62
        return $this->filesystem->exists($this->targetDirectory);
63
    }
64
65
    /**
66
     * @param string $directory
67
     *
68
     * @return \Jellyfish\Transfer\TransferCleanerInterface
69
     */
70
    protected function cleanDirectory(string $directory): TransferCleanerInterface
71
    {
72
        $finder = $this->finderFactory->create();
73
74
        $iterator = $finder->in($directory)->depth(0)->getIterator();
75
76
        foreach ($iterator as $item) {
77
            if (!($item instanceof SplFileInfo) || !is_string($item->getRealPath())) {
78
                continue;
79
            }
80
81
            $itemRealPath = $item->getRealPath();
82
83
            if (!$this->canRemove($itemRealPath)) {
84
                continue;
85
            }
86
87
            if ($item->isDir()) {
88
                $this->cleanDirectory($itemRealPath);
89
            }
90
91
            $this->filesystem->remove($itemRealPath);
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $realPathOfItem
99
     *
100
     * @return bool
101
     */
102
    protected function canRemove(string $realPathOfItem): bool
103
    {
104
        $realPathOfFactoryRegistry = $this->targetDirectory . static::EXCLUDED_FILE;
105
106
        return $realPathOfFactoryRegistry !== $realPathOfItem;
107
    }
108
}
109