Path::deleteDir()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 19
rs 9.2222
1
<?php
2
3
namespace Tleckie\Template\Compiler;
4
5
use DirectoryIterator;
6
use RuntimeException;
7
use function array_pop;
8
use function explode;
9
use function file_get_contents;
10
use function file_put_contents;
11
use function filemtime;
12
use function implode;
13
use function is_dir;
14
use function is_file;
15
use function mkdir;
16
use function rmdir;
17
use function sprintf;
18
use function trim;
19
use function unlink;
20
21
/**
22
 * Class Path
23
 *
24
 * @package Tleckie\Template\Compiler
25
 * @author  Teodoro Leckie Westberg <[email protected]>
26
 */
27
class Path implements PathInterface
28
{
29
    /** @var string|null */
30
    public ?string $file = null;
31
32
    /** @var string */
33
    private string $path;
34
35
    /**
36
     * Path constructor.
37
     *
38
     * @param string $path
39
     */
40
    public function __construct(string $path)
41
    {
42
        $this->path = sprintf("%s/", rtrim($path, '/'));
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getPath(): string
49
    {
50
        return $this->path;
51
    }
52
53
    /**
54
     * @return string|null
55
     */
56
    public function getFile(): ?string
57
    {
58
        return $this->file;
59
    }
60
61
    /**
62
     * @param string $file
63
     * @return PathInterface
64
     */
65
    public function withFile(string $file): PathInterface
66
    {
67
        $object = clone $this;
68
69
        $object->file = sprintf("%s", trim($file, '/'));
70
71
        return $object;
72
    }
73
74
    /**
75
     * @return string|null
76
     */
77
    public function read(): ?string
78
    {
79
        if (!$this->fileExist()) {
80
            throw new RuntimeException(
81
                sprintf('File [%s] not exists', $this->getFullPath())
82
            );
83
        }
84
85
        return file_get_contents($this->getFullPath());
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function fileExist(): bool
92
    {
93
        return is_file($this->getFullPath());
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getFullPath(): string
100
    {
101
        return sprintf("%s%s", $this->path, $this->file);
102
    }
103
104
    /**
105
     * @param string $content
106
     * @return bool
107
     */
108
    public function write(string $content): bool
109
    {
110
        if (!$this->dirExists()) {
111
            $this->createPath();
112
        }
113
114
        return false !== @file_put_contents(
115
            $this->getFullPath(),
116
            $content
117
        );
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function dirExists(): bool
124
    {
125
        return is_dir($this->getRealPath());
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    private function getRealPath(): string
132
    {
133
        $items = explode('/', $this->getFullPath());
134
135
        if (!is_null($this->file)) {
136
            array_pop($items);
137
        }
138
139
        return implode('/', $items);
140
    }
141
142
    /**
143
     * @throws RuntimeException
144
     */
145
    public function createPath(): void
146
    {
147
        if (!mkdir($concurrentDirectory = $this->getRealPath(), 0755, true) &&
148
            !is_dir($concurrentDirectory)) {
149
            throw new RuntimeException(
150
                sprintf(
151
                    'Directory "%s" was not created',
152
                    $concurrentDirectory
153
                )
154
            );
155
        }
156
    }
157
158
    /**
159
     * @param string|null $path
160
     * @return bool
161
     */
162
    public function deleteDir(string $path = null): bool
163
    {
164
        $iterator = new DirectoryIterator($path ?? $this->getRealPath());
165
166
        foreach ($iterator as $fileInfo) {
167
            if ($fileInfo->isDot()) {
168
                continue;
169
            }
170
171
            if ($fileInfo->isDir() && $this->deleteDir($fileInfo->getPathname())) {
172
                rmdir($fileInfo->getPathname());
173
            }
174
175
            if ($fileInfo->isFile()) {
176
                unlink($fileInfo->getPathname());
177
            }
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     * @return int
185
     */
186
    public function getModificationTime(): int
187
    {
188
        if (!$this->fileExist()) {
189
            throw new RuntimeException(
190
                sprintf('File [%s] not exists', $this->getFullPath())
191
            );
192
        }
193
194
        return filemtime($this->getFullPath());
195
    }
196
}
197