ResourceCollectionObjectNormalizer::normalize()   B
last analyzed

Complexity

Conditions 7
Paths 17

Size

Total Lines 36

Duplication

Lines 13
Ratio 36.11 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
dl 13
loc 36
ccs 18
cts 18
cp 1
rs 8.4106
c 0
b 0
f 0
cc 7
nc 17
nop 3
crap 7
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializers\Hateoas\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\ResourceCollection;
17
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
/**
22
 * The normalizer for normalize resource collection.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class ResourceCollectionObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
27
{
28
    use NormalizerAwareTrait;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param ResourceCollection $object
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37 3
    public function normalize($object, $format = null, array $context = [])
38
    {
39 3 View Code Duplication
        if (!$object instanceof ResourceCollection) {
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...
40 1
            throw new \InvalidArgumentException(\sprintf(
41 1
                'The normalizer support only "%s" but "%s" given.',
42 1
                ResourceCollection::class,
43 1
                \is_object($object) ? \get_class($object) : \gettype($object)
44
            ));
45
        }
46
47 2
        $data = [];
48 2
        $links = [];
49
50 2 View Code Duplication
        if (\count($object->getRelations())) {
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...
51 1
            $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
52
        }
53
54 2 View Code Duplication
        if (\count($object->getActions())) {
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...
55 1
            $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
56
        }
57
58 2
        foreach ($object as $resource) {
59 2
            $data[] = $this->normalizer->normalize($resource, $format, $context);
60
        }
61
62 2
        if (\count($links)) {
63
            return [
64 1
                '_links' => $links,
65
                '_embedded' => [
66 1
                    'items' => $data,
67
                ],
68
            ];
69
        }
70
71 1
        return $data;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function supportsNormalization($data, $format = null): bool
78
    {
79 2
        return \is_object($data) && $data instanceof ResourceCollection;
80
    }
81
}
82