Completed
Push — master ( 2c0930...9cb330 )
by Timur
01:51
created

SelectelAdapter   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 36
c 2
b 1
f 0
lcom 2
cbo 3
dl 0
loc 293
rs 8.8

23 Methods

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