Completed
Push — master ( 83bd2d...9517e2 )
by Arne
03:10
created

FlysystemConnectionAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 18
loc 72
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A read() 0 11 2
A write() 9 9 2
A writeStream() 0 9 2
A exists() 0 4 1
A unlink() 9 9 2
A getReadStream() 0 11 2

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Archivr\ConnectionAdapter;
4
5
use League\Flysystem\Filesystem;
6
7
class FlysystemConnectionAdapter implements ConnectionAdapterInterface
8
{
9
    /**
10
     * @var Filesystem
11
     */
12
    protected $filesystem;
13
14
    public function __construct(Filesystem $filesystem)
15
    {
16
        $this->filesystem = $filesystem;
17
        $this->filesystem->getConfig()->set('disable_asserts', true);
18
    }
19
20
    public function read(string $relativePath): string
21
    {
22
        $content = $this->filesystem->read($relativePath);
23
24
        if (!is_string($content))
25
        {
26
            throw new \RuntimeException(sprintf('read() failed for %s.', $relativePath));
27
        }
28
29
        return $content;
30
    }
31
32 View Code Duplication
    public function write(string $relativePath, string $content)
0 ignored issues
show
Duplication introduced by
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...
33
    {
34
        $success = $this->filesystem->write($relativePath, $content);
35
36
        if (!$success)
37
        {
38
            throw new \RuntimeException(sprintf('write() failed for %s.', $relativePath));
39
        }
40
    }
41
42
    public function writeStream(string $relativePath, $stream)
43
    {
44
        $success = $this->filesystem->writeStream($relativePath, $stream);
45
46
        if (!$success)
47
        {
48
            throw new \RuntimeException(sprintf('writeStream() failed for %s.', $relativePath));
49
        }
50
    }
51
52
    public function exists(string $relativePath): bool
53
    {
54
        return $this->filesystem->has($relativePath);
55
    }
56
57 View Code Duplication
    public function unlink(string $relativePath)
0 ignored issues
show
Duplication introduced by
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...
58
    {
59
        $success = $this->filesystem->delete($relativePath);
60
61
        if (!$success)
62
        {
63
            throw new \RuntimeException(sprintf('unlink() failed for %s', $relativePath));
64
        }
65
    }
66
67
    public function getReadStream(string $relativePath)
68
    {
69
        $stream = $this->filesystem->readStream($relativePath);
70
71
        if (!is_resource($stream))
72
        {
73
            throw new \RuntimeException(sprintf('getReadStream() failed for %s.', $relativePath));
74
        }
75
76
        return $stream;
77
    }
78
}
79