ErrorCollectionObjectNormalizer::normalize()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 55

Duplication

Lines 6
Ratio 10.91 %

Code Coverage

Tests 28
CRAP Score 8

Importance

Changes 0
Metric Value
dl 6
loc 55
ccs 28
cts 28
cp 1
rs 7.7373
c 0
b 0
f 0
cc 8
nc 64
nop 3
crap 8

How to fix   Long Method   

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
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\VndError\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\Error\ErrorCollection;
17
use FiveLab\Component\Resource\Resource\Error\ErrorResource;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * Normalizer for normalize error collection for Vnd.Error format.
24
 *
25
 * @author Vitaliy Zhuk <[email protected]>
26
 */
27
class ErrorCollectionObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
28
{
29
    use NormalizerAwareTrait;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function supportsNormalization($data, $format = null): bool
35
    {
36 2
        return \is_object($data) && $data instanceof ErrorCollection;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @param ErrorCollection $object
43
     */
44 2
    public function normalize($object, $format = null, array $context = []): array
45
    {
46 2
        $nested = $object->getMessage() || $object->getReason();
47
48 2
        $errors = [];
49
50 2
        foreach ($object as $item) {
51 2
            $errors[] = $this->normalizer->normalize($item, $format, $context);
52
        }
53
54 2
        $links = [];
55
56 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...
57 2
            $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
58
        }
59
60 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...
61 1
            $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
62
        }
63
64 2
        if ($nested) {
65 1
            $innerError = new ErrorResource(
66 1
                $object->getMessage(),
67 1
                $object->getReason(),
68 1
                $object->getPath(),
69 1
                $object->getAttributes(),
70 1
                $object->getIdentifier()
71
            );
72
73 1
            $data = $this->normalizer->normalize($innerError, $format, $context);
74
75 1
            if (\count($links)) {
76 1
                $data['_links'] = $links;
77
            }
78
79 1
            $data['_embedded'] = [
80 1
                'errors' => $errors,
81
            ];
82
83 1
            return $data;
84
        }
85
86
        $data = [
87 1
            'total' => \count($errors),
88
            '_embedded' => [
89 1
                'errors' => $errors,
90
            ],
91
        ];
92
93 1
        if (\count($links)) {
94 1
            $data['_links'] = $links;
95
        }
96
97 1
        return $data;
98
    }
99
}
100