Completed
Branch guzzle-5 (ec0a50)
by Chris
05:02 queued 01:23
created

GuzzleAdapter::has()   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 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 GuzzleHttp\Message\Response;
10
use League\Flysystem\AdapterInterface;
11
use League\Flysystem\Config;
12
use League\Flysystem\Util\MimeType;
13
14
/**
15
 * Uses Guzzle as a backend for HTTP URLs.
16
 */
17
class GuzzleAdapter implements AdapterInterface
18
{
19
    /**
20
     * Whether this endpoint supports HEAD requests.
21
     *
22
     * @var bool
23
     */
24
    protected $supportsHead = true;
25
26
    /**
27
     * The base URL.
28
     *
29
     * @var string
30
     */
31
    protected $base;
32
33
    /**
34
     * The Guzzle HTTP client.
35
     *
36
     * @var \GuzzleHttp\ClientInterface
37
     */
38
    protected $client;
39
40
    /**
41
     * The visibility of this adapter.
42
     *
43
     * @var string
44
     */
45
    protected $visibility = AdapterInterface::VISIBILITY_PUBLIC;
46
47
    /**
48
     * Constructs a GuzzleAdapter object.
49
     *
50
     * @param string                      $base         The base URL.
51
     * @param \GuzzleHttp\ClientInterface $client       An optional Guzzle client.
52
     * @param bool                        $supportsHead Whether the client supports HEAD requests.
53
     */
54 3
    public function __construct($base, ClientInterface $client = null, $supportsHead = true)
55
    {
56 3
        $this->client = $client ?: new Client();
57 3
        $this->supportsHead = $supportsHead;
58
59 3
        $parsed = parse_url($base);
60 3
        $this->base = $parsed['scheme'] . '://';
61
62 3
        if (isset($parsed['user'])) {
63 3
            $this->visibility = AdapterInterface::VISIBILITY_PRIVATE;
64 3
            $this->base .= $parsed['user'];
65
66 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...
67 3
                $this->base .= ':' . $parsed['pass'];
68 3
            }
69
70 3
            $this->base .= '@';
71 3
        };
72
73 3
        $this->base .= $parsed['host'] . '/';
74
75 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...
76 3
            $this->base .= trim($parsed['path'], '/') . '/';
77 3
        }
78 3
    }
79
80
    /**
81
     * Returns the base URL.
82
     *
83
     * @return string The base URL.
84
     */
85 3
    public function getBaseUrl()
86
    {
87 3
        return $this->base;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 3
    public function copy($path, $newpath)
94
    {
95 3
        return false;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 3
    public function createDir($path, Config $config)
102
    {
103 3
        return false;
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 3
    public function delete($path)
110
    {
111 3
        return false;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 3
    public function deleteDir($path)
118
    {
119 3
        return false;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125 3
    public function getMetadata($path)
126
    {
127 3
        if (! $response = $this->head($path)) {
128 3
            return false;
129
        }
130
131 3
        if ($mimetype = $response->getHeader('Content-Type')) {
132 3
            list($mimetype) = explode(';', $mimetype, 2);
133 3
            $mimetype = trim($mimetype);
134 3
        } else {
135
            // Remove any query strings or fragments.
136 3
            list($path) = explode('#', $path, 2);
137 3
            list($path) = explode('?', $path, 2);
138 3
            $extension = pathinfo($path, PATHINFO_EXTENSION);
139 3
            $mimetype = $extension ? MimeType::detectByFileExtension($extension) : 'text/plain';
140
        }
141
142
        return [
143 3
            'type' => 'file',
144 3
            'path' => $path,
145 3
            'timestamp' => (int) strtotime($response->getHeader('Last-Modified')),
146 3
            'size' => (int) $response->getHeader('Content-Length'),
147 3
            'visibility' => $this->visibility,
148 3
            'mimetype' => $mimetype,
149 3
        ];
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155 3
    public function getMimetype($path)
156
    {
157 3
        return $this->getMetadata($path);
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163 3
    public function getSize($path)
164
    {
165 3
        return $this->getMetadata($path);
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171 3
    public function getTimestamp($path)
172
    {
173 3
        return $this->getMetadata($path);
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179 3
    public function getVisibility($path)
180
    {
181
        return [
182 3
            'path' => $path,
183 3
            'visibility' => $this->visibility,
184 3
        ];
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190 3
    public function has($path)
191
    {
192 3
        return (bool) $this->head($path);
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198 3
    public function listContents($directory = '', $recursive = false)
199
    {
200 3
        return [];
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206 3
    public function read($path)
207
    {
208 3
        if (! $response = $this->get($path)) {
209 3
            return false;
210
        }
211
212
        return [
213 3
            'path' => $path,
214 3
            'contents' => (string) $response->getBody(),
215 3
        ];
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221 3
    public function readStream($path)
222
    {
223 3
        if (! $response = $this->get($path)) {
224 3
            return false;
225
        }
226
227
        return [
228 3
            'path' => $path,
229 3
            'stream' => $response->getBody()->detach(),
230 3
        ];
231
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236 3
    public function rename($path, $newpath)
237
    {
238 3
        return false;
239
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244 3
    public function setVisibility($path, $visibility)
245
    {
246 3
        if ($visibility === $this->visibility) {
247 3
            return $this->getVisibility($path);
248
        }
249
250 3
        return false;
251
    }
252
253
    /**
254
     * @inheritdoc
255
     */
256 3
    public function update($path, $contents, Config $conf)
257
    {
258 3
        return false;
259
    }
260
261
    /**
262
     * @inheritdoc
263
     */
264 3
    public function updateStream($path, $resource, Config $config)
265
    {
266 3
        return false;
267
    }
268
269
    /**
270
     * @inheritdoc
271
     */
272 3
    public function write($path, $contents, Config $config)
273
    {
274 3
        return false;
275
    }
276
277
    /**
278
     * @inheritdoc
279
     */
280 3
    public function writeStream($path, $resource, Config $config)
281
    {
282 3
        return false;
283
    }
284
285
    /**
286
     * Performs a GET request.
287
     *
288
     * @param string $path The path to GET.
289
     *
290
     * @return \GuzzleHttp\Message\Response|false The response or false if failed.
291
     */
292 6
    protected function get($path)
293
    {
294
        try {
295 6
            $response = $this->client->get($this->base . $path);
296 6
        } catch (BadResponseException $e) {
297 6
            return false;
298
        }
299
300 6
        if ((int) $response->getStatusCode() !== 200) {
301 3
            return false;
302
        }
303
304 6
        return $response;
305
    }
306
307
308
    /**
309
     * Performs a HEAD request.
310
     *
311
     * @param string $path The path to HEAD.
312
     *
313
     * @return \GuzzleHttp\Message\Response|false The response or false if failed.
314
     */
315 6
    protected function head($path)
316
    {
317 6
        if (! $this->supportsHead) {
318 3
            return $this->get($path);
319
        }
320
321
        try {
322 6
            $response = $this->client->head($this->base . $path);
323 6
        } catch (ClientException $e) {
324 6
            if ((int) $e->getResponse()->getStatusCode() === 405) {
325 3
                $this->supportsHead = false;
326
327 3
                return $this->get($path);
328
            }
329
330 3
            return false;
331 3
        } catch (BadResponseException $e) {
332 3
            return false;
333
        }
334
335 6
        if ((int) $response->getStatusCode() !== 200) {
336 3
            return false;
337
        }
338
339 6
        return $response;
340
    }
341
}
342