Files::putUserInfo()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 17
ccs 0
cts 11
cp 0
crap 12
rs 9.9332
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\Contract\Service\ProofValidatorInterface;
13
use ChampsLibres\WopiLib\Contract\Service\WopiInterface;
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 Psr\Log\LoggerInterface;
20
use Throwable;
21
22
use function array_key_exists;
23
use function is_array;
24
25
/**
26
 * Class Files.
27
 *
28
 * phpcs:disable Generic.Files.LineLength.TooLong
29
 */
30
final class Files
31
{
32
    private LoggerInterface $logger;
33
34
    private Psr17Interface $psr17;
35
36
    private WopiInterface $wopi;
37
38
    private ProofValidatorInterface $wopiProofValidator;
39
40
    public function __construct(
41
        LoggerInterface $logger,
42
        WopiInterface $wopi,
43
        ProofValidatorInterface $wopiProofValidator,
44
        Psr17Interface $psr17
45
    ) {
46
        $this->logger = $logger;
47
        $this->wopi = $wopi;
48
        $this->wopiProofValidator = $wopiProofValidator;
49
        $this->psr17 = $psr17;
50
    }
51
52
    public function checkFileInfo(string $fileId, RequestInterface $request): ResponseInterface
53
    {
54
        if (!$this->wopiProofValidator->isValid($request)) {
55
            return $this->onProofValidationFailed();
56
        }
57
58
        try {
59
            $checkFileInfo = $this->wopi->checkFileInfo(
60
                $fileId,
61
                $this->getParam($request->getUri(), 'access_token'),
62
                $request
63
            );
64
        } catch (Throwable $e) {
65
            throw $e;
66
        }
67
68
        return $checkFileInfo;
69
    }
70
71
    public function deleteFile(string $fileId, RequestInterface $request): ResponseInterface
72
    {
73
        if (!$this->wopiProofValidator->isValid($request)) {
74
            return $this->onProofValidationFailed();
75
        }
76
77
        try {
78
            $deleteFile = $this->wopi->deleteFile(
79
                $fileId,
80
                $this->getParam($request->getUri(), 'access_token'),
81
                $request
82
            );
83
        } catch (Throwable $e) {
84
            throw $e;
85
        }
86
87
        return $deleteFile;
88
    }
89
90
    public function enumerateAncestors(string $fileId, RequestInterface $request): ResponseInterface
91
    {
92
        if (!$this->wopiProofValidator->isValid($request)) {
93
            return $this->onProofValidationFailed();
94
        }
95
96
        try {
97
            $enumerateAncestors = $this->wopi->enumerateAncestors(
98
                $fileId,
99
                $this->getParam($request->getUri(), 'access_token'),
100
                $request
101
            );
102
        } catch (Throwable $e) {
103
            throw $e;
104
        }
105
106
        return $enumerateAncestors;
107
    }
108
109
    public function getFile(string $fileId, RequestInterface $request): ResponseInterface
110
    {
111
        if (!$this->wopiProofValidator->isValid($request)) {
112
            return $this->onProofValidationFailed();
113
        }
114
115
        try {
116
            $getFile = $this->wopi->getFile(
117
                $fileId,
118
                $this->getParam($request->getUri(), 'access_token'),
119
                $request
120
            );
121
        } catch (Throwable $e) {
122
            throw $e;
123
        }
124
125
        return $getFile;
126
    }
127
128
    public function getLock(string $fileId, RequestInterface $request): ResponseInterface
129
    {
130
        if (!$this->wopiProofValidator->isValid($request)) {
131
            return $this->onProofValidationFailed();
132
        }
133
134
        try {
135
            $getLock = $this->wopi->getLock(
136
                $fileId,
137
                $this->getParam($request->getUri(), 'access_token'),
138
                $request
139
            );
140
        } catch (Throwable $e) {
141
            throw $e;
142
        }
143
144
        return $getLock;
145
    }
146
147
    public function getShareUrl(string $fileId, RequestInterface $request): ResponseInterface
148
    {
149
        if (!$this->wopiProofValidator->isValid($request)) {
150
            return $this->onProofValidationFailed();
151
        }
152
153
        try {
154
            $getShareUrl = $this->wopi->enumerateAncestors(
155
                $fileId,
156
                $this->getParam($request->getUri(), 'access_token'),
157
                $request
158
            );
159
        } catch (Throwable $e) {
160
            throw $e;
161
        }
162
163
        return $getShareUrl;
164
    }
165
166
    public function lock(string $fileId, RequestInterface $request): ResponseInterface
167
    {
168
        if (!$this->wopiProofValidator->isValid($request)) {
169
            return $this->onProofValidationFailed();
170
        }
171
172
        try {
173
            $lock = $this->wopi->lock(
174
                $fileId,
175
                $this->getParam($request->getUri(), 'access_token'),
176
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
177
                $request
178
            );
179
        } catch (Throwable $e) {
180
            throw $e;
181
        }
182
183
        return $lock;
184
    }
185
186
    public function putFile(string $fileId, RequestInterface $request): ResponseInterface
187
    {
188
        if (!$this->wopiProofValidator->isValid($request)) {
189
            return $this->onProofValidationFailed();
190
        }
191
192
        try {
193
            $putFile = $this->wopi->putFile(
194
                $fileId,
195
                $this->getParam($request->getUri(), 'access_token'),
196
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
197
                $request->getHeaderLine(WopiInterface::HEADER_EDITORS),
198
                $request
199
            );
200
        } catch (Throwable $e) {
201
            throw $e;
202
        }
203
204
        return $putFile;
205
    }
206
207
    public function putRelativeFile(string $fileId, RequestInterface $request): ResponseInterface
208
    {
209
        if (!$this->wopiProofValidator->isValid($request)) {
210
            return $this->onProofValidationFailed();
211
        }
212
213
        $suggestedTarget = $request->hasHeader(WopiInterface::HEADER_SUGGESTED_TARGET) ?
214
            mb_convert_encoding($request->getHeaderLine(WopiInterface::HEADER_SUGGESTED_TARGET), 'UTF-8', 'UTF-7') :
215
            null;
216
        $relativeTarget = $request->hasHeader(WopiInterface::HEADER_RELATIVE_TARGET) ?
217
            mb_convert_encoding($request->getHeaderLine(WopiInterface::HEADER_RELATIVE_TARGET), 'UTF-8', 'UTF-7') :
218
            null;
219
        $overwriteRelativeTarget = $request->hasHeader(WopiInterface::HEADER_OVERWRITE_RELATIVE_TARGET) ?
220
            ('false' === strtolower($request->getHeaderLine(WopiInterface::HEADER_OVERWRITE_RELATIVE_TARGET)) ? false : true) :
221
            false;
222
223
        try {
224
            $putRelativeFile = $this->wopi->putRelativeFile(
225
                $fileId,
226
                $this->getParam($request->getUri(), 'access_token'),
227
                $suggestedTarget,
0 ignored issues
show
Bug introduced by
It seems like $suggestedTarget can also be of type array; however, parameter $suggestedTarget of ChampsLibres\WopiLib\Con...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

227
                /** @scrutinizer ignore-type */ $suggestedTarget,
Loading history...
228
                $relativeTarget,
0 ignored issues
show
Bug introduced by
It seems like $relativeTarget can also be of type array; however, parameter $relativeTarget of ChampsLibres\WopiLib\Con...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

228
                /** @scrutinizer ignore-type */ $relativeTarget,
Loading history...
229
                $overwriteRelativeTarget,
230
                (int) $request->getHeaderLine(WopiInterface::HEADER_SIZE),
231
                $request
232
            );
233
        } catch (Throwable $e) {
234
            throw $e;
235
        }
236
237
        return $putRelativeFile;
238
    }
239
240
    public function putUserInfo(string $fileId, RequestInterface $request): ResponseInterface
241
    {
242
        if (!$this->wopiProofValidator->isValid($request)) {
243
            return $this->onProofValidationFailed();
244
        }
245
246
        try {
247
            $putUserInfo = $this->wopi->putUserInfo(
248
                $fileId,
249
                $this->getParam($request->getUri(), 'access_token'),
250
                $request
251
            );
252
        } catch (Throwable $e) {
253
            throw $e;
254
        }
255
256
        return $putUserInfo;
257
    }
258
259
    public function refreshLock(string $fileId, RequestInterface $request): ResponseInterface
260
    {
261
        if (!$this->wopiProofValidator->isValid($request)) {
262
            return $this->onProofValidationFailed();
263
        }
264
265
        try {
266
            $refreshLock = $this->wopi->refreshLock(
267
                $fileId,
268
                $this->getParam($request->getUri(), 'access_token'),
269
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
270
                $request
271
            );
272
        } catch (Throwable $e) {
273
            throw $e;
274
        }
275
276
        return $refreshLock;
277
    }
278
279
    public function renameFile(string $fileId, RequestInterface $request): ResponseInterface
280
    {
281
        if (!$this->wopiProofValidator->isValid($request)) {
282
            return $this->onProofValidationFailed();
283
        }
284
285
        $requestedName = $request->hasHeader(WopiInterface::HEADER_REQUESTED_NAME) ?
286
            mb_convert_encoding($request->getHeaderLine(WopiInterface::HEADER_REQUESTED_NAME), 'UTF-8', 'UTF-7') :
287
            false;
288
289
        if (false === $requestedName) {
290
            return $this
291
                ->psr17
292
                ->createResponse(400);
293
        }
294
295
        try {
296
            $renameFile = $this->wopi->renameFile(
297
                $fileId,
298
                $this->getParam($request->getUri(), 'access_token'),
299
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
300
                $requestedName,
0 ignored issues
show
Bug introduced by
It seems like $requestedName can also be of type array; however, parameter $xWopiRequestedName of ChampsLibres\WopiLib\Con...Interface::renameFile() does only seem to accept 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

300
                /** @scrutinizer ignore-type */ $requestedName,
Loading history...
301
                $request
302
            );
303
        } catch (Throwable $e) {
304
            throw $e;
305
        }
306
307
        return $renameFile;
308
    }
309
310
    public function unlock(string $fileId, RequestInterface $request): ResponseInterface
311
    {
312
        if (!$this->wopiProofValidator->isValid($request)) {
313
            return $this->onProofValidationFailed();
314
        }
315
316
        try {
317
            $unlock = $this->wopi->unlock(
318
                $fileId,
319
                $this->getParam($request->getUri(), 'access_token'),
320
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
321
                $request
322
            );
323
        } catch (Throwable $e) {
324
            throw $e;
325
        }
326
327
        return $unlock;
328
    }
329
330
    public function unlockAndRelock(string $fileId, RequestInterface $request): ResponseInterface
331
    {
332
        if (!$this->wopiProofValidator->isValid($request)) {
333
            return $this->onProofValidationFailed();
334
        }
335
336
        try {
337
            $unlockAndRelock = $this->wopi->unlockAndRelock(
338
                $fileId,
339
                $this->getParam($request->getUri(), 'access_token'),
340
                $request->getHeaderLine(WopiInterface::HEADER_LOCK),
341
                $request->getHeaderLine(WopiInterface::HEADER_OLD_LOCK),
342
                $request
343
            );
344
        } catch (Throwable $e) {
345
            throw $e;
346
        }
347
348
        return $unlockAndRelock;
349
    }
350
351
    private function getParam(UriInterface $uri, string $param): string
352
    {
353
        $output = [];
354
355
        parse_str($uri->getQuery(), $output);
356
357
        if (!array_key_exists($param, $output)) {
358
            throw new Exception('No param found.');
359
        }
360
361
        $r = $output[$param];
362
363
        if (is_array($r)) {
364
            throw new Exception('Param is an array, not a string');
365
        }
366
367
        return $r;
368
    }
369
370
    private function onProofValidationFailed(): ResponseInterface
371
    {
372
        $this->logger->error('[wopi] Proof validation failed');
373
374
        return $this
375
            ->psr17
376
            ->createResponse(500)
377
            ->withBody($this->psr17->createStream((string) json_encode([
378
                'message' => 'Proof validation failed',
379
            ])));
380
    }
381
}
382