Completed
Push — master ( c02786...ba52e0 )
by Alessandro
5s
created

Cleaner::cleanUpDir()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

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