FlysystemStorageAdapter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 65.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 56
loc 85
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A writeStream() 18 18 3
A exists() 0 11 2
A unlink() 18 18 3
A getReadStream() 20 20 3

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 Storeman\StorageAdapter;
4
5
use Storeman\Exception;
6
use League\Flysystem\Filesystem;
7
8
abstract class FlysystemStorageAdapter extends AbstractStorageAdapter
9
{
10
    /**
11
     * @var Filesystem
12
     */
13
    protected $filesystem;
14
15
    public function __construct(Filesystem $filesystem)
16
    {
17
        parent::__construct();
18
19
        $this->filesystem = $filesystem;
20
    }
21
22 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...
23
    {
24
        $this->logger->debug("Writing stream to {$relativePath}...");
25
26
        try
27
        {
28
            $success = $this->filesystem->putStream($relativePath, $stream);
29
30
            if (!$success)
31
            {
32
                throw new Exception(sprintf('writeStream() failed for %s.', $relativePath));
33
            }
34
        }
35
        catch (\Exception $exception)
36
        {
37
            throw new Exception($exception->getMessage(), 0, $exception);
38
        }
39
    }
40
41
    public function exists(string $relativePath): bool
42
    {
43
        try
44
        {
45
            return $this->filesystem->has($relativePath);
46
        }
47
        catch (\Exception $exception)
48
        {
49
            throw new Exception($exception->getMessage(), 0, $exception);
50
        }
51
    }
52
53 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...
54
    {
55
        $this->logger->debug("Deleting {$relativePath}...");
56
57
        try
58
        {
59
            $success = $this->filesystem->delete($relativePath);
60
61
            if (!$success)
62
            {
63
                throw new Exception(sprintf('unlink() failed for %s', $relativePath));
64
            }
65
        }
66
        catch (\Exception $exception)
67
        {
68
            throw new Exception($exception->getMessage(), 0, $exception);
69
        }
70
    }
71
72 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...
73
    {
74
        $this->logger->debug("Getting read stream for {$relativePath}...");
75
76
        try
77
        {
78
            $stream = $this->filesystem->readStream($relativePath);
79
80
            if (!is_resource($stream))
81
            {
82
                throw new Exception(sprintf('getReadStream() failed for %s.', $relativePath));
83
            }
84
85
            return $stream;
86
        }
87
        catch (\Exception $exception)
88
        {
89
            throw new Exception($exception->getMessage(), 0, $exception);
90
        }
91
    }
92
}
93