Completed
Branch guzzle-3 (354c42)
by Chris
08:55 queued 04:55
created

GuzzleAdapter::deleteDir()   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 1
crap 1
1
<?php
2
3
namespace Twistor\Flysystem;
4
5
use Guzzle\Http\Client;
6
use Guzzle\Http\ClientInterface;
7
use Guzzle\Http\Exception\BadResponseException;
8
use Guzzle\Http\Exception\ClientErrorResponseException;
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 \Guzzle\Http\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 \Guzzle\Http\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(';', $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
        return [
142 3
            'type' => 'file',
143 3
            'path' => $path,
144 3
            'timestamp' => (int) strtotime($response->getHeader('Last-Modified')),
145 3
            'size' => (int) trim($response->getHeader('Content-Length')),
146 3
            'visibility' => $this->visibility,
147 3
            'mimetype' => $mimetype,
148 3
        ];
149
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154 3
    public function getMimetype($path)
155
    {
156 3
        return $this->getMetadata($path);
157
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162 3
    public function getSize($path)
163
    {
164 3
        return $this->getMetadata($path);
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170 3
    public function getTimestamp($path)
171
    {
172 3
        return $this->getMetadata($path);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178 3
    public function getVisibility($path)
179
    {
180
        return [
181 3
            'path' => $path,
182 3
            'visibility' => $this->visibility,
183 3
        ];
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 3
    public function has($path)
190
    {
191 3
        return (bool) $this->head($path);
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197 3
    public function listContents($directory = '', $recursive = false)
198
    {
199 3
        return [];
200
    }
201
202
    /**
203
     * @inheritdoc
204
     */
205 3
    public function read($path)
206
    {
207 3
        if (! $response = $this->get($path)) {
208 3
            return false;
209
        }
210
211
        return [
212 3
            'path' => $path,
213 3
            'contents' => (string) $response->getBody(),
214 3
        ];
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220 3
    public function readStream($path)
221
    {
222 3
        if (! $response = $this->get($path)) {
223 3
            return false;
224
        }
225
226
        return [
227 3
            'path' => $path,
228 3
            'stream' => $response->getBody()->getStream(),
229 3
        ];
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235 3
    public function rename($path, $newpath)
236
    {
237 3
        return false;
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243 3
    public function setVisibility($path, $visibility)
244
    {
245 3
        if ($visibility === $this->visibility) {
246 3
            return $this->getVisibility($path);
247
        }
248
249 3
        return false;
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 3
    public function update($path, $contents, Config $conf)
256
    {
257 3
        return false;
258
    }
259
260
    /**
261
     * @inheritdoc
262
     */
263 3
    public function updateStream($path, $resource, Config $config)
264
    {
265 3
        return false;
266
    }
267
268
    /**
269
     * @inheritdoc
270
     */
271 3
    public function write($path, $contents, Config $config)
272
    {
273 3
        return false;
274
    }
275
276
    /**
277
     * @inheritdoc
278
     */
279 3
    public function writeStream($path, $resource, Config $config)
280
    {
281 3
        return false;
282
    }
283
284
    /**
285
     * Performs a GET request.
286
     *
287
     * @param string $path The path to GET.
288
     *
289
     * @return \Guzzle\Http\Message\Response|false The response or false if failed.
290
     */
291 6
    protected function get($path)
292
    {
293
        try {
294 6
            $response = $this->client->get($this->base . $path)->send();
295 6
        } catch (BadResponseException $e) {
296 6
            return false;
297
        }
298
299 6
        if ($response->getStatusCode() !== 200) {
300 3
            return false;
301
        }
302
303 6
        return $response;
304
    }
305
306
    /**
307
     * Performs a HEAD request.
308
     *
309
     * @param string $path The path to HEAD.
310
     *
311
     * @return \Guzzle\Http\Message\Response|false The response or false if failed.
312
     */
313 6
    protected function head($path)
314
    {
315 6
        if (! $this->supportsHead) {
316 3
            return $this->get($path);
317
        }
318
319
        try {
320 6
            $response = $this->client->head($this->base . $path)->send();
321 6
        } catch (ClientErrorResponseException $e) {
322 6
            if ($e->getResponse()->getStatusCode() === 405) {
323 3
                $this->supportsHead = false;
324
325 3
                return $this->get($path);
326
            }
327
328 3
            return false;
329 3
        } catch (BadResponseException $e) {
330 3
            return false;
331
        }
332
333 6
        if ($response->getStatusCode() !== 200) {
334 3
            return false;
335
        }
336
337 6
        return $response;
338
    }
339
}
340