Cleaner   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 51
ccs 16
cts 17
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A purgeCurrentTempDir() 0 4 1
A cleanUpDir() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\File;
6
7
use Paraunit\Lifecycle\EngineEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
/**
11
 * Class Cleaner
12
 * @package Paraunit\File
13
 */
14
class Cleaner implements EventSubscriberInterface
15
{
16
    /** @var TempDirectory */
17
    private $tempDirectory;
18
19
    /**
20
     * Cleaner constructor.
21
     * @param TempDirectory $tempDirectory
22
     */
23 25
    public function __construct(TempDirectory $tempDirectory)
24
    {
25 25
        $this->tempDirectory = $tempDirectory;
26
    }
27
28 70
    public static function getSubscribedEvents(): array
29
    {
30
        return [
31 70
            EngineEvent::BEFORE_START => 'purgeCurrentTempDir',
32 70
            EngineEvent::END => 'purgeCurrentTempDir',
33
        ];
34
    }
35
36 24
    public function purgeCurrentTempDir()
37
    {
38 24
        self::cleanUpDir($this->tempDirectory->getTempDirForThisExecution());
39
    }
40
41
    /**
42
     * @param string $dir
43
     * @return bool True if the directory existed and it has been deleted
44
     */
45 66
    public static function cleanUpDir(string $dir): bool
46
    {
47 66
        if (! file_exists($dir)) {
48
            return false;
49
        }
50
51 66
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
52 66
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
53
54 66
        foreach ($files as $file) {
55 66
            if ($file->isDir()) {
56 58
                rmdir($file->getRealPath());
57
            } else {
58 66
                unlink($file->getRealPath());
59
            }
60
        }
61
62 66
        return rmdir($dir);
63
    }
64
}
65