Completed
Push — master ( f56c85...ec37c6 )
by
unknown
54:29 queued 43:29
created

TemporaryDirectory::create()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 24
nop 0
1
<?php
2
3
namespace Spatie\TemporaryDirectory;
4
5
use Exception;
6
use InvalidArgumentException;
7
8
class TemporaryDirectory
9
{
10
    /** @var string */
11
    protected $location;
12
13
    /** @var string */
14
    protected $name;
15
16
    /** @var bool */
17
    protected $forceCreate = false;
18
19
    /** @return $this */
20
    public function create()
21
    {
22
        if (empty($this->location)) {
23
            $this->location = $this->getSystemTemporaryDirectory();
24
        }
25
26
        if (empty($this->name)) {
27
            $this->name = microtime();
28
        }
29
30
        if ($this->forceCreate && file_exists($this->getFullPath())) {
31
            $this->deleteDirectory($this->getFullPath());
32
        }
33
34
        if (file_exists($this->getFullPath())) {
35
            throw new InvalidArgumentException("Path `{$this->getFullPath()}` already exists.");
36
        }
37
38
        if (! file_exists($this->getFullPath())) {
39
            mkdir($this->getFullPath(), 0777, true);
40
        }
41
42
        return $this;
43
    }
44
45
    /** @return $this */
46
    public function force()
47
    {
48
        $this->forceCreate = true;
49
50
        return $this;
51
    }
52
53
    /**
54
     *  @param string $name
55
     *
56
     *  @return $this
57
     */
58
    public function name($name)
59
    {
60
        $this->name = $this->sanitizeName($name);
61
62
        return $this;
63
    }
64
65
    /**
66
     *  @param string $location
67
     *
68
     *  @return $this
69
     */
70
    public function location($location)
71
    {
72
        $this->location = $this->sanitizePath($location);
73
74
        return $this;
75
    }
76
77
    public function path(string $pathOrFilename = ''): string
78
    {
79
        if (empty($pathOrFilename)) {
80
            return $this->getFullPath();
81
        }
82
83
        $path = $this->getFullPath().DIRECTORY_SEPARATOR.trim($pathOrFilename, '/');
84
85
        $directoryPath = $this->removeFilenameFromPath($path);
86
87
        if (! file_exists($directoryPath)) {
88
            mkdir($directoryPath, 0777, true);
89
        }
90
91
        return $path;
92
    }
93
94
    /** @return $this */
95
    public function empty()
96
    {
97
        $this->deleteDirectory($this->getFullPath());
98
        mkdir($this->getFullPath());
99
100
        return $this;
101
    }
102
103
    public function delete()
104
    {
105
        $this->deleteDirectory($this->getFullPath());
106
    }
107
108
    protected function getFullPath(): string
109
    {
110
        return $this->location.DIRECTORY_SEPARATOR.$this->name;
111
    }
112
113
    protected function isValidDirectoryName(string $directoryName): bool
114
    {
115
        return strpbrk($directoryName, '\\/?%*:|"<>') === false;
116
    }
117
118
    protected function getSystemTemporaryDirectory(): string
119
    {
120
        return rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
121
    }
122
123
    protected function sanitizePath(string $path): string
124
    {
125
        $path = rtrim($path);
126
127
        return rtrim($path, DIRECTORY_SEPARATOR);
128
    }
129
130
    protected function sanitizeName(string $name): string
131
    {
132
        if (! $this->isValidDirectoryName($name)) {
133
            throw new Exception("The directory name `$name` contains invalid characters.");
134
        }
135
136
        return trim($name);
137
    }
138
139
    protected function removeFilenameFromPath(string $path): string
140
    {
141
        if (! $this->isFilePath($path)) {
142
            return $path;
143
        }
144
145
        return substr($path, 0, strrpos($path, DIRECTORY_SEPARATOR));
146
    }
147
148
    protected function isFilePath(string $path): bool
149
    {
150
        return strpos($path, '.') !== false;
151
    }
152
153
    protected function deleteDirectory(string $path): bool
154
    {
155
        if (! file_exists($path)) {
156
            return true;
157
        }
158
159
        if (! is_dir($path)) {
160
            return unlink($path);
161
        }
162
163
        foreach (scandir($path) as $item) {
164
            if ($item == '.' || $item == '..') {
165
                continue;
166
            }
167
168
            if (! $this->deleteDirectory($path.DIRECTORY_SEPARATOR.$item)) {
169
                return false;
170
            }
171
        }
172
173
        return rmdir($path);
174
    }
175
}
176