Completed
Push — master ( 17da21...74837e )
by Xiang
02:15
created

WebDAVAdapter::createDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
crap 6
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
    public function __construct(Client $client, $prefix = null)
44
    {
45
        $this->client = $client;
46
        $this->setPathPrefix($prefix);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getMetadata($path)
53
    {
54
        $location = $this->applyPathPrefix($path);
55
56
        try {
57
            $result = $this->client->propFind($location, [
58
                '{DAV:}displayname',
59
                '{DAV:}getcontentlength',
60
                '{DAV:}getcontenttype',
61
                '{DAV:}getlastmodified',
62
            ]);
63
64
            return $this->normalizeObject($result, $path);
65
        } catch (Exception $e) {
66
            return false;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function has($path)
74
    {
75
        $location = $this->applyPathPrefix($path);
76
77
        try {
78
            $response = $this->client->request('HEAD', $location);
79
            return $response['statusCode'] === 200;
80
        } catch (Exception $e) {
81
            return false;
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function read($path)
89
    {
90
        $location = $this->applyPathPrefix($path);
91
92
        try {
93
            $response = $this->client->request('GET', $location);
94
95
            if ($response['statusCode'] !== 200) {
96
                return false;
97
            }
98
99
            return array_merge([
100
                'contents' => $response['body'],
101
                'timestamp' => strtotime(is_array($response['headers']['last-modified'])
102
                    ? current($response['headers']['last-modified'])
103
                    : $response['headers']['last-modified']),
104
                'path' => $path,
105
            ], Util::map($response['headers'], static::$resultMap));
106
        } catch (Exception $e) {
107
            return false;
108
        }
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function write($path, $contents, Config $config)
115
    {
116
        $location = $this->applyPathPrefix($path);
117
        $res = $this->client->request('PUT', $location, $contents);
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
        $result = compact('path', 'contents');
119
120
        if ($config->get('visibility')) {
121
            throw new LogicException(__CLASS__.' does not support visibility settings.');
122
        }
123
124
        return $result;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function update($path, $contents, Config $config)
131
    {
132
        return $this->write($path, $contents, $config);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function rename($path, $newpath)
139
    {
140
        $location = $this->applyPathPrefix($path);
141
        $newLocation = $this->applyPathPrefix($newpath);
142
143
        try {
144
            $response = $this->client->request('MOVE', '/'.ltrim($location, '/'), null, [
145
                'Destination' => '/'.ltrim($newLocation, '/'),
146
            ]);
147
148
            if ($response['statusCode'] >= 200 && $response['statusCode'] < 300) {
149
                return true;
150
            }
151
        } catch (NotFound $e) {
152
            // Would have returned false here, but would be redundant
153
        }
154
155
        return false;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function delete($path)
162
    {
163
        $location = $this->applyPathPrefix($path);
164
165
        try {
166
            $this->client->request('DELETE', $location);
167
168
            return true;
169
        } catch (NotFound $e) {
170
            return false;
171
        }
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function createDir($path, Config $config)
178
    {
179
        $location = $this->applyPathPrefix($path);
180
        $response = $this->client->request('MKCOL', $location);
181
182
        if ($response['statusCode'] !== 201) {
183
            return false;
184
        }
185
186
        return compact('path') + ['type' => 'dir'];
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function deleteDir($dirname)
193
    {
194
        return $this->delete($dirname);
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function listContents($directory = '', $recursive = false)
201
    {
202
        $location = $this->applyPathPrefix($directory);
203
        $response = $this->client->propFind($location, [
204
            '{DAV:}displayname',
205
            '{DAV:}getcontentlength',
206
            '{DAV:}getcontenttype',
207
            '{DAV:}getlastmodified',
208
        ], 1);
209
210
        array_shift($response);
211
        $result = [];
212
213
        foreach ($response as $path => $object) {
214
            $path = $this->removePathPrefix($path);
215
            $object = $this->normalizeObject($object, $path);
216
            $result[] = $object;
217
218
            if ($recursive && $object['type'] === 'dir') {
219
                $result = array_merge($result, $this->listContents($object['path'], true));
220
            }
221
        }
222
223
        return $result;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function getSize($path)
230
    {
231
        return $this->getMetadata($path);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function getTimestamp($path)
238
    {
239
        return $this->getMetadata($path);
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function getMimetype($path)
246
    {
247
        return $this->getMetadata($path);
248
    }
249
250
    /**
251
     * Normalise a WebDAV repsonse object.
252
     *
253
     * @param array  $object
254
     * @param string $path
255
     *
256
     * @return array
257
     */
258
    protected function normalizeObject(array $object, $path)
259
    {
260
        if (! isset($object['{DAV:}getcontentlength'])) {
261
            return ['type' => 'dir', 'path' => trim($path, '/')];
262
        }
263
264
        $result = Util::map($object, static::$resultMap);
265
266
        if (isset($object['{DAV:}getlastmodified'])) {
267
            $result['timestamp'] = strtotime($object['{DAV:}getlastmodified']);
268
        }
269
270
        $result['type'] = 'file';
271
        $result['path'] = trim($path, '/');
272
273
        return $result;
274
    }
275
}
276