Completed
Push — master ( 823c18...cc9241 )
by Alessandro
07:14
created

Cleaner::deleteTempConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Paraunit\File;
4
5
use Paraunit\Configuration\PHPUnitConfig;
6
7
/**
8
 * Class Cleaner
9
 * @package Paraunit\File
10
 */
11
class Cleaner
12
{
13
    /** @var  TempDirectory */
14
    private $tempDirectory;
15
16
    /** @var PHPUnitConfig */
17
    private $phpunitConfig;
18
19
    /**
20
     * Cleaner constructor.
21
     * @param TempDirectory $tempDirectory
22
     * @param PHPUnitConfig $phpunitConfig
23
     */
24 15
    public function __construct(TempDirectory $tempDirectory, PHPUnitConfig $phpunitConfig)
25
    {
26 15
        $this->tempDirectory = $tempDirectory;
27 15
        $this->phpunitConfig = $phpunitConfig;
28 15
    }
29
30 15
    public function purgeCurrentTempDir()
31
    {
32 15
        self::cleanUpDir($this->tempDirectory->getTempDirForThisExecution());
33 15
    }
34
35 15
    public function deleteTempConfig()
36
    {
37 15
        $filename = $this->phpunitConfig->getFileFullPath();
38 15
        if (file_exists($filename)) {
39 15
            unlink($filename);
40
        }
41 15
    }
42
43
    /**
44
     * @param string $dir
45
     * @return bool True if the directory existed and it has been deleted
46
     */
47 49
    public static function cleanUpDir($dir)
48
    {
49 49
        if (! file_exists($dir)) {
50
            return false;
51
        }
52
53 49
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
54 49
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
55
56 49
        foreach ($files as $file) {
57 49
            if ($file->isDir()) {
58 49
                rmdir($file->getRealPath());
59
            } else {
60 49
                unlink($file->getRealPath());
61
            }
62
        }
63
64 49
        return rmdir($dir);
65
    }
66
}
67