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

Cleaner::purgeCurrentTempDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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