ResultNormalizer::normalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Normalizer\Pagination;
5
6
use Paysera\Pagination\Entity\Result;
7
use Paysera\Component\Normalization\NormalizationContext;
8
use Paysera\Component\Normalization\NormalizerInterface;
9
use Paysera\Component\Normalization\TypeAwareInterface;
10
11
class ResultNormalizer implements NormalizerInterface, TypeAwareInterface
12
{
13
    /**
14
     * @param Result $result
15
     * @param NormalizationContext $normalizationContext
16
     * @return array
17
     */
18
    public function normalize($result, NormalizationContext $normalizationContext)
19
    {
20
        return [
21
            'items' => $result->getItems(),
22
            '_metadata' => $this->mapMetadataFromEntity($result),
23
        ];
24
    }
25
26
    private function mapMetadataFromEntity(Result $result)
27
    {
28
        $data = [
29
            'total' => $result->getTotalCount(),
30
            'has_next' => $result->hasNext(),
31
            'has_previous' => $result->hasPrevious(),
32
        ];
33
34
        if ($result->getNextCursor() !== null) {
35
            $data['cursors']['after'] = $result->getNextCursor();
36
        }
37
        if ($result->getPreviousCursor() !== null) {
38
            $data['cursors']['before'] = $result->getPreviousCursor();
39
        }
40
41
        return $data;
42
    }
43
44 81
    public function getType(): string
45
    {
46 81
        return Result::class;
47
    }
48
}
49