Completed
Pull Request — master (#34)
by Alessandro
03:03 queued 40s
created

Cleaner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
ccs 17
cts 18
cp 0.9444
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 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