Completed
Push — master ( 9cb330...46f907 )
by Timur
01:52
created

SelectelAdapter   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 32
c 2
b 1
f 0
lcom 2
cbo 4
dl 0
loc 265
rs 9.6

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFile() 0 4 1
A transformFiles() 0 15 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
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
            ];
64
        }
65
66
        return $result;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function has($path)
73
    {
74
        return $this->container->files()->exists($path);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function read($path)
81
    {
82
        try {
83
            $file = $this->getFile($path);
84
        } catch (FileNotFoundException $e) {
85
            return false;
86
        }
87
88
        $contents = $file->read();
89
90
        return compact('contents');
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function readStream($path)
97
    {
98
        try {
99
            $file = $this->getFile($path);
100
        } catch (FileNotFoundException $e) {
101
            return false;
102
        }
103
104
        $stream = $file->readStream();
105
106
        rewind($stream);
107
108
        return compact('stream');
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function listContents($directory = '', $recursive = false)
115
    {
116
        $files = $this->container->files()->withPrefix($directory)->get();
117
        $result = $this->transformFiles($files);
118
119
        return $result;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getMetadata($path)
126
    {
127
        $files = $this->listContents($path);
128
129
        return isset($files[0]) ? $files[0] : false;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getSize($path)
136
    {
137
        return $this->getMetadata($path);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getMimetype($path)
144
    {
145
        return $this->getMetadata($path);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getTimestamp($path)
152
    {
153
        return $this->getMetadata($path);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function write($path, $contents, Config $config)
160
    {
161
        return $this->writeToContainer('String', $path, $contents);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function writeStream($path, $resource, Config $config)
168
    {
169
        return $this->writeToContainer('Stream', $path, $resource);
170
    }
171
172
    /**
173
     * Writes string or stream to container.
174
     *
175
     * @param string          $type    Upload type
176
     * @param string          $path    File path
177
     * @param string|resource $payload String content or Stream resource
178
     *
179
     * @return array|bool
180
     */
181
    protected function writeToContainer($type, $path, $payload)
182
    {
183
        try {
184
            $this->container->{'uploadFrom'.$type}($path, $payload);
185
        } catch (UploadFailedException $e) {
186
            return false;
187
        }
188
189
        return $this->getMetadata($path);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function update($path, $contents, Config $config)
196
    {
197
        return $this->write($path, $contents, $config);
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function updateStream($path, $resource, Config $config)
204
    {
205
        return $this->writeStream($path, $resource, $config);
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function rename($path, $newpath)
212
    {
213
        try {
214
            $this->getFile($path)->rename($newpath);
215
        } catch (ApiRequestFailedException $e) {
216
            return false;
217
        }
218
219
        return true;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function copy($path, $newpath)
226
    {
227
        try {
228
            $this->getFile($path)->copy($newpath);
229
        } catch (ApiRequestFailedException $e) {
230
            return false;
231
        }
232
233
        return true;
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function delete($path)
240
    {
241
        try {
242
            $this->getFile($path)->delete();
243
        } catch (ApiRequestFailedException $e) {
244
            return false;
245
        }
246
247
        return true;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function deleteDir($path)
254
    {
255
        try {
256
            $this->container->deleteDir($path);
257
        } catch (ApiRequestFailedException $e) {
258
            return false;
259
        }
260
261
        return true;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function createDir($dirname, Config $config)
268
    {
269
        try {
270
            $this->container->createDir($dirname);
271
        } catch (ApiRequestFailedException $e) {
272
            return false;
273
        }
274
275
        return $this->getMetadata($dirname);
276
    }
277
}
278