SelectelAdapter   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 33
c 3
b 1
f 0
lcom 2
cbo 4
dl 0
loc 278
rs 9.3999

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFile() 0 4 1
A transformFiles() 0 16 3
A has() 0 4 1
A read() 0 12 2
A readStream() 0 14 2
A listContents() 0 7 1
A getMetadata() 0 6 2
A getSize() 0 4 1
A getMimetype() 0 4 1
A getTimestamp() 0 4 1
A write() 0 4 1
A writeStream() 0 4 1
A writeToContainer() 0 10 2
A update() 0 4 1
A updateStream() 0 4 1
A rename() 0 10 2
A copy() 0 10 2
A delete() 0 10 2
A deleteDir() 0 10 2
A createDir() 0 10 2
A getUrl() 0 4 1
1
<?php
2
3
namespace ArgentCrusade\Flysystem\Selectel;
4
5
use League\Flysystem\Config;
6
use League\Flysystem\AdapterInterface;
7
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
8
use ArgentCrusade\Selectel\CloudStorage\Contracts\ContainerContract;
9
use ArgentCrusade\Selectel\CloudStorage\Exceptions\FileNotFoundException;
10
use ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException;
11
use ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException;
12
13
class SelectelAdapter implements AdapterInterface
14
{
15
    use NotSupportingVisibilityTrait;
16
17
    /**
18
     * Storage container.
19
     *
20
     * @var \ArgentCrusade\Selectel\CloudStorage\Contracts\ContainerContract
21
     */
22
    protected $container;
23
24
    /**
25
     * Create new instance.
26
     *
27
     * @param \ArgentCrusade\Selectel\CloudStorage\Contracts\ContainerContract $container
28
     */
29
    public function __construct(ContainerContract $container)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * Loads file from container.
36
     *
37
     * @param string $path Path to file.
38
     *
39
     * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract
40
     */
41
    protected function getFile($path)
42
    {
43
        return $this->container->files()->find($path);
44
    }
45
46
    /**
47
     * Transforms internal files array to Flysystem-compatible one.
48
     *
49
     * @param array $files Original Selectel's files array.
50
     *
51
     * @return array
52
     */
53
    protected function transformFiles($files)
54
    {
55
        $result = [];
56
57
        foreach ($files as $file) {
58
            $result[] = [
59
                'type' => $file['content_type'] === 'application/directory' ? 'dir' : 'file',
60
                'path' => $file['name'],
61
                'size' => intval($file['bytes']),
62
                'timestamp' => strtotime($file['last_modified']),
63
                'mimetype' => $file['content_type'],
64
            ];
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function has($path)
74
    {
75
        return $this->container->files()->exists($path);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function read($path)
82
    {
83
        try {
84
            $file = $this->getFile($path);
85
        } catch (FileNotFoundException $e) {
86
            return false;
87
        }
88
89
        $contents = $file->read();
90
91
        return compact('contents');
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function readStream($path)
98
    {
99
        try {
100
            $file = $this->getFile($path);
101
        } catch (FileNotFoundException $e) {
102
            return false;
103
        }
104
105
        $stream = $file->readStream();
106
107
        rewind($stream);
108
109
        return compact('stream');
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function listContents($directory = '', $recursive = false)
116
    {
117
        $files = $this->container->files()->withPrefix($directory)->get();
118
        $result = $this->transformFiles($files);
119
120
        return $result;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getMetadata($path)
127
    {
128
        $files = $this->listContents($path);
129
130
        return isset($files[0]) ? $files[0] : false;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getSize($path)
137
    {
138
        return $this->getMetadata($path);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getMimetype($path)
145
    {
146
        return $this->getMetadata($path);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getTimestamp($path)
153
    {
154
        return $this->getMetadata($path);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function write($path, $contents, Config $config)
161
    {
162
        return $this->writeToContainer('String', $path, $contents);
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function writeStream($path, $resource, Config $config)
169
    {
170
        return $this->writeToContainer('Stream', $path, $resource);
171
    }
172
173
    /**
174
     * Writes string or stream to container.
175
     *
176
     * @param string          $type    Upload type
177
     * @param string          $path    File path
178
     * @param string|resource $payload String content or Stream resource
179
     *
180
     * @return array|bool
181
     */
182
    protected function writeToContainer($type, $path, $payload)
183
    {
184
        try {
185
            $this->container->{'uploadFrom'.$type}($path, $payload);
186
        } catch (UploadFailedException $e) {
187
            return false;
188
        }
189
190
        return $this->getMetadata($path);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function update($path, $contents, Config $config)
197
    {
198
        return $this->write($path, $contents, $config);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function updateStream($path, $resource, Config $config)
205
    {
206
        return $this->writeStream($path, $resource, $config);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function rename($path, $newpath)
213
    {
214
        try {
215
            $this->getFile($path)->rename($newpath);
216
        } catch (ApiRequestFailedException $e) {
217
            return false;
218
        }
219
220
        return true;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function copy($path, $newpath)
227
    {
228
        try {
229
            $this->getFile($path)->copy($newpath);
230
        } catch (ApiRequestFailedException $e) {
231
            return false;
232
        }
233
234
        return true;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function delete($path)
241
    {
242
        try {
243
            $this->getFile($path)->delete();
244
        } catch (ApiRequestFailedException $e) {
245
            return false;
246
        }
247
248
        return true;
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254
    public function deleteDir($path)
255
    {
256
        try {
257
            $this->container->deleteDir($path);
258
        } catch (ApiRequestFailedException $e) {
259
            return false;
260
        }
261
262
        return true;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268
    public function createDir($dirname, Config $config)
269
    {
270
        try {
271
            $this->container->createDir($dirname);
272
        } catch (ApiRequestFailedException $e) {
273
            return false;
274
        }
275
276
        return $this->getMetadata($dirname);
277
    }
278
279
    /**
280
     * Get full URL to given path.
281
     *
282
     * @param string $path = ''
283
     *
284
     * @return string
285
     */
286
    public function getUrl($path = '')
287
    {
288
        return $this->container->url($path);
289
    }
290
}
291