Completed
Push — master ( 060764...f21b16 )
by
unknown
43s queued 31s
created

CollectionNormalizer::normalize()   D

Complexity

Conditions 20
Paths 326

Size

Total Lines 70
Code Lines 41

Duplication

Lines 38
Ratio 54.29 %

Importance

Changes 0
Metric Value
dl 38
loc 70
rs 4.1883
c 0
b 0
f 0
cc 20
eloc 41
nc 326
nop 3

How to fix   Long Method    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
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Hal\Serializer;
15
16
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
17
use ApiPlatform\Core\DataProvider\PaginatorInterface;
18
use ApiPlatform\Core\DataProvider\PartialPaginatorInterface;
19
use ApiPlatform\Core\Serializer\ContextTrait;
20
use ApiPlatform\Core\Util\IriHelper;
21
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
25
/**
26
 * Normalizes collections in the HAL format.
27
 *
28
 * @author Kevin Dunglas <[email protected]>
29
 * @author Hamza Amrouche <[email protected]>
30
 */
31
final class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
32
{
33
    use ContextTrait;
34
    use NormalizerAwareTrait;
35
36
    const FORMAT = 'jsonhal';
37
38
    private $resourceClassResolver;
39
    private $pageParameterName;
40
41
    public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName)
42
    {
43
        $this->resourceClassResolver = $resourceClassResolver;
44
        $this->pageParameterName = $pageParameterName;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supportsNormalization($data, $format = null)
51
    {
52
        return self::FORMAT === $format && (is_array($data) || ($data instanceof \Traversable));
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function normalize($object, $format = null, array $context = [])
59
    {
60
        $data = [];
61 View Code Duplication
        if (isset($context['api_sub_level'])) {
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...
62
            foreach ($object as $index => $obj) {
63
                $data[$index] = $this->normalizer->normalize($obj, $format, $context);
64
            }
65
66
            return $data;
67
        }
68
69
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
70
        $context = $this->initContext($resourceClass, $context);
71
        $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
72
73
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null;
74 View Code Duplication
        if ($paginated = $isPaginator = $object instanceof PartialPaginatorInterface) {
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...
75
            if ($object instanceof PaginatorInterface) {
76
                $paginated = 1. !== $lastPage = $object->getLastPage();
77
            } else {
78
                $pageTotalItems = (float) count($object);
79
            }
80
81
            $currentPage = $object->getCurrentPage();
82
            $itemsPerPage = $object->getItemsPerPage();
83
        }
84
85
        $data = [
86
            '_links' => [
87
                'self' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null),
88
            ],
89
        ];
90
91 View Code Duplication
        if ($paginated) {
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...
92
            if (null !== $lastPage) {
93
                $data['_links']['first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.);
94
                $data['_links']['last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
95
            }
96
97
            if (1. !== $currentPage) {
98
                $data['_links']['prev'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.);
99
            }
100
101
            if (null !== $lastPage && $currentPage !== $lastPage || null === $lastPage && $pageTotalItems >= $itemsPerPage) {
102
                $data['_links']['next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.);
103
            }
104
        }
105
106
        foreach ($object as $obj) {
107
            $item = $this->normalizer->normalize($obj, $format, $context);
108
109
            $data['_embedded']['item'][] = $item;
110
            $data['_links']['item'][] = $item['_links']['self'];
111
        }
112
113
        $paginated = null;
114 View Code Duplication
        if (
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...
115
            is_array($object) ||
116
            ($paginated = $object instanceof PaginatorInterface) ||
117
            $object instanceof \Countable && !$object instanceof PartialPaginatorInterface
118
        ) {
119
            $data['totalItems'] = $paginated ? (int) $object->getTotalItems() : count($object);
120
        }
121
122
        if ($isPaginator) {
123
            $data['itemsPerPage'] = (int) $itemsPerPage;
124
        }
125
126
        return $data;
127
    }
128
}
129