Completed
Push — master ( 1f7593...5c020a )
by Raffael
12:52 queued 07:24
created

QueryDecoder::process()   D

Complexity

Conditions 15
Paths 240

Size

Total Lines 54

Duplication

Lines 12
Ratio 22.22 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 12
loc 54
ccs 0
cts 44
cp 0
rs 4.5833
c 0
b 0
f 0
cc 15
nc 240
nop 2
crap 240

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...
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $query['watch'] (integer) and 'false' (string) can never be identical. Maybe you want to use a loose comparison != instead?
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 ($query['stream'] === null && empty($query['sort'])) {
75
            $query['sort'] = ['created' => -1];
76
        }
77
78
        $request = $request->withQueryParams($query);
79
80
        return $handler->handle($request);
81
    }
82
}
83