Completed
Push — master ( c5a3b4...fa0d52 )
by Arne
01:49
created

FlysystemStorageDriver::getReadStream()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace Archivr\StorageDriver;
4
5
use Archivr\Exception\Exception;
6
use League\Flysystem\Exception as FlysystemException;
7
use League\Flysystem\Filesystem;
8
9
class FlysystemStorageDriver implements StorageDriverInterface
10
{
11
    /**
12
     * @var Filesystem
13
     */
14
    protected $filesystem;
15
16
    public function __construct(Filesystem $filesystem)
17
    {
18
        $this->filesystem = $filesystem;
19
    }
20
21
    public function read(string $relativePath): string
22
    {
23
        try
24
        {
25
            $content = $this->filesystem->read($relativePath);
26
27
            if (!is_string($content))
28
            {
29
                throw new Exception(sprintf('read() failed for %s.', $relativePath));
30
            }
31
        }
32
        catch (FlysystemException $exception)
33
        {
34
            throw new Exception($exception->getMessage(), 0, $exception);
35
        }
36
37
        return $content;
38
    }
39
40
    public function write(string $relativePath, string $content)
41
    {
42
        try
43
        {
44
            $success = $this->filesystem->put($relativePath, $content);
45
46
            if (!$success)
47
            {
48
                throw new Exception(sprintf('write() failed for %s.', $relativePath));
49
            }
50
        }
51
        catch (FlysystemException $exception)
52
        {
53
            throw new Exception($exception->getMessage(), 0, $exception);
54
        }
55
    }
56
57
    public function writeStream(string $relativePath, $stream)
58
    {
59
        try
60
        {
61
            $success = $this->filesystem->putStream($relativePath, $stream);
62
63
            if (!$success)
64
            {
65
                throw new Exception(sprintf('writeStream() failed for %s.', $relativePath));
66
            }
67
        }
68
        catch (FlysystemException $exception)
69
        {
70
            throw new Exception($exception->getMessage(), 0, $exception);
71
        }
72
    }
73
74
    public function exists(string $relativePath): bool
75
    {
76
        try
77
        {
78
            return $this->filesystem->has($relativePath);
79
        }
80
        catch (FlysystemException $exception)
81
        {
82
            throw new Exception($exception->getMessage(), 0, $exception);
83
        }
84
    }
85
86 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...
87
    {
88
        try
89
        {
90
            $success = $this->filesystem->delete($relativePath);
91
92
            if (!$success)
93
            {
94
                throw new Exception(sprintf('unlink() failed for %s', $relativePath));
95
            }
96
        }
97
        catch (FlysystemException $exception)
98
        {
99
            throw new Exception($exception->getMessage(), 0, $exception);
100
        }
101
    }
102
103 View Code Duplication
    public function getReadStream(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...
104
    {
105
        try
106
        {
107
            $stream = $this->filesystem->readStream($relativePath);
108
109
            if (!is_resource($stream))
110
            {
111
                throw new Exception(sprintf('getReadStream() failed for %s.', $relativePath));
112
            }
113
114
            return $stream;
115
        }
116
        catch (FlysystemException $exception)
117
        {
118
            throw new Exception($exception->getMessage(), 0, $exception);
119
        }
120
    }
121
}
122