ResultNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 14.29%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 38
ccs 2
cts 14
cp 0.1429
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 7 1
A mapMetadataFromEntity() 0 17 3
A getType() 0 4 1
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