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

Cleaner   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 56
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A purgeCurrentTempDir() 0 4 1
A deleteTempConfig() 0 7 2
A cleanUpDir() 0 19 4
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