PagerDenormalizer::denormalize()   C
last analyzed

Complexity

Conditions 12
Paths 14

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 14
nop 2
crap 12

How to fix   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
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Normalizer\Pagination;
5
6
use Paysera\Pagination\Entity\OrderingPair;
7
use Paysera\Pagination\Entity\Pager;
8
use Paysera\Component\Normalization\DenormalizationContext;
9
use Paysera\Component\Normalization\Exception\InvalidDataException;
10
use Paysera\Component\Normalization\ObjectDenormalizerInterface;
11
use Paysera\Component\Normalization\TypeAwareInterface;
12
use Paysera\Component\ObjectWrapper\Exception\InvalidItemException;
13
use Paysera\Component\ObjectWrapper\ObjectWrapper;
14
15
class PagerDenormalizer implements ObjectDenormalizerInterface, TypeAwareInterface
16
{
17
    private $defaultMaxLimit;
18
19 105
    public function __construct($defaultMaxLimit = 200)
20
    {
21 105
        $this->defaultMaxLimit = $defaultMaxLimit;
22 105
    }
23
24 25
    public function denormalize(ObjectWrapper $input, DenormalizationContext $context)
25
    {
26 25
        $limit = isset($input['limit']) ? $this->getPositiveInt($input, 'limit') : null;
27 22
        if ($limit !== null && $limit > $this->defaultMaxLimit) {
28 1
            throw new InvalidItemException(
29 1
                'limit',
30 1
                sprintf('limit cannot exceed %s', $this->defaultMaxLimit)
31
            );
32
        }
33
34 21
        $offset = isset($input['offset']) ? $this->getPositiveInt($input, 'offset') : null;
35 18
        $after = $input->getString('after');
36 17
        $before = $input->getString('before');
37
38
        if (
39 16
            ($before !== null && $after !== null)
40 14
            || ($before !== null && $offset !== null)
41 16
            || ($after !== null && $offset !== null)
42
        ) {
43 4
            throw new InvalidDataException('Only one of offset, before and after can be specified');
44
        }
45
46 12
        $orderingPairs = isset($input['sort']) ? $this->parseOrderingPairs($input->getString('sort')) : [];
47
48 11
        return (new Pager())
49 11
            ->setOrderingPairs($orderingPairs)
50 11
            ->setOffset($offset)
51 11
            ->setLimit($limit)
52 11
            ->setBefore($before)
53 11
            ->setAfter($after)
54
        ;
55
    }
56
57 4
    private function parseOrderingPairs(string $ordering): array
58
    {
59 4
        $orderingPairs = [];
60 4
        $orderingFields = explode(',', $ordering);
61 4
        foreach ($orderingFields as $field) {
62 4
            $orderAsc = true;
63 4
            if (mb_substr($field, 0, 1) === '-') {
64 1
                $field = mb_substr($field, 1);
65 1
                $orderAsc = false;
66
            }
67
68 4
            $orderingPairs[] = new OrderingPair($field, $orderAsc);
69
        }
70
71 4
        return $orderingPairs;
72
    }
73
74 15
    private function getPositiveInt(ObjectWrapper $input, string $key): int
75
    {
76 15
        $filtered = filter_var($input->getRequiredString($key), FILTER_VALIDATE_INT, ['options' => [
77
            'min_range' => 0,
78
        ]]);
79
80 13
        if ($filtered === false) {
81 4
            throw new InvalidItemException($key, sprintf('%s must be positive integer', $key));
82
        }
83
84 9
        return $filtered;
85
    }
86
87 81
    public function getType(): string
88
    {
89 81
        return Pager::class;
90
    }
91
}
92