Completed
Branch guzzle-6 (5b50be)
by Chris
01:50
created

GuzzleAdapter::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Twistor\Flysystem;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\BadResponseException;
8
use GuzzleHttp\Exception\ClientException;
9
use League\Flysystem\AdapterInterface;
10
use League\Flysystem\Config;
11
use League\Flysystem\Util\MimeType;
12
13
/**
14
 * Uses Guzzle as a backend for HTTP URLs.
15
 */
16
class GuzzleAdapter implements AdapterInterface
17
{
18
    /**
19
     * Whether this endpoint supports head requests.
20
     *
21
     * @var bool
22
     */
23
    protected $supportsHead = true;
24
25
    /**
26
     * The base URL.
27
     *
28
     * @var string
29
     */
30
    protected $base;
31
32
    /**
33
     * The Guzzle HTTP client.
34
     *
35
     * @var \GuzzleHttp\ClientInterface
36
     */
37
    protected $client;
38
39
    /**
40
     * The visibility of this adapter.
41
     *
42
     * @var string
43
     */
44
    protected $visibility = AdapterInterface::VISIBILITY_PUBLIC;
45
46
    /**
47
     * Constructs a GuzzleAdapter object.
48
     *
49
     * @param string                      $base         The base URL.
50
     * @param \GuzzleHttp\ClientInterface $client       An optional Guzzle client.
51
     * @param bool                        $supportsHead Whether the client supports HEAD requests.
52
     */
53 3
    public function __construct($base, ClientInterface $client = null, $supportsHead = true)
54
    {
55 3
        $this->client = $client ?: new Client();
56 3
        $this->supportsHead = $supportsHead;
57
58 3
        $parsed = parse_url($base);
59 3
        $this->base = $parsed['scheme'] . '://';
60
61 3
        if (isset($parsed['user'])) {
62 3
            $this->visibility = AdapterInterface::VISIBILITY_PRIVATE;
63 3
            $this->base .= $parsed['user'];
64
65 3 View Code Duplication
            if (isset($parsed['pass']) && $parsed['pass'] !== '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66 3
                $this->base .= ':' . $parsed['pass'];
67 3
            }
68
69 3
            $this->base .= '@';
70 3
        };
71
72 3
        $this->base .= $parsed['host'] . '/';
73
74 3 View Code Duplication
        if (isset($parsed['path']) && $parsed['path'] !== '/') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75 3
            $this->base .= trim($parsed['path'], '/') . '/';
76 3
        }
77 3
    }
78
79
    /**
80
     * Returns the base URL.
81
     *
82
     * @return string The base URL.
83
     */
84 3
    public function getBaseUrl()
85
    {
86 3
        return $this->base;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 3
    public function copy($path, $newpath)
93
    {
94 3
        return false;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 3
    public function createDir($path, Config $config)
101
    {
102 3
        return false;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 3
    public function delete($path)
109
    {
110 3
        return false;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 3
    public function deleteDir($path)
117
    {
118 3
        return false;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 3
    public function getMetadata($path)
125
    {
126 3
        if (! $response = $this->head($path)) {
127 3
            return false;
128
        }
129
130 3
        if ($mimetype = $response->getHeader('Content-Type')) {
131 3
            list($mimetype) = explode(';', reset($mimetype), 2);
132 3
            $mimetype = trim($mimetype);
133 3
        } else {
134
            // Remove any query strings or fragments.
135 3
            list($path) = explode('#', $path, 2);
136 3
            list($path) = explode('?', $path, 2);
137 3
            $extension = pathinfo($path, PATHINFO_EXTENSION);
138 3
            $mimetype = $extension ? MimeType::detectByFileExtension($extension) : 'text/plain';
139
        }
140
141 3
        $last_modified = $response->getHeader('Last-Modified');
142 3
        $length = $response->getHeader('Content-Length');
143
144
        return [
145 3
            'type' => 'file',
146 3
            'path' => $path,
147 3
            'timestamp' => (int) strtotime(reset($last_modified)),
148 3
            'size' => (int) reset($length),
149 3
            'visibility' => $this->visibility,
150 3
            'mimetype' => $mimetype,
151 3
        ];
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157 3
    public function getMimetype($path)
158
    {
159 3
        return $this->getMetadata($path);
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165 3
    public function getSize($path)
166
    {
167 3
        return $this->getMetadata($path);
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173 3
    public function getTimestamp($path)
174
    {
175 3
        return $this->getMetadata($path);
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 3
    public function getVisibility($path)
182
    {
183
        return [
184 3
            'path' => $path,
185 3
            'visibility' => $this->visibility,
186 3
        ];
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192 3
    public function has($path)
193
    {
194 3
        return (bool) $this->head($path);
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200 3
    public function listContents($directory = '', $recursive = false)
201
    {
202 3
        return [];
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208 3
    public function read($path)
209
    {
210 3
        if (! $response = $this->get($path)) {
211 3
            return false;
212
        }
213
214
        return [
215 3
            'path' => $path,
216 3
            'contents' => (string) $response->getBody(),
217 3
        ];
218
    }
219
220
    /**
221
     * @inheritdoc
222
     */
223 3
    public function readStream($path)
224
    {
225 3
        if (! $response = $this->get($path)) {
226 3
            return false;
227
        }
228
229
        return [
230 3
            'path' => $path,
231 3
            'stream' => $response->getBody()->detach(),
232 3
        ];
233
    }
234
235
    /**
236
     * @inheritdoc
237
     */
238 3
    public function rename($path, $newpath)
239
    {
240 3
        return false;
241
    }
242
243
    /**
244
     * @inheritdoc
245
     */
246 3
    public function setVisibility($path, $visibility)
247
    {
248 3
        if ($visibility === $this->visibility) {
249 3
            return $this->getVisibility($path);
250
        }
251
252 3
        return false;
253
    }
254
255
    /**
256
     * @inheritdoc
257
     */
258 3
    public function update($path, $contents, Config $conf)
259
    {
260 3
        return false;
261
    }
262
263
    /**
264
     * @inheritdoc
265
     */
266 3
    public function updateStream($path, $resource, Config $config)
267
    {
268 3
        return false;
269
    }
270
271
    /**
272
     * @inheritdoc
273
     */
274 3
    public function write($path, $contents, Config $config)
275
    {
276 3
        return false;
277
    }
278
279
    /**
280
     * @inheritdoc
281
     */
282 3
    public function writeStream($path, $resource, Config $config)
283
    {
284 3
        return false;
285
    }
286
287
    /**
288
     * Performs a GET request.
289
     *
290
     * @param string $path The path to GET.
291
     *
292
     * @return \GuzzleHttp\Psr7\Response|false The response or false if failed.
293
     */
294 6
    protected function get($path)
295
    {
296
        try {
297 6
            $response = $this->client->get($this->base . $path);
298 6
        } catch (BadResponseException $e) {
299 6
            return false;
300
        }
301
302 6
        if ($response->getStatusCode() !== 200) {
303 3
            return false;
304
        }
305
306 6
        return $response;
307
    }
308
309
    /**
310
     * Performs a HEAD request.
311
     *
312
     * @param string $path The path to HEAD.
313
     *
314
     * @return \GuzzleHttp\Psr7\Response|false The response or false if failed.
315
     */
316 6
    protected function head($path)
317
    {
318 6
        if (! $this->supportsHead) {
319 3
            return $this->get($path);
320
        }
321
322
        try {
323 6
            $response = $this->client->head($this->base . $path);
324 6
        } catch (ClientException $e) {
325 6
            if ($e->getResponse()->getStatusCode() === 405) {
326 3
                $this->supportsHead = false;
327
328 3
                return $this->get($path);
329
            }
330
331 3
            return false;
332 3
        } catch (BadResponseException $e) {
333 3
            return false;
334
        }
335
336 6
        if ($response->getStatusCode() !== 200) {
337 3
            return false;
338
        }
339
340 6
        return $response;
341
    }
342
}
343