Completed
Push — master ( 9fe096...a04045 )
by
unknown
07:07
created

TemporaryDirectory   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 0
loc 112
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 7
A create() 0 4 1
A forceCreate() 0 4 1
A path() 0 16 3
A empty() 0 4 1
A delete() 0 4 1
A getSystemTemporaryDirectory() 0 4 1
A sanitizePath() 0 6 1
A removeFilenameFromPath() 0 8 2
A isFilePath() 0 4 1
C deleteDirectory() 0 22 7
1
<?php
2
3
namespace Spatie\TemporaryDirectory;
4
5
use InvalidArgumentException;
6
7
class TemporaryDirectory
8
{
9
    /** @var string */
10
    protected $path;
11
12
    public function __construct(string $path, bool $overwriteExistingDirectory = false)
13
    {
14
        if (empty($path)) {
15
            $path = microtime();
16
        }
17
18
        $this->path = $this->getSystemTemporaryDirectory().DIRECTORY_SEPARATOR.$this->sanitizePath($path);
19
20
        if ($overwriteExistingDirectory && file_exists($this->path)) {
21
            $this->deleteDirectory($this->path);
22
        }
23
24
        if (! $overwriteExistingDirectory && file_exists($this->path)) {
25
            throw new InvalidArgumentException("Path `{$path}` already exists.");
26
        }
27
28
        if (! file_exists($this->path)) {
29
            mkdir($this->path, 0777, true);
30
        }
31
    }
32
33
    public static function create($path)
34
    {
35
        return new TemporaryDirectory($path, false);
36
    }
37
38
    public static function forceCreate($path)
39
    {
40
        return new TemporaryDirectory($path, true);
41
    }
42
43
    public function path(string $pathOrFilename = ''): string
44
    {
45
        if (empty($pathOrFilename)) {
46
            return $this->path;
47
        }
48
49
        $path = $this->path.DIRECTORY_SEPARATOR.trim($pathOrFilename, '/');
50
51
        $directoryPath = $this->removeFilenameFromPath($path);
52
53
        if (! file_exists($directoryPath)) {
54
            mkdir($directoryPath, 0777, true);
55
        }
56
57
        return $path;
58
    }
59
60
    public function empty() {
61
        $this->deleteDirectory($this->path);
62
        mkdir($this->path);
63
    }
64
65
    public function delete()
66
    {
67
        $this->deleteDirectory($this->path);
68
    }
69
70
    protected function getSystemTemporaryDirectory()
71
    {
72
        return rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
73
    }
74
75
    protected function sanitizePath(string $path): string
76
    {
77
        $path = rtrim($path);
78
79
        return rtrim($path, DIRECTORY_SEPARATOR);
80
    }
81
82
    protected function removeFilenameFromPath(string $path): string
83
    {
84
        if (! $this->isFilePath($path)) {
85
            return $path;
86
        }
87
88
        return substr($path, 0, strrpos($path, DIRECTORY_SEPARATOR));
89
    }
90
91
    protected function isFilePath(string $path): bool
92
    {
93
        return strpos($path, '.') !== false;
94
    }
95
96
    protected function deleteDirectory(string $path): bool
97
    {
98
        if (! file_exists($path)) {
99
            return true;
100
        }
101
102
        if (! is_dir($path)) {
103
            return unlink($path);
104
        }
105
106
        foreach (scandir($path) as $item) {
107
            if ($item == '.' || $item == '..') {
108
                continue;
109
            }
110
111
            if (! $this->deleteDirectory($path.DIRECTORY_SEPARATOR.$item)) {
112
                return false;
113
            }
114
        }
115
116
        return rmdir($path);
117
    }
118
}
119