Filesystem   C
last analyzed

Complexity

Total Complexity 53

Size/Duplication

Total Lines 391
Duplicated Lines 34.53 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 53
lcom 1
cbo 12
dl 135
loc 391
ccs 148
cts 148
cp 1
rs 6.96
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAdapter() 0 4 1
A has() 0 6 2
A write() 8 8 1
A writeStream() 14 14 2
A put() 0 11 3
A putStream() 0 16 4
A readAndDelete() 0 14 2
A update() 9 9 1
A updateStream() 13 13 2
A read() 11 11 2
A readStream() 11 11 2
A rename() 9 9 1
A copy() 9 9 1
A delete() 0 7 1
A deleteDir() 0 10 2
A createDir() 7 7 1
A getMimetype() 11 11 3
A getTimestamp() 11 11 3
A getVisibility() 11 11 3
A getSize() 11 11 3
A setVisibility() 0 7 1
A getMetadata() 0 7 1
A assertPresent() 0 6 3
A listContents() 0 8 1
A assertAbsent() 0 6 3
A get() 0 14 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Filesystem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Filesystem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace League\Flysystem;
4
5
use InvalidArgumentException;
6
use League\Flysystem\Adapter\CanOverwriteFiles;
7
use League\Flysystem\Plugin\PluggableTrait;
8
use League\Flysystem\Util\ContentListingFormatter;
9
10
/**
11
 * @method array getWithMetadata(string $path, array $metadata)
12
 * @method bool  forceCopy(string $path, string $newpath)
13
 * @method bool  forceRename(string $path, string $newpath)
14
 * @method array listFiles(string $path = '', boolean $recursive = false)
15
 * @method array listPaths(string $path = '', boolean $recursive = false)
16
 * @method array listWith(array $keys = [], $directory = '', $recursive = false)
17
 */
18
class Filesystem implements FilesystemInterface
19
{
20
    use PluggableTrait;
21
    use ConfigAwareTrait;
22
23
    /**
24
     * @var AdapterInterface
25
     */
26
    protected $adapter;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param AdapterInterface $adapter
32
     * @param Config|array     $config
33
     */
34 246
    public function __construct(AdapterInterface $adapter, $config = null)
35
    {
36 246
        $this->adapter = $adapter;
37 246
        $this->setConfig($config);
38 246
    }
39
40
    /**
41
     * Get the Adapter.
42
     *
43
     * @return AdapterInterface adapter
44
     */
45 210
    public function getAdapter()
46
    {
47 210
        return $this->adapter;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 177
    public function has($path)
54
    {
55 177
        $path = Util::normalizePath($path);
56
57 177
        return strlen($path) === 0 ? false : (bool) $this->getAdapter()->has($path);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 15 View Code Duplication
    public function write($path, $contents, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65 15
        $path = Util::normalizePath($path);
66 15
        $this->assertAbsent($path);
67 12
        $config = $this->prepareConfig($config);
68
69 12
        return (bool) $this->getAdapter()->write($path, $contents, $config);
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 15 View Code Duplication
    public function writeStream($path, $resource, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 15
        if ( ! is_resource($resource)) {
78 6
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
79
        }
80
81 9
        $path = Util::normalizePath($path);
82 9
        $this->assertAbsent($path);
83 9
        $config = $this->prepareConfig($config);
84
85 9
        Util::rewindStream($resource);
86
87 9
        return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 12
    public function put($path, $contents, array $config = [])
94
    {
95 12
        $path = Util::normalizePath($path);
96 12
        $config = $this->prepareConfig($config);
97
98 12
        if ( ! $this->getAdapter() instanceof CanOverwriteFiles && $this->has($path)) {
99 6
            return (bool) $this->getAdapter()->update($path, $contents, $config);
100
        }
101
102 9
        return (bool) $this->getAdapter()->write($path, $contents, $config);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 15
    public function putStream($path, $resource, array $config = [])
109
    {
110 15
        if ( ! is_resource($resource)) {
111 3
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
112
        }
113
114 12
        $path = Util::normalizePath($path);
115 12
        $config = $this->prepareConfig($config);
116 12
        Util::rewindStream($resource);
117
118 12
        if ( ! $this->getAdapter() instanceof CanOverwriteFiles && $this->has($path)) {
119 6
            return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
120
        }
121
122 9
        return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 6
    public function readAndDelete($path)
129
    {
130 6
        $path = Util::normalizePath($path);
131 6
        $this->assertPresent($path);
132 6
        $contents = $this->read($path);
133
134 6
        if ($contents === false) {
135 3
            return false;
136
        }
137
138 3
        $this->delete($path);
139
140 3
        return $contents;
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146 6 View Code Duplication
    public function update($path, $contents, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148 6
        $path = Util::normalizePath($path);
149 6
        $config = $this->prepareConfig($config);
150
151 6
        $this->assertPresent($path);
152
153 6
        return (bool) $this->getAdapter()->update($path, $contents, $config);
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 15 View Code Duplication
    public function updateStream($path, $resource, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161 15
        if ( ! is_resource($resource)) {
162 6
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
163
        }
164
165 9
        $path = Util::normalizePath($path);
166 9
        $config = $this->prepareConfig($config);
167 9
        $this->assertPresent($path);
168 9
        Util::rewindStream($resource);
169
170 9
        return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176 36 View Code Duplication
    public function read($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178 36
        $path = Util::normalizePath($path);
179 36
        $this->assertPresent($path);
180
181 30
        if ( ! ($object = $this->getAdapter()->read($path))) {
182 3
            return false;
183
        }
184
185 27
        return $object['contents'];
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191 12 View Code Duplication
    public function readStream($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193 12
        $path = Util::normalizePath($path);
194 12
        $this->assertPresent($path);
195
196 12
        if ( ! $object = $this->getAdapter()->readStream($path)) {
197 6
            return false;
198
        }
199
200 9
        return $object['stream'];
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206 9 View Code Duplication
    public function rename($path, $newpath)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208 9
        $path = Util::normalizePath($path);
209 9
        $newpath = Util::normalizePath($newpath);
210 9
        $this->assertPresent($path);
211 9
        $this->assertAbsent($newpath);
212
213 9
        return (bool) $this->getAdapter()->rename($path, $newpath);
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219 12 View Code Duplication
    public function copy($path, $newpath)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221 12
        $path = Util::normalizePath($path);
222 12
        $newpath = Util::normalizePath($newpath);
223 12
        $this->assertPresent($path);
224 12
        $this->assertAbsent($newpath);
225
226 12
        return $this->getAdapter()->copy($path, $newpath);
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232 60
    public function delete($path)
233
    {
234 60
        $path = Util::normalizePath($path);
235 60
        $this->assertPresent($path);
236
237 57
        return $this->getAdapter()->delete($path);
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243 63
    public function deleteDir($dirname)
244
    {
245 63
        $dirname = Util::normalizePath($dirname);
246
247 63
        if ($dirname === '') {
248 3
            throw new RootViolationException('Root directories can not be deleted.');
249
        }
250
251 60
        return (bool) $this->getAdapter()->deleteDir($dirname);
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257 3 View Code Duplication
    public function createDir($dirname, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
    {
259 3
        $dirname = Util::normalizePath($dirname);
260 3
        $config = $this->prepareConfig($config);
261
262 3
        return (bool) $this->getAdapter()->createDir($dirname, $config);
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 12
    public function listContents($directory = '', $recursive = false)
269
    {
270 12
        $directory = Util::normalizePath($directory);
271 12
        $contents = $this->getAdapter()->listContents($directory, $recursive);
272
273 12
        return (new ContentListingFormatter($directory, $recursive, $this->config->get('case_sensitive', true)))
274 12
            ->formatListing($contents);
275
    }
276
277
    /**
278
     * @inheritdoc
279
     */
280 9 View Code Duplication
    public function getMimetype($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282 9
        $path = Util::normalizePath($path);
283 9
        $this->assertPresent($path);
284
285 9
        if (( ! $object = $this->getAdapter()->getMimetype($path)) || ! array_key_exists('mimetype', $object)) {
286 3
            return false;
287
        }
288
289 6
        return $object['mimetype'];
290
    }
291
292
    /**
293
     * @inheritdoc
294
     */
295 9 View Code Duplication
    public function getTimestamp($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
    {
297 9
        $path = Util::normalizePath($path);
298 9
        $this->assertPresent($path);
299
300 9
        if (( ! $object = $this->getAdapter()->getTimestamp($path)) || ! array_key_exists('timestamp', $object)) {
301 3
            return false;
302
        }
303
304 6
        return $object['timestamp'];
305
    }
306
307
    /**
308
     * @inheritdoc
309
     */
310 9 View Code Duplication
    public function getVisibility($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
311
    {
312 9
        $path = Util::normalizePath($path);
313 9
        $this->assertPresent($path);
314
315 9
        if (( ! $object = $this->getAdapter()->getVisibility($path)) || ! array_key_exists('visibility', $object)) {
316 3
            return false;
317
        }
318
319 6
        return $object['visibility'];
320
    }
321
322
    /**
323
     * @inheritdoc
324
     */
325 12 View Code Duplication
    public function getSize($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
326
    {
327 12
        $path = Util::normalizePath($path);
328 12
        $this->assertPresent($path);
329
330 9
        if (( ! $object = $this->getAdapter()->getSize($path)) || ! array_key_exists('size', $object)) {
331 3
            return false;
332
        }
333
334 6
        return (int) $object['size'];
335
    }
336
337
    /**
338
     * @inheritdoc
339
     */
340 9
    public function setVisibility($path, $visibility)
341
    {
342 9
        $path = Util::normalizePath($path);
343 9
        $this->assertPresent($path);
344
345 6
        return (bool) $this->getAdapter()->setVisibility($path, $visibility);
346
    }
347
348
    /**
349
     * @inheritdoc
350
     */
351 51
    public function getMetadata($path)
352
    {
353 51
        $path = Util::normalizePath($path);
354 51
        $this->assertPresent($path);
355
356 51
        return $this->getAdapter()->getMetadata($path);
357
    }
358
359
    /**
360
     * @inheritdoc
361
     */
362 69
    public function get($path, Handler $handler = null)
363
    {
364 69
        $path = Util::normalizePath($path);
365
366 69
        if ( ! $handler) {
367 45
            $metadata = $this->getMetadata($path);
368 45
            $handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path);
0 ignored issues
show
Deprecated Code introduced by Frank de Jonge
The class League\Flysystem\File has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
Deprecated Code introduced by Frank de Jonge
The class League\Flysystem\Directory has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
369
        }
370
371 69
        $handler->setPath($path);
372 69
        $handler->setFilesystem($this);
373
374 69
        return $handler;
375
    }
376
377
    /**
378
     * Assert a file is present.
379
     *
380
     * @param string $path path to file
381
     *
382
     * @throws FileNotFoundException
383
     *
384
     * @return void
385
     */
386 147
    public function assertPresent($path)
387
    {
388 147
        if ($this->config->get('disable_asserts', false) === false && ! $this->has($path)) {
389 18
            throw new FileNotFoundException($path);
390
        }
391 135
    }
392
393
    /**
394
     * Assert a file is absent.
395
     *
396
     * @param string $path path to file
397
     *
398
     * @throws FileExistsException
399
     *
400
     * @return void
401
     */
402 45
    public function assertAbsent($path)
403
    {
404 45
        if ($this->config->get('disable_asserts', false) === false && $this->has($path)) {
405 3
            throw new FileExistsException($path);
406
        }
407 42
    }
408
}
409