Completed
Push — master ( d566b3...f02f0c )
by Nikita
01:22
created

GameapAdapter::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Knik\Flysystem\Gameap;
4
5
use League\Flysystem\Config;
6
use League\Flysystem\Util;
7
use InvalidArgumentException;
8
use League\Flysystem\AdapterInterface;
9
10
class GameapAdapter extends GameapAbstractAdapter
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $configurable = [
16
        'host',
17
        'port',
18
        'username',
19
        'password',
20
        'privateKey',
21
        'privateKeyPass',
22
        'timeout',
23
        'root',
24
    ];
25
26
    /**
27
     * @return bool
28
     * @throws \Exception
29
     */
30 66
    public function isConnected()
31
    {
32 66
        return !empty($this->connection);
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38 3
    public function write($path, $contents, Config $config)
39
    {
40 3
        if (! $this->upload($path, $contents, $config)) {
41 3
            return false;
42
        }
43
44 3
        return compact('contents', 'path');
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 3
    public function writeStream($path, $resource, Config $config)
51
    {
52 3
        if (! $this->upload($path, $resource, $config)) {
53 3
            return false;
54
        }
55
56 3
        return compact( 'path');
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 3
    public function update($path, $contents, Config $config)
63
    {
64 3
        return $this->upload($path, $contents, $config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->upload($path, $contents, $config); (resource|boolean) is incompatible with the return type declared by the interface League\Flysystem\AdapterInterface::update of type array|false.

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...
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 3
    public function updateStream($path, $resource, Config $config)
71
    {
72 3
        return $this->upload($path, $resource, $config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->upload($path, $resource, $config); (resource|boolean) is incompatible with the return type declared by the interface League\Flysystem\AdapterInterface::updateStream of type array|false.

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...
73
    }
74
75
    /**
76
     * Upload a file.
77
     *
78
     * @param string          $path
79
     * @param string|resource $contents
80
     * @param Config          $config
81
     * @return bool
82
     */
83 15
    public function upload($path, $contents, Config $config)
84
    {
85 15
        $path = $this->applyPathPrefix($path);
86
87 15
        $config = Util::ensureConfig($config);
88
89 15
        $visibility = $config->get('visibility') ?: 'public';
90 15
        $permissions = $this->{'perm' . ucfirst($visibility)};
91
92 15
        if (is_resource($contents)) {
93 6
            $result = $this->getConnection()->put($contents, $path, $permissions);
94 11
        } else if (is_string($contents)) {
95 6
            $fp = fopen('php://temp', 'w+b');
96 6
            fwrite($fp, $contents);
97 6
            rewind($fp);
98 6
            $result = $this->getConnection()->put($fp, $path, $permissions);
99 2
        } else {
100 3
            throw new InvalidArgumentException('Invalid contents');
101
        }
102
103 12
        return $result;
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 3
    public function rename($path, $newpath)
110
    {
111 3
        $path = $this->applyPathPrefix($path);
112 3
        $newpath = $this->applyPathPrefix($newpath);
113
114 3
        $this->getConnection()->rename($path, $newpath);
115
116 3
        return true;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 3
    public function copy($path, $newpath)
123
    {
124 3
        $path = $this->applyPathPrefix($path);
125 3
        $newpath = $this->applyPathPrefix($newpath);
126
127 3
        return $this->getConnection()->copy($path, $newpath);
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133 9
    public function delete($path)
134
    {
135 9
        $path = $this->applyPathPrefix($path);
136
137 9
        return $this->getConnection()->delete($path, true);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143 3
    public function deleteDir($dirname)
144
    {
145 3
        return $this->delete($dirname);
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 4
    public function createDir($dirname, Config $config)
152
    {
153 3
        $dirname = $this->applyPathPrefix($dirname);
154
155 3
        $config = Util::ensureConfig($config);
156
157 4
        $visibility = $config->get('visibility') ?: 'public';
158 3
        $permissions = $this->{'perm'.ucfirst($visibility)};
159
160 3
        $this->getConnection()->mkdir($dirname, $permissions);
161
162 3
        return ['path' => $dirname];
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168 6
    public function setVisibility($path, $visibility)
169
    {
170 6
        $location = $this->applyPathPrefix($path);
171
172 6
        $visibility = ucfirst($visibility);
173
174 6
        if (! isset($this->{'perm'.$visibility})) {
175 3
            throw new InvalidArgumentException('Unknown visibility: '.$visibility);
176
        }
177
178 3
        $permissions = $this->{'perm'.$visibility};
179
180 3
        if ($this->getConnection()->chmod($permissions, $location)) {
181 3
            return compact('path', 'visibility');
182
        } else {
183 3
            return false;
184
        }
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190 36
    public function has($path)
191
    {
192 36
        $fullPath = $this->applyPathPrefix($path);
193 36
        return $this->getConnection()->exist($fullPath);
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199 6
    public function read($path)
200
    {
201 6
        $object = $this->readStream($path);
202 6
        $object['contents'] = stream_get_contents($object['stream']);
203 6
        fclose($object['stream']);
204 6
        unset($object['stream']);
205
206 6
        return $object;
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212 6
    public function readStream($path)
213
    {
214 6
        $location = $this->applyPathPrefix($path);
215 6
        $fileHandle = fopen('php://temp', 'w+b');
216 6
        $stream = $this->getConnection()->get($location, $fileHandle);
217
218 6
        return compact('stream', 'path');
219
    }
220
221
222
    /**
223
     * @inheritdoc
224
     */
225 3
    public function listContents($directory = '', $recursive = false)
226
    {
227 3
        $directory = $this->applyPathPrefix($directory);
228 3
        $listing = $this->getConnection()->directoryContents($directory);
229
230 3
        $result = [];
231 3
        foreach ($listing as $object) {
232 3
            $path = empty($directory) ? $object['name'] : $this->applyPathPrefix($object['name']);
233 3
            $result[] = $this->normalizeListingObject($path, $object);
234
235 3
            if ($recursive && isset($object['type']) && $object['type'] === 'dir') {
236 2
                $result = array_merge($result, $this->listContents($path));
237
            }
238 1
        }
239
240 3
        return $result;
241
    }
242
243
244
    /**
245
     * @inheritdoc
246
     */
247 12
    public function getMetadata($path)
248
    {
249 12
        $fullPath = $this->applyPathPrefix($path);
250
251 12
        $meta = $this->getConnection()->metadata($fullPath);
252
253
        return [
254 12
            'path' => $path,
255 12
            'size' => $meta['size'],
256 12
            'type' => $meta['type'],
257 12
            'timestamp' => $meta['mtime'],
258 12
            'visibility' => $meta['permissions'] & (0777 ^ $this->permPrivate) ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE,
259 12
            'mimetype' => $meta['mimetype'],
260 4
        ];
261
    }
262
263
264
    /**
265
     * @inheritdoc
266
     */
267 3
    public function getSize($path)
268
    {
269 3
        return $this->getMetadata($path);
270
    }
271
272
273
    /**
274
     * @inheritdoc
275
     */
276 3
    public function getMimetype($path)
277
    {
278 3
        $fullPath = $this->applyPathPrefix($path);
279 3
        $meta = $this->getConnection()->metadata($fullPath);
280 3
        $mimetype = $meta['mimetype'];
281
282 3
        if (empty($mimetype)) {
283 3
            $data = $this->read($path);
284 3
            $mimetype = Util::guessMimeType($path, $data['contents']);
285 1
        }
286
287 3
        return compact('mimetype', 'path');
288
    }
289
290
291
    /**
292
     * @inheritdoc
293
     */
294 3
    public function getTimestamp($path)
295
    {
296 3
        return $this->getMetadata($path);
297
    }
298
299
300
    /**
301
     * @inheritdoc
302
     */
303 6
    public function getVisibility($path)
304
    {
305 6
        return $this->getMetadata($path);
306
    }
307
}