Passed
Push — master ( fab510...504f5c )
by Pol
02:23
created

Files::putRelativeFile()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 4
b 0
f 0
nc 33
nop 2
dl 0
loc 33
ccs 0
cts 11
cp 0
crap 56
rs 8.5866
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace ChampsLibres\WopiBundle\Controller;
11
12
use ChampsLibres\WopiLib\Service\Contract\WopiInterface;
13
use ChampsLibres\WopiLib\Service\Contract\WopiProofValidatorInterface;
14
use Exception;
15
use loophp\psr17\Psr17Interface;
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\UriInterface;
19
use Throwable;
20
21
use function array_key_exists;
22
23
final class Files
24
{
25
    private Psr17Interface $psr17;
26
27
    private WopiInterface $wopi;
28
29
    private WopiProofValidatorInterface $wopiProofValidator;
30
31
    public function __construct(
32
        WopiInterface $wopi,
33
        WopiProofValidatorInterface $wopiProofValidator,
34
        Psr17Interface $psr17
35
    ) {
36
        $this->wopi = $wopi;
37
        $this->wopiProofValidator = $wopiProofValidator;
38
        $this->psr17 = $psr17;
39
    }
40
41
    public function checkFileInfo(string $fileId, RequestInterface $request): ResponseInterface
42
    {
43
        if (!$this->wopiProofValidator->isValid($request)) {
44
            return $this
45
                ->psr17
46
                ->createResponse(500);
47
        }
48
49
        try {
50
            $checkFileInfo = $this->wopi->checkFileInfo(
51
                $fileId,
52
                $this->getParam($request->getUri(), 'access_token'),
53
                $request
54
            );
55
        } catch (Throwable $e) {
56
            throw $e;
57
        }
58
59
        return $checkFileInfo;
60
    }
61
62
    public function deleteFile(string $fileId, RequestInterface $request): ResponseInterface
63
    {
64
        if (!$this->wopiProofValidator->isValid($request)) {
65
            return $this
66
                ->psr17
67
                ->createResponse(500);
68
        }
69
70
        try {
71
            $deleteFile = $this->wopi->deleteFile(
72
                $fileId,
73
                $this->getParam($request->getUri(), 'access_token'),
74
                $request
75
            );
76
        } catch (Throwable $e) {
77
            throw $e;
78
        }
79
80
        return $deleteFile;
81
    }
82
83
    public function enumerateAncestors(string $fileId, RequestInterface $request): ResponseInterface
84
    {
85
        if (!$this->wopiProofValidator->isValid($request)) {
86
            return $this
87
                ->psr17
88
                ->createResponse(500);
89
        }
90
91
        try {
92
            $enumerateAncestors = $this->wopi->enumerateAncestors(
93
                $fileId,
94
                $this->getParam($request->getUri(), 'access_token'),
95
                $request
96
            );
97
        } catch (Throwable $e) {
98
            throw $e;
99
        }
100
101
        return $enumerateAncestors;
102
    }
103
104
    public function getFile(string $fileId, RequestInterface $request): ResponseInterface
105
    {
106
        if (!$this->wopiProofValidator->isValid($request)) {
107
            return $this
108
                ->psr17
109
                ->createResponse(500);
110
        }
111
112
        try {
113
            $getFile = $this->wopi->getFile(
114
                $fileId,
115
                $this->getParam($request->getUri(), 'access_token'),
116
                $request
117
            );
118
        } catch (Throwable $e) {
119
            throw $e;
120
        }
121
122
        return $getFile;
123
    }
124
125
    public function getLock(string $fileId, RequestInterface $request): ResponseInterface
126
    {
127
        if (!$this->wopiProofValidator->isValid($request)) {
128
            return $this
129
                ->psr17
130
                ->createResponse(500);
131
        }
132
133
        try {
134
            $getLock = $this->wopi->getLock(
135
                $fileId,
136
                $this->getParam($request->getUri(), 'access_token'),
137
                $request
138
            );
139
        } catch (Throwable $e) {
140
            throw $e;
141
        }
142
143
        return $getLock;
144
    }
145
146
    public function getShareUrl(string $fileId, RequestInterface $request): ResponseInterface
147
    {
148
        if (!$this->wopiProofValidator->isValid($request)) {
149
            return $this
150
                ->psr17
151
                ->createResponse(500);
152
        }
153
154
        try {
155
            $getShareUrl = $this->wopi->enumerateAncestors(
156
                $fileId,
157
                $this->getParam($request->getUri(), 'access_token'),
158
                $request
159
            );
160
        } catch (Throwable $e) {
161
            throw $e;
162
        }
163
164
        return $getShareUrl;
165
    }
166
167
    public function lock(string $fileId, RequestInterface $request): ResponseInterface
168
    {
169
        if (!$this->wopiProofValidator->isValid($request)) {
170
            return $this
171
                ->psr17
172
                ->createResponse(500);
173
        }
174
175
        try {
176
            $lock = $this->wopi->lock(
177
                $fileId,
178
                $this->getParam($request->getUri(), 'access_token'),
179
                $request->getHeaderLine('X-WOPI-Lock'),
180
                $request
181
            );
182
        } catch (Throwable $e) {
183
            throw $e;
184
        }
185
186
        return $lock;
187
    }
188
189
    public function putFile(string $fileId, RequestInterface $request): ResponseInterface
190
    {
191
        if (!$this->wopiProofValidator->isValid($request)) {
192
            return $this
193
                ->psr17
194
                ->createResponse(500);
195
        }
196
197
        try {
198
            $putFile = $this->wopi->putFile(
199
                $fileId,
200
                $this->getParam($request->getUri(), 'access_token'),
201
                $request->getHeaderLine('X-WOPI-Lock'),
202
                $request->getHeaderLine('X-WOPI-Editors'),
203
                $request
204
            );
205
        } catch (Throwable $e) {
206
            throw $e;
207
        }
208
209
        return $putFile;
210
    }
211
212
    public function putRelativeFile(string $fileId, RequestInterface $request): ResponseInterface
213
    {
214
        if (!$this->wopiProofValidator->isValid($request)) {
215
            return $this
216
                ->psr17
217
                ->createResponse(500);
218
        }
219
220
        $suggestedTarget = $request->hasHeader('X-WOPI-SuggestedTarget') ?
221
            mb_convert_encoding($request->getHeaderLine('X-WOPI-SuggestedTarget'), 'UTF-8', 'UTF-7') :
222
            null;
223
        $relativeTarget = $request->hasHeader('X-WOPI-RelativeTarget') ?
224
            mb_convert_encoding($request->getHeaderLine('X-WOPI-RelativeTarget'), 'UTF-8', 'UTF-7') :
225
            null;
226
        $overwriteRelativeTarget = $request->hasHeader('X-WOPI-OverwriteRelativeTarget') ?
227
            ('false' === strtolower($request->getHeaderLine('X-WOPI-OverwriteRelativeTarget')) ? false : true) :
228
            false;
229
230
        try {
231
            $putRelativeFile = $this->wopi->putRelativeFile(
232
                $fileId,
233
                $this->getParam($request->getUri(), 'access_token'),
234
                $suggestedTarget,
0 ignored issues
show
Bug introduced by
It seems like $suggestedTarget can also be of type array; however, parameter $suggestedTarget of ChampsLibres\WopiLib\Ser...face::putRelativeFile() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
                /** @scrutinizer ignore-type */ $suggestedTarget,
Loading history...
235
                $relativeTarget,
0 ignored issues
show
Bug introduced by
It seems like $relativeTarget can also be of type array; however, parameter $relativeTarget of ChampsLibres\WopiLib\Ser...face::putRelativeFile() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
                /** @scrutinizer ignore-type */ $relativeTarget,
Loading history...
236
                $overwriteRelativeTarget,
237
                (int) $request->getHeaderLine('X-WOPI-Size'),
238
                $request
239
            );
240
        } catch (Throwable $e) {
241
            throw $e;
242
        }
243
244
        return $putRelativeFile;
245
    }
246
247
    public function putUserInfo(string $fileId, RequestInterface $request): ResponseInterface
248
    {
249
        if (!$this->wopiProofValidator->isValid($request)) {
250
            return $this
251
                ->psr17
252
                ->createResponse(500);
253
        }
254
255
        try {
256
            $putUserInfo = $this->wopi->putUserInfo(
257
                $fileId,
258
                $this->getParam($request->getUri(), 'access_token'),
259
                $request
260
            );
261
        } catch (Throwable $e) {
262
            throw $e;
263
        }
264
265
        return $putUserInfo;
266
    }
267
268
    public function refreshLock(string $fileId, RequestInterface $request): ResponseInterface
269
    {
270
        if (!$this->wopiProofValidator->isValid($request)) {
271
            return $this
272
                ->psr17
273
                ->createResponse(500);
274
        }
275
276
        try {
277
            $refreshLock = $this->wopi->refreshLock(
278
                $fileId,
279
                $this->getParam($request->getUri(), 'access_token'),
280
                $request->getHeaderLine('X-WOPI-Lock'),
281
                $request
282
            );
283
        } catch (Throwable $e) {
284
            throw $e;
285
        }
286
287
        return $refreshLock;
288
    }
289
290
    public function renameFile(string $fileId, RequestInterface $request): ResponseInterface
291
    {
292
        if (!$this->wopiProofValidator->isValid($request)) {
293
            return $this
294
                ->psr17
295
                ->createResponse(500);
296
        }
297
298
        try {
299
            $renameFile = $this->wopi->renameFile(
300
                $fileId,
301
                $this->getParam($request->getUri(), 'access_token'),
302
                $request->getHeaderLine('X-WOPI-Lock'),
303
                $request->getHeaderLine('X-WOPI-RequestedName'),
304
                $request
305
            );
306
        } catch (Throwable $e) {
307
            throw $e;
308
        }
309
310
        return $renameFile;
311
    }
312
313
    public function unlock(string $fileId, RequestInterface $request): ResponseInterface
314
    {
315
        if (!$this->wopiProofValidator->isValid($request)) {
316
            return $this
317
                ->psr17
318
                ->createResponse(500);
319
        }
320
321
        try {
322
            $unlock = $this->wopi->unlock(
323
                $fileId,
324
                $this->getParam($request->getUri(), 'access_token'),
325
                $request->getHeaderLine('X-WOPI-Lock'),
326
                $request
327
            );
328
        } catch (Throwable $e) {
329
            throw $e;
330
        }
331
332
        return $unlock;
333
    }
334
335
    public function unlockAndRelock(string $fileId, RequestInterface $request): ResponseInterface
336
    {
337
        if (!$this->wopiProofValidator->isValid($request)) {
338
            return $this
339
                ->psr17
340
                ->createResponse(500);
341
        }
342
343
        try {
344
            $unlockAndRelock = $this->wopi->unlockAndRelock(
345
                $fileId,
346
                $this->getParam($request->getUri(), 'access_token'),
347
                $request->getHeaderLine('X-WOPI-Lock'),
348
                $request->getHeaderLine('X-WOPI-OldLock'),
349
                $request
350
            );
351
        } catch (Throwable $e) {
352
            throw $e;
353
        }
354
355
        return $unlockAndRelock;
356
    }
357
358
    private function getParam(UriInterface $uri, string $param): string
359
    {
360
        $output = [];
361
362
        parse_str($uri->getQuery(), $output);
363
364
        if (!array_key_exists($param, $output)) {
365
            // TODO
366
            throw new Exception('No param found.');
367
        }
368
369
        return $output[$param];
370
    }
371
}
372