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

FlysystemConnectionAdapter::getReadStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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