Completed
Push — master ( 654ea3...822960 )
by Ramesh
11s
created

BackblazeAdapter::listContents()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 5.3563
cc 9
eloc 19
nc 5
nop 2
1
<?php
2
3
namespace Mhetreramesh\Flysystem;
4
5
use ChrisWhite\B2\Client;
6
use League\Flysystem\Adapter\AbstractAdapter;
7
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
8
use League\Flysystem\Config;
9
use GuzzleHttp\Psr7;
10
11
class BackblazeAdapter extends AbstractAdapter {
12
13
    use NotSupportingVisibilityTrait;
14
15
    protected $client;
16
17
    protected $bucketName;
18
19
    public function __construct(Client $client, $bucketName)
20
    {
21
        $this->client = $client;
22
        $this->bucketName = $bucketName;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function has($path)
29
    {
30
        return $this->getClient()->fileExists(['FileName' => $path, 'BucketName' => $this->bucketName]);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function write($path, $contents, Config $config)
37
    {
38
        $file = $this->getClient()->upload([
39
            'BucketName' => $this->bucketName,
40
            'FileName' => $path,
41
            'Body' => $contents
42
        ]);
43
        return $this->getFileInfo($file);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function writeStream($path, $resource, Config $config)
50
    {
51
        $file = $this->getClient()->upload([
52
            'BucketName' => $this->bucketName,
53
            'FileName' => $path,
54
            'Body' => $resource
55
        ]);
56
        return $this->getFileInfo($file);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function update($path, $contents, Config $config)
63
    {
64
        $file = $this->getClient()->upload([
65
            'BucketName' => $this->bucketName,
66
            'FileName' => $path,
67
            'Body' => $contents
68
        ]);
69
        return $this->getFileInfo($file);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function updateStream($path, $resource, Config $config)
76
    {
77
        $file = $this->getClient()->upload([
78
            'BucketName' => $this->bucketName,
79
            'FileName' => $path,
80
            'Body' => $resource
81
        ]);
82
        return $this->getFileInfo($file);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function read($path)
89
    {
90
        $file = $this->getClient()->getFile([
91
            'BucketName' => $this->bucketName,
92
            'FileName' => $path
93
        ]);
94
        $fileContent = $this->getClient()->download([
95
            'FileId' => $file->getId()
96
        ]);
97
        return ['contents' => $fileContent];
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function readStream($path)
104
    {
105
        $stream = Psr7\stream_for();
106
        $download = $this->getClient()->download([
107
            'BucketName' => $this->bucketName,
108
            'FileName' => $path,
109
            'SaveAs' => $stream,
110
        ]);
111
        $stream->seek(0);
112
        try {
113
            $resource = Psr7\StreamWrapper::getResource($stream);
114
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Mhetreramesh\Flysystem\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
115
            return false;
116
        }
117
        return $download === true ? ['stream' => $resource] : false;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function rename($path, $newpath)
124
    {
125
        return false;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function copy($path, $newPath)
132
    {
133
        return $this->getClient()->upload([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getClient(..._get_contents($path))); (ChrisWhite\B2\File) is incompatible with the return type declared by the interface League\Flysystem\AdapterInterface::copy of type boolean.

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...
134
            'BucketName' => $this->bucketName,
135
            'FileName' => $newPath,
136
            'Body' => @file_get_contents($path)
137
        ]);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function delete($path)
144
    {
145
        return $this->getClient()->deleteFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function deleteDir($path)
152
    {
153
        return $this->getClient()->deleteFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function createDir($path, Config $config)
160
    {
161
        return $this->getClient()->upload([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getClient(... $path, 'Body' => '')); (ChrisWhite\B2\File) is incompatible with the return type declared by the interface League\Flysystem\AdapterInterface::createDir 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...
162
            'BucketName' => $this->bucketName,
163
            'FileName' => $path,
164
            'Body' => ''
165
        ]);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getMetadata($path)
172
    {
173
        return false;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function getMimetype($path)
180
    {
181
        return false;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function getSize($path)
188
    {
189
        $file = $this->getClient()->getFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
190
191
        return $this->getFileInfo($file);
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getTimestamp($path)
198
    {
199
        $file = $this->getClient()->getFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
200
201
        return $this->getFileInfo($file);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function getClient()
208
    {
209
        return $this->client;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function listContents($directory = '', $recursive = false)
216
    {
217
        $fileObjects = $this->getClient()->listFiles([
218
            'BucketName' => $this->bucketName,
219
        ]);
220
        if ($recursive === true && $directory === '') {
221
            $regex = '/^.*$/';
222
        } else if ($recursive === true && $directory !== '') {
223
            $regex = '/^' . preg_quote($directory) . '\/.*$/';
224
        } else if ($recursive === false && $directory === '') {
225
            $regex = '/^(?!.*\\/).*$/';
226
        } else if ($recursive === false && $directory !== '') {
227
            $regex = '/^' . preg_quote($directory) . '\/(?!.*\\/).*$/';
228
        } else {
229
            throw new \InvalidArgumentException();
230
        }
231
        $fileObjects = array_filter($fileObjects, function ($fileObject) use ($directory, $regex) {
232
            return 1 === preg_match($regex, $fileObject->getName());
233
        });
234
        $normalized = array_map(function ($fileObject) {
235
            return $this->getFileInfo($fileObject);
236
        }, $fileObjects);
237
        return array_values($normalized);
238
    }
239
240
    /**
241
     * Get file info
242
     *
243
     * @param $file
244
     *
245
     * @return array
246
     */
247
248
    protected function getFileInfo($file)
249
    {
250
        $normalized = [
251
            'type' => 'file',
252
            'path' => $file->getName(),
253
            'timestamp' => substr($file->getUploadTimestamp(), 0, -3),
254
            'size' => $file->getSize()
255
        ];
256
257
        return $normalized;
258
    }
259
}
260