Completed
Push — master ( 7c0ec5...af2bde )
by Alessandro
06:40 queued 57s
created

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