InMemoryFilesystem::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Filesystem\Manager;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Filesystem\Data\InMemoryFile;
18
use Valkyrja\Filesystem\Data\InMemoryMetadata;
19
use Valkyrja\Filesystem\Enum\Visibility;
20
use Valkyrja\Filesystem\Manager\Contract\FilesystemContract;
21
use Valkyrja\Filesystem\Throwable\Exception\UnableToReadContentsException;
22
use Valkyrja\Throwable\Exception\RuntimeException;
23
24
use function fread;
25
use function str_starts_with;
26
use function time;
27
28
class InMemoryFilesystem implements FilesystemContract
29
{
30
    /**
31
     * @var array<string, InMemoryFile>
32
     */
33
    protected array $files = [];
34
35
    public function __construct(
36
        InMemoryFile ...$files
37
    ) {
38
        foreach ($files as $file) {
39
            $this->files[$file->name] = $file;
40
        }
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    #[Override]
47
    public function exists(string $path): bool
48
    {
49
        return isset($this->files[$path]);
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    #[Override]
56
    public function read(string $path): string
57
    {
58
        return $this->files[$path]->contents
59
            ?? throw new UnableToReadContentsException("Error reading file contents for $path");
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    #[Override]
66
    public function write(string $path, string $contents): bool
67
    {
68
        $this->files[$path] = new InMemoryFile($path, $contents, timestamp: time());
69
70
        return true;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     *
76
     * @param resource $resource The resource
77
     */
78
    #[Override]
79
    public function writeStream(string $path, $resource): bool
80
    {
81
        $pathContents = fread($resource, 4096);
82
83
        if ($pathContents === false) {
84
            throw new RuntimeException('Failed to read provided resource');
85
        }
86
87
        $this->files[$path] = new InMemoryFile($path, $pathContents, timestamp: time());
88
89
        return true;
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    #[Override]
96
    public function update(string $path, string $contents): bool
97
    {
98
        return $this->write($path, $contents);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    #[Override]
105
    public function updateStream(string $path, $resource): bool
106
    {
107
        return $this->writeStream($path, $resource);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    #[Override]
114
    public function put(string $path, string $contents): bool
115
    {
116
        return $this->write($path, $contents);
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    #[Override]
123
    public function putStream(string $path, $resource): bool
124
    {
125
        return $this->writeStream($path, $resource);
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    #[Override]
132
    public function rename(string $path, string $newPath): bool
133
    {
134
        if ($this->exists($newPath) || ! $this->exists($path)) {
135
            return false;
136
        }
137
138
        $this->files[$newPath] = $this->files[$path];
139
140
        $this->delete($path);
141
142
        return true;
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    #[Override]
149
    public function copy(string $path, string $newPath): bool
150
    {
151
        if ($this->exists($newPath) || ! $this->exists($path)) {
152
            return false;
153
        }
154
155
        $this->files[$newPath] = $this->files[$path];
156
157
        return true;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    #[Override]
164
    public function delete(string $path): bool
165
    {
166
        unset($this->files[$path]);
167
168
        return true;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    #[Override]
175
    public function metadata(string $path): array|null
176
    {
177
        return $this->getMetadataInternal($path)?->toArray();
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    #[Override]
184
    public function mimetype(string $path): string|null
185
    {
186
        return $this->getMetadataInternal($path)->mimetype ?? null;
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192
    #[Override]
193
    public function size(string $path): int|null
194
    {
195
        return $this->getMetadataInternal($path)->size ?? null;
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    #[Override]
202
    public function timestamp(string $path): int|null
203
    {
204
        return $this->files[$path]->timestamp ?? null;
205
    }
206
207
    /**
208
     * @inheritDoc
209
     */
210
    #[Override]
211
    public function visibility(string $path): string|null
212
    {
213
        return $this->getMetadataInternal($path)->visibility ?? null;
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219
    #[Override]
220
    public function setVisibility(string $path, Visibility $visibility): bool
221
    {
222
        if (! $this->exists($path)) {
223
            return false;
224
        }
225
226
        $this->files[$path]->metadata->visibility = $visibility->value;
227
228
        return true;
229
    }
230
231
    /**
232
     * @inheritDoc
233
     */
234
    #[Override]
235
    public function setVisibilityPublic(string $path): bool
236
    {
237
        return $this->setVisibility($path, Visibility::PUBLIC);
238
    }
239
240
    /**
241
     * @inheritDoc
242
     */
243
    #[Override]
244
    public function setVisibilityPrivate(string $path): bool
245
    {
246
        return $this->setVisibility($path, Visibility::PRIVATE);
247
    }
248
249
    /**
250
     * @inheritDoc
251
     */
252
    #[Override]
253
    public function createDir(string $path): bool
254
    {
255
        $this->files[$path] = new InMemoryFile($path, timestamp: time());
256
257
        return true;
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    #[Override]
264
    public function deleteDir(string $path): bool
265
    {
266
        foreach ($this->files as $filePath => $file) {
267
            if (str_starts_with($filePath, $path)) {
268
                unset($this->files[$filePath]);
269
            }
270
        }
271
272
        return true;
273
    }
274
275
    /**
276
     * @inheritDoc
277
     */
278
    #[Override]
279
    public function listContents(string|null $directory = null, bool $recursive = false): array
280
    {
281
        $directory ??= '';
282
283
        $contents = [];
284
285
        foreach ($this->files as $filePath => $file) {
286
            if (str_starts_with($filePath, $directory)) {
287
                $contents[] = [
288
                    'path'     => $filePath,
289
                    'contents' => $file->contents,
290
                ];
291
            }
292
        }
293
294
        return $contents;
295
    }
296
297
    protected function getMetadataInternal(string $path): InMemoryMetadata|null
298
    {
299
        return $this->files[$path]->metadata ?? null;
300
    }
301
}
302