Completed
Push — guzzle-5 ( 6a2a51...ce0964 )
by Chris
06:48
created

GuzzleAdapter   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 307
Duplicated Lines 11.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 40
c 2
b 0
f 1
lcom 1
cbo 4
dl 34
loc 307
ccs 101
cts 101
cp 1
rs 8.2609

23 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 6 24 7
A getBaseUrl() 0 4 1
A copy() 0 4 1
A createDir() 0 4 1
A delete() 0 4 1
A deleteDir() 0 4 1
B getMetadata() 0 26 4
A getMimetype() 0 4 1
A getSize() 0 4 1
A getTimestamp() 0 4 1
A getVisibility() 0 7 1
A has() 0 8 2
A listContents() 0 4 1
A read() 0 11 2
A readStream() 0 11 2
A rename() 0 4 1
A setVisibility() 0 8 2
A update() 0 4 1
A updateStream() 0 4 1
A write() 0 4 1
A writeStream() 0 4 1
A get() 14 14 3
A head() 14 14 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like GuzzleAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GuzzleAdapter, and based on these observations, apply Extract Interface, too.

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