Completed
Push — master ( 09f332...b1ca1d )
by Timur
01:48
created

SelectelAdapter::writeToContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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