Completed
Push — guzzle-5 ( cd54c4...f90878 )
by Chris
03:49
created

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