Completed
Push — master ( 65b5b4...2290e5 )
by Arne
05:25
created

FlysystemStorageAdapter::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 16
Ratio 100 %

Importance

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