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 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 38
ccs 14
cts 14
cp 1
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 Paysera\Bundle\ApiBundle\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
/**
12
 * @see PagedQueryNormalizer should be used to optimize queries depending on requested fields
13
 */
14
class ResultNormalizer implements NormalizerInterface, TypeAwareInterface
15
{
16
    /**
17
     * @param Result $result
18
     * @param NormalizationContext $normalizationContext
19
     * @return array
20
     */
21 12
    public function normalize($result, NormalizationContext $normalizationContext)
22
    {
23
        return [
24 12
            'items' => $result->getItems(),
25 12
            '_metadata' => $this->mapMetadataFromEntity($result),
26
        ];
27
    }
28
29 12
    private function mapMetadataFromEntity(Result $result)
30
    {
31
        $data = [
32 12
            'total' => $result->getTotalCount(),
33 12
            'has_next' => $result->hasNext(),
34 12
            'has_previous' => $result->hasPrevious(),
35
        ];
36
37 12
        if ($result->getNextCursor() !== null) {
38 12
            $data['cursors']['after'] = $result->getNextCursor();
39
        }
40 12
        if ($result->getPreviousCursor() !== null) {
41 12
            $data['cursors']['before'] = $result->getPreviousCursor();
42
        }
43
44 12
        return $data;
45
    }
46
47 96
    public function getType(): string
48
    {
49 96
        return Result::class;
50
    }
51
}
52