Completed
Push — master ( b64fc1...5651bd )
by Raffael
16:20 queued 08:39
created

QueryDecoder::process()   F

Complexity

Conditions 16
Paths 336

Size

Total Lines 56

Duplication

Lines 12
Ratio 21.43 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 12
loc 56
ccs 0
cts 46
cp 0
rs 3.0333
c 0
b 0
f 0
cc 16
nc 336
nop 2
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Rest\Middlewares;
13
14
use function MongoDB\BSON\fromJSON;
15
use function MongoDB\BSON\toPHP;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
use Tubee\Rest\Exception;
21
use Zend\Diactoros\Response;
22
23
class QueryDecoder implements MiddlewareInterface
24
{
25
    /**
26
     * Process a server request and return a response.
27
     */
28
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
29
    {
30
        $query = $request->getQueryParams();
31
32
        if (isset($query['offset'])) {
33
            $query['offset'] = (int) $query['offset'];
34
        }
35
36
        if (isset($query['limit'])) {
37
            $query['limit'] = (int) $query['limit'];
38
        }
39
40 View Code Duplication
        if (isset($query['watch']) && !empty($query['watch']) && $query['watch'] !== 'false') {
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...
41
            $query['watch'] = true;
42
        } else {
43
            $query['watch'] = null;
44
        }
45
46
        if (!empty($query['query'])) {
47
            $query['query'] = toPHP(fromJSON($query['query']), [
48
                'root' => 'array',
49
                'document' => 'array',
50
                'array' => 'array',
51
            ]);
52
        } else {
53
            $query['query'] = [];
54
        }
55
56 View Code Duplication
        if (isset($query['stream']) && $query['stream'] !== 'false' && !empty($query['stream'])) {
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...
57
            if (!isset($query['limit'])) {
58
                $query['limit'] = null;
59
            }
60
        } else {
61
            $query['stream'] = null;
62
        }
63
64
        if (!empty($query['sort'])) {
65
            $query['sort'] = json_decode(htmlspecialchars_decode($query['sort']), true);
66
67
            if (json_last_error()) {
68
                throw new Exception\InvalidInput('failed to decode provided sort: '.json_last_error_msg().', sort needs to be valid json');
69
            }
70
        } else {
71
            $query['sort'] = [];
72
        }
73
74
        if (isset($query['stream']) && empty($query['sort'])) {
75
            $query['sort'] = ['created' => 1];
76
        } elseif (empty($query['sort'])) {
77
            $query['sort'] = ['created' => -1];
78
        }
79
80
        $request = $request->withQueryParams($query);
81
82
        return $handler->handle($request);
83
    }
84
}
85