Completed
Push — master ( 400051...4d5855 )
by Alessandro
07:06
created

Cleaner::purgeCurrentTempDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 11
    public function __construct(TempDirectory $tempDirectory)
19
    {
20 11
        $this->tempDirectory = $tempDirectory;
21 11
    }
22
23 11
    public function purgeCurrentTempDir()
24
    {
25 11
        self::cleanUpDir($this->tempDirectory->getTempDirForThisExecution());
26 11
    }
27
28
    /**
29
     * @param string $dir
30
     * @return bool True if the directory existed and it has been deleted
31
     */
32 43
    public static function cleanUpDir($dir)
33
    {
34 43
        if (! file_exists($dir)) {
35
            return false;
36
        }
37
38 43
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
39 43
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
40
41 43
        foreach ($files as $file) {
42 43
            if ($file->isDir()) {
43 43
                rmdir($file->getRealPath());
44
            } else {
45 43
                unlink($file->getRealPath());
46
            }
47
        }
48
49 43
        return rmdir($dir);
50
    }
51
}
52