FlysystemFilesystem::listContents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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;
15
16
use League\Flysystem\FilesystemException;
17
use League\Flysystem\FilesystemOperator as FlysystemInterface;
18
use League\Flysystem\StorageAttributes;
19
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...
20
use Valkyrja\Filesystem\Contract\Filesystem as Contract;
21
use Valkyrja\Filesystem\Enum\Visibility;
22
23
use function array_map;
24
25
/**
26
 * Class FlysystemFilesystem.
27
 *
28
 * @author Melech Mizrachi
29
 */
30
class FlysystemFilesystem implements Contract
31
{
32
    /**
33
     * FlysystemFilesystem constructor.
34
     *
35
     * @param FlysystemInterface $flysystem The Flysystem filesystem
36
     */
37
    public function __construct(
38
        protected FlysystemInterface $flysystem
39
    ) {
40
    }
41
42
    /**
43
     * @inheritDoc
44
     *
45
     * @throws FilesystemException
46
     */
47
    #[Override]
48
    public function exists(string $path): bool
49
    {
50
        return $this->flysystem->has($path);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     *
56
     * @throws FilesystemException
57
     */
58
    #[Override]
59
    public function read(string $path): string
60
    {
61
        return $this->flysystem->read($path);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     *
67
     * @throws FilesystemException
68
     */
69
    #[Override]
70
    public function write(string $path, string $contents): bool
71
    {
72
        $this->flysystem->write($path, $contents);
73
74
        return true;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     *
80
     * @throws FilesystemException
81
     */
82
    #[Override]
83
    public function writeStream(string $path, $resource): bool
84
    {
85
        $this->flysystem->writeStream($path, $resource);
86
87
        return true;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     *
93
     * @throws FilesystemException
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
     * @throws FilesystemException
105
     */
106
    #[Override]
107
    public function updateStream(string $path, $resource): bool
108
    {
109
        return $this->writeStream($path, $resource);
110
    }
111
112
    /**
113
     * @inheritDoc
114
     *
115
     * @throws FilesystemException
116
     */
117
    #[Override]
118
    public function put(string $path, string $contents): bool
119
    {
120
        return $this->write($path, $contents);
121
    }
122
123
    /**
124
     * @inheritDoc
125
     *
126
     * @throws FilesystemException
127
     */
128
    #[Override]
129
    public function putStream(string $path, $resource): bool
130
    {
131
        return $this->writeStream($path, $resource);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     *
137
     * @throws FilesystemException
138
     */
139
    #[Override]
140
    public function rename(string $path, string $newPath): bool
141
    {
142
        $this->flysystem->move($path, $newPath);
143
144
        return true;
145
    }
146
147
    /**
148
     * @inheritDoc
149
     *
150
     * @throws FilesystemException
151
     */
152
    #[Override]
153
    public function copy(string $path, string $newPath): bool
154
    {
155
        $this->flysystem->copy($path, $newPath);
156
157
        return true;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     *
163
     * @throws FilesystemException
164
     */
165
    #[Override]
166
    public function delete(string $path): bool
167
    {
168
        $this->flysystem->delete($path);
169
170
        return true;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    #[Override]
177
    public function metadata(string $path): array|null
178
    {
179
        return null;
180
    }
181
182
    /**
183
     * @inheritDoc
184
     *
185
     * @throws FilesystemException
186
     */
187
    #[Override]
188
    public function mimetype(string $path): string|null
189
    {
190
        return $this->flysystem->mimeType($path);
191
    }
192
193
    /**
194
     * @inheritDoc
195
     *
196
     * @throws FilesystemException
197
     */
198
    #[Override]
199
    public function size(string $path): int|null
200
    {
201
        return $this->flysystem->fileSize($path);
202
    }
203
204
    /**
205
     * @inheritDoc
206
     *
207
     * @throws FilesystemException
208
     */
209
    #[Override]
210
    public function timestamp(string $path): int|null
211
    {
212
        return $this->flysystem->lastModified($path);
213
    }
214
215
    /**
216
     * @inheritDoc
217
     *
218
     * @throws FilesystemException
219
     */
220
    #[Override]
221
    public function visibility(string $path): string|null
222
    {
223
        return $this->flysystem->visibility($path);
224
    }
225
226
    /**
227
     * @inheritDoc
228
     *
229
     * @throws FilesystemException
230
     */
231
    #[Override]
232
    public function setVisibility(string $path, Visibility $visibility): bool
233
    {
234
        $this->flysystem->setVisibility($path, $visibility->value);
235
236
        return true;
237
    }
238
239
    /**
240
     * @inheritDoc
241
     *
242
     * @throws FilesystemException
243
     */
244
    #[Override]
245
    public function setVisibilityPublic(string $path): bool
246
    {
247
        $this->flysystem->setVisibility($path, Visibility::PUBLIC->value);
248
249
        return true;
250
    }
251
252
    /**
253
     * @inheritDoc
254
     *
255
     * @throws FilesystemException
256
     */
257
    #[Override]
258
    public function setVisibilityPrivate(string $path): bool
259
    {
260
        $this->flysystem->setVisibility($path, Visibility::PRIVATE->value);
261
262
        return true;
263
    }
264
265
    /**
266
     * @inheritDoc
267
     *
268
     * @throws FilesystemException
269
     */
270
    #[Override]
271
    public function createDir(string $path): bool
272
    {
273
        $this->flysystem->createDirectory($path);
274
275
        return true;
276
    }
277
278
    /**
279
     * @inheritDoc
280
     *
281
     * @throws FilesystemException
282
     */
283
    #[Override]
284
    public function deleteDir(string $path): bool
285
    {
286
        $this->flysystem->deleteDirectory($path);
287
288
        return true;
289
    }
290
291
    /**
292
     * @inheritDoc
293
     *
294
     * @throws FilesystemException
295
     *
296
     * @psalm-suppress MixedReturnTypeCoercion
297
     */
298
    #[Override]
299
    public function listContents(string|null $directory = null, bool $recursive = false): array
300
    {
301
        return array_map(
302
            /**
303
             * @return array<string, string|int>
304
             */
305
            static fn (StorageAttributes $attributes): array => (array) $attributes->jsonSerialize(),
306
            $this->flysystem->listContents($directory ?? '', $recursive)->toArray()
307
        );
308
    }
309
310
    /**
311
     * @inheritDoc
312
     */
313
    public function getFlysystem(): FlysystemInterface
314
    {
315
        return $this->flysystem;
316
    }
317
}
318