Completed
Pull Request — master (#548)
by Albin
05:45
created

Flysystem::rename()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 8.7972
cc 4
eloc 14
nc 4
nop 2
1
<?php
2
3
namespace Gaufrette\Adapter;
4
5
use Gaufrette\Adapter;
6
use Gaufrette\Exception\FileNotFound;
7
use Gaufrette\Exception\StorageFailure;
8
use Gaufrette\Exception\UnsupportedAdapterMethodException;
9
use League\Flysystem\AdapterInterface;
10
use League\Flysystem\FileNotFoundException;
11
use League\Flysystem\Util;
12
13
class Flysystem implements Adapter, ListKeysAware
14
{
15
    /**
16
     * @var AdapterInterface
17
     */
18
    private $adapter;
19
20
    /**
21
     * @var Config
22
     */
23
    private $config;
24
25
    /**
26
     * @param AdapterInterface  $adapter
27
     * @param Config|array|null $config
28
     */
29
    public function __construct(AdapterInterface $adapter, $config = null)
30
    {
31
        $this->adapter = $adapter;
32
        $this->config = Util::ensureConfig($config);
0 ignored issues
show
Documentation Bug introduced by
It seems like \League\Flysystem\Util::ensureConfig($config) of type object<League\Flysystem\Config> is incompatible with the declared type object<Gaufrette\Adapter\Config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 View Code Duplication
    public function read($key)
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...
39
    {
40
        try {
41
            $result = $this->adapter->read($key);
42
        } catch (\Exception $e) {
43
            if ($e instanceof FileNotFoundException) {
44
                throw new FileNotFound($key, $e->getCode(), $e);
45
            }
46
47
            throw StorageFailure::unexpectedFailure('read', ['key' => $key], $e);
48
        }
49
50
        if (false === $result) {
51
            throw StorageFailure::unexpectedFailure('read', ['key' => $key]);
52
        }
53
54
        return $result['contents'];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function write($key, $content)
61
    {
62
        try {
63
            $result = $this->adapter->write($key, $content, $this->config);
64
        } catch (\Exception $e) {
65
            throw StorageFailure::unexpectedFailure(
66
                'write',
67
                ['key' => $key, 'content' => $content],
68
                $e
69
            );
70
        }
71
72
        if (false === $result) {
73
            throw StorageFailure::unexpectedFailure(
74
                'write',
75
                ['key' => $key, 'content' => $content]
76
            );
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function exists($key)
84
    {
85
        try {
86
            return $this->adapter->has($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->adapter->has($key); of type array|boolean|null adds the type array to the return on line 86 which is incompatible with the return type declared by the interface Gaufrette\Adapter::exists of type boolean.
Loading history...
87
        } catch (\Exception $e) {
88
            throw StorageFailure::unexpectedFailure('exists', ['key' => $key], $e);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function keys()
96
    {
97
        try {
98
            return array_map(function ($content) {
99
                return $content['path'];
100
            }, $this->adapter->listContents());
101
        } catch (\Exception $e) {
102
            throw StorageFailure::unexpectedFailure('keys', [], $e);
103
        }
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function listKeys($prefix = '')
110
    {
111
        $dirs = [];
112
        $keys = [];
113
114
        try {
115
            foreach ($this->adapter->listContents() as $content) {
116
                if (empty($prefix) || 0 === strpos($content['path'], $prefix)) {
117
                    if ('dir' === $content['type']) {
118
                        $dirs[] = $content['path'];
119
                    } else {
120
                        $keys[] = $content['path'];
121
                    }
122
                }
123
            }
124
        } catch (\Exception $e) {
125
            throw StorageFailure::unexpectedFailure(
126
                'listKeys',
127
                ['prefix' => $prefix],
128
                $e
129
            );
130
        }
131
132
        return [
133
            'keys' => $keys,
134
            'dirs' => $dirs,
135
        ];
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 View Code Duplication
    public function mtime($key)
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...
142
    {
143
        try {
144
            $result = $this->adapter->getTimestamp($key);
145
        } catch (\Exception $e) {
146
            if ($e instanceof FileNotFoundException) {
147
                throw new FileNotFound($key, $e->getCode(), $e);
148
            }
149
150
            throw StorageFailure::unexpectedFailure('mtime', ['key' => $key], $e);
151
        }
152
153
        if (false === $result) {
154
            throw StorageFailure::unexpectedFailure('mtime', ['key' => $key]);
155
        }
156
157
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Gaufrette\Adapter::mtime of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 View Code Duplication
    public function delete($key)
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...
164
    {
165
        try {
166
            $result = $this->adapter->delete($key);
167
        } catch (\Exception $e) {
168
            if ($e instanceof FileNotFoundException) {
169
                throw new FileNotFound($key, $e->getCode(), $e);
170
            }
171
172
            throw StorageFailure::unexpectedFailure('delete', ['key' => $key], $e);
173
        }
174
175
        if (false === $result) {
176
            throw StorageFailure::unexpectedFailure('delete', ['key' => $key]);
177
        }
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function rename($sourceKey, $targetKey)
184
    {
185
        try {
186
            $result = $this->adapter->rename($sourceKey, $targetKey);
187
        } catch (\Exception $e) {
188
            if ($e instanceof FileNotFoundException) {
189
                throw new FileNotFound($sourceKey, $e->getCode(), $e);
190
            }
191
192
            throw StorageFailure::unexpectedFailure(
193
                'rename',
194
                ['sourceKey' => $sourceKey, 'targetKey' => $targetKey],
195
                $e
196
            );
197
        }
198
199
        if (false === $result) {
200
            throw StorageFailure::unexpectedFailure(
201
                'rename',
202
                ['sourceKey' => $sourceKey, 'targetKey' => $targetKey]
203
            );
204
        }
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function isDirectory($key)
211
    {
212
        throw new UnsupportedAdapterMethodException('isDirectory is not supported by this adapter.');
213
    }
214
}
215