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

Cleaner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
ccs 15
cts 16
cp 0.9375
rs 10

3 Methods

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