GameapAdapter::delete()   A
last analyzed

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