TemporaryDirectory::removeNode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
/**
4
 * This file is part of peridot-temporary-plugin.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace holyshared\peridot\temporary;
13
14
use \SplFileInfo;
15
use \RecursiveDirectoryIterator;
16
use \FilesystemIterator;
17
use \RecursiveIteratorIterator;
18
19
20
final class TemporaryDirectory extends TemporaryNode implements FileSystemNode
21
{
22
23
    public function __construct($path, $mode = FileSystemPermission::NORMAL)
24
    {
25
        mkdir($path, $mode);
26
        $this->node = new SplFileInfo($path);
27
    }
28
29
    public function createNewFile($name, $mode = FileSystemPermission::NORMAL)
30
    {
31
        $newFile = $this->resolvePath($name);
32
        $file = new TemporaryFile($newFile, $mode);
33
        return $file;
34
    }
35
36
    public function resolvePath($relativePath)
37
    {
38
        return $this->getPath() . '/' . $relativePath;
39
    }
40
41
    protected function removeNode()
42
    {
43
        $fileNodes = $this->createNodeIterator();
44
45
        foreach ($fileNodes as $fileNode) {
46
            if ($fileNode->isDir()) {
47
                continue;
48
            }
49
            unlink($fileNode->getPathname());
50
        }
51
        rmdir($this->getPath());
52
    }
53
54
    private function createNodeIterator()
55
    {
56
        $directory = $this->getPath();
57
        $directoryIterator = new RecursiveDirectoryIterator($directory,
58
            FilesystemIterator::CURRENT_AS_FILEINFO |
59
            FilesystemIterator::KEY_AS_PATHNAME |
60
            FilesystemIterator::SKIP_DOTS
61
        );
62
63
        $nodeIterator = new RecursiveIteratorIterator(
64
            $directoryIterator,
65
            RecursiveIteratorIterator::LEAVES_ONLY
66
        );
67
68
        return $nodeIterator;
69
    }
70
71
}
72