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

QueryDecoder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 60
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 3
dl 12
loc 60
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 12 54 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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