Completed
Pull Request — master (#94)
by Alessandro
04:59
created

Cleaner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 43
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

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