Completed
Push — master ( 993468...5fd9a9 )
by Markus
12s queued 10s
created

TransferCleaner::cleanDirectory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Jellyfish\Transfer;
4
5
use Jellyfish\Filesystem\FilesystemInterface;
6
use Jellyfish\Finder\FinderFactoryInterface;
7
use SplFileInfo;
8
9
class TransferCleaner implements TransferCleanerInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $targetDirectory;
15
16
    /**
17
     * @var \Jellyfish\Filesystem\FilesystemInterface
18
     */
19
    protected $filesystem;
20
21
    /**
22
     * @var \Jellyfish\Finder\FinderFactoryInterface
23
     */
24
    protected $finderFactory;
25
26
    /**
27
     * @param \Jellyfish\Finder\FinderFactoryInterface $finderFactory
28
     * @param \Jellyfish\Filesystem\FilesystemInterface $filesystem
29
     * @param string $targetDirectory
30
     */
31
    public function __construct(
32
        FinderFactoryInterface $finderFactory,
33
        FilesystemInterface $filesystem,
34
        string $targetDirectory
35
    ) {
36
        $this->finderFactory = $finderFactory;
37
        $this->targetDirectory = $targetDirectory;
38
        $this->filesystem = $filesystem;
39
    }
40
41
    /**
42
     * @return \Jellyfish\Transfer\TransferCleanerInterface
43
     */
44
    public function clean(): TransferCleanerInterface
45
    {
46
        return $this->cleanDirectory($this->targetDirectory);
47
    }
48
49
    /**
50
     * @param string $directory
51
     *
52
     * @return \Jellyfish\Transfer\TransferCleanerInterface
53
     */
54
    protected function cleanDirectory(string $directory): TransferCleanerInterface
55
    {
56
        $finder = $this->finderFactory->create();
57
58
        $iterator = $finder->in($directory)->getIterator();
59
60
        foreach ($iterator as $item) {
61
            if (!($item instanceof SplFileInfo)) {
62
                continue;
63
            }
64
65
            if ($item->isDir()) {
66
                $this->cleanDirectory($item->getRealPath());
67
            }
68
69
            $this->filesystem->remove($item->getRealPath());
70
        }
71
72
        return $this;
73
    }
74
}
75