Completed
Push — master ( 7d5317...877556 )
by Arne
02:32
created

FlysystemStorageAdapter::writeStream()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Storeman\StorageAdapter;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\NullLogger;
8
use Storeman\Exception;
9
use League\Flysystem\Filesystem;
10
11
abstract class FlysystemStorageAdapter implements LoggerAwareInterface, StorageAdapterInterface
12
{
13
    use LoggerAwareTrait;
14
15
    /**
16
     * @var Filesystem
17
     */
18
    protected $filesystem;
19
20
    public function __construct(Filesystem $filesystem)
21
    {
22
        $this->filesystem = $filesystem;
23
        $this->logger = new NullLogger();
24
    }
25
26 View Code Duplication
    public function writeStream(string $relativePath, $stream)
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...
27
    {
28
        $this->logger->debug("Writing stream to {$relativePath}...");
29
30
        try
31
        {
32
            $success = $this->filesystem->putStream($relativePath, $stream);
33
34
            if (!$success)
35
            {
36
                throw new Exception(sprintf('writeStream() failed for %s.', $relativePath));
37
            }
38
        }
39
        catch (\Exception $exception)
40
        {
41
            throw new Exception($exception->getMessage(), 0, $exception);
42
        }
43
    }
44
45
    public function exists(string $relativePath): bool
46
    {
47
        try
48
        {
49
            return $this->filesystem->has($relativePath);
50
        }
51
        catch (\Exception $exception)
52
        {
53
            throw new Exception($exception->getMessage(), 0, $exception);
54
        }
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
        $this->logger->debug("Deleting {$relativePath}...");
60
61
        try
62
        {
63
            $success = $this->filesystem->delete($relativePath);
64
65
            if (!$success)
66
            {
67
                throw new Exception(sprintf('unlink() failed for %s', $relativePath));
68
            }
69
        }
70
        catch (\Exception $exception)
71
        {
72
            throw new Exception($exception->getMessage(), 0, $exception);
73
        }
74
    }
75
76 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...
77
    {
78
        $this->logger->debug("Getting read stream for {$relativePath}...");
79
80
        try
81
        {
82
            $stream = $this->filesystem->readStream($relativePath);
83
84
            if (!is_resource($stream))
85
            {
86
                throw new Exception(sprintf('getReadStream() failed for %s.', $relativePath));
87
            }
88
89
            return $stream;
90
        }
91
        catch (\Exception $exception)
92
        {
93
            throw new Exception($exception->getMessage(), 0, $exception);
94
        }
95
    }
96
}
97