WebDAVAdapter::getTimestamp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OrangeJuice\Flysystem\WebDAV;
4
5
use League\Flysystem\Adapter\AbstractAdapter;
6
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
7
use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait;
8
use League\Flysystem\Adapter\Polyfill\StreamedTrait;
9
use League\Flysystem\Config;
10
use League\Flysystem\Util;
11
use LogicException;
12
use Sabre\DAV\Client;
13
use Sabre\DAV\Exception;
14
use Sabre\DAV\Exception\NotFound;
15
16
class WebDAVAdapter extends AbstractAdapter
17
{
18
    use StreamedTrait;
19
    use StreamedCopyTrait;
20
    use NotSupportingVisibilityTrait;
21
22
    /**
23
     * @var array
24
     */
25
    protected static $resultMap = [
26
        '{DAV:}getcontentlength' => 'size',
27
        '{DAV:}getcontenttype' => 'mimetype',
28
        'content-length' => 'size',
29
        'content-type' => 'mimetype',
30
    ];
31
32
    /**
33
     * @var Client
34
     */
35
    protected $client;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param Client $client
41
     * @param string $prefix
42
     */
43 66
    public function __construct(Client $client, $prefix = null)
44
    {
45 66
        $this->client = $client;
46 66
        $this->setPathPrefix($prefix);
47 66
    }
48
49
    /**
50
     * Get client instance.
51
     *
52
     * @return Client
53
     */
54
    public function getClient()
55
    {
56
        return $this->client;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 12
    public function getMetadata($path)
63
    {
64 12
        $location = $this->applyPathPrefix($path);
65
66
        try {
67 12
            $result = $this->client->propFind($location, [
68 12
                '{DAV:}displayname',
69 4
                '{DAV:}getcontentlength',
70 4
                '{DAV:}getcontenttype',
71 4
                '{DAV:}getlastmodified',
72 4
            ]);
73
74 12
            return $this->normalizeObject($result, $path);
75
        } catch (Exception $e) {
76
            return false;
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 6
    public function has($path)
84
    {
85 6
        $location = $this->applyPathPrefix($path);
86
87
        try {
88 6
            $response = $this->client->request('HEAD', $location);
89 3
            return $response['statusCode'] === 200;
90 3
        } catch (Exception $e) {
91 3
            return false;
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 15
    public function read($path)
99
    {
100 15
        $location = $this->applyPathPrefix($path);
101
102
        try {
103 15
            $response = $this->client->request('GET', $location);
104
105 12
            if ($response['statusCode'] !== 200) {
106 6
                return false;
107
            }
108
109 6
            return array_merge([
110 6
                'contents' => $response['body'],
111 6
                'timestamp' => strtotime(is_array($response['headers']['last-modified'])
112 4
                    ? current($response['headers']['last-modified'])
113 6
                    : $response['headers']['last-modified']),
114 6
                'path' => $path,
115 6
            ], Util::map($response['headers'], static::$resultMap));
116 3
        } catch (Exception $e) {
117 3
            return false;
118
        }
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 9
    public function write($path, $contents, Config $config)
125
    {
126 9
        $location = $this->applyPathPrefix($path);
127 9
        $this->client->request('PUT', $location, $contents);
128 9
        $result = compact('path', 'contents');
129
130 9
        if ($config->get('visibility')) {
131 3
            throw new LogicException(__CLASS__.' does not support visibility settings.');
132
        }
133
134 6
        return $result;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 3
    public function update($path, $contents, Config $config)
141
    {
142 3
        return $this->write($path, $contents, $config);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 9
    public function rename($path, $newpath)
149
    {
150 9
        $location = $this->applyPathPrefix($path);
151 9
        $newLocation = $this->applyPathPrefix($newpath);
152
153
        try {
154 9
            $response = $this->client->request('MOVE', '/'.ltrim($location, '/'), null, [
155 9
                'Destination' => '/'.ltrim($newLocation, '/'),
156 3
            ]);
157
158 6
            if ($response['statusCode'] >= 200 && $response['statusCode'] < 300) {
159 5
                return true;
160
            }
161 4
        } catch (NotFound $e) {
162
            // Would have returned false here, but would be redundant
163
        }
164
165 6
        return false;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 6
    public function delete($path)
172
    {
173 6
        $location = $this->applyPathPrefix($path);
174
175
        try {
176 6
            $this->client->request('DELETE', $location);
177
178 3
            return true;
179 3
        } catch (NotFound $e) {
180 3
            return false;
181
        }
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 6
    public function createDir($path, Config $config)
188
    {
189 6
        $location = $this->applyPathPrefix($path);
190 6
        $response = $this->client->request('MKCOL', $location);
191
192 6
        if ($response['statusCode'] !== 201) {
193 3
            return false;
194
        }
195
196 3
        return compact('path') + ['type' => 'dir'];
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 6
    public function deleteDir($dirname)
203
    {
204 6
        return $this->delete($dirname);
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 3
    public function listContents($directory = '', $recursive = false)
211
    {
212 3
        $location = $this->applyPathPrefix($directory);
213 3
        $response = $this->client->propFind($location, [
214 3
            '{DAV:}displayname',
215 1
            '{DAV:}getcontentlength',
216 1
            '{DAV:}getcontenttype',
217 1
            '{DAV:}getlastmodified',
218 3
        ], 1);
219
220 3
        array_shift($response);
221 3
        $result = [];
222
223 3
        foreach ($response as $path => $object) {
224 3
            $path = $this->removePathPrefix($path);
225 3
            $object = $this->normalizeObject($object, $path);
226 3
            $result[] = $object;
227
228 3
            if ($recursive && $object['type'] === 'dir') {
229 3
                $result = array_merge($result, $this->listContents($object['path'], true));
230 1
            }
231 1
        }
232
233 3
        return $result;
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239 3
    public function getSize($path)
240
    {
241 3
        return $this->getMetadata($path);
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247 3
    public function getTimestamp($path)
248
    {
249 3
        return $this->getMetadata($path);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 3
    public function getMimetype($path)
256
    {
257 3
        return $this->getMetadata($path);
258
    }
259
260
    /**
261
     * Normalise a WebDAV repsonse object.
262
     *
263
     * @param array  $object
264
     * @param string $path
265
     *
266
     * @return array
267
     */
268 15
    protected function normalizeObject(array $object, $path)
269
    {
270 15
        if (! isset($object['{DAV:}getcontentlength'])) {
271 3
            return ['type' => 'dir', 'path' => trim($path, '/')];
272
        }
273
274 15
        $result = Util::map($object, static::$resultMap);
275
276 15
        if (isset($object['{DAV:}getlastmodified'])) {
277 12
            $result['timestamp'] = strtotime($object['{DAV:}getlastmodified']);
278 4
        }
279
280 15
        $result['type'] = 'file';
281 15
        $result['path'] = trim($path, '/');
282
283 15
        return $result;
284
    }
285
}
286