Passed
Pull Request — master (#2)
by Vitaliy
03:08 queued 36s
created

ErrorCollectionNormalizer::normalize()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 29
c 0
b 0
f 0
nc 64
nop 3
dl 0
loc 57
rs 8.2114

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 ErrorCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
28
{
29
    use NormalizerAwareTrait;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function supportsNormalization($data, $format = null): bool
35
    {
36
        return \is_object($data) && $data instanceof ErrorCollection;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @param ErrorCollection $object
43
     */
44
    public function normalize($object, $format = null, array $context = []): array
45
    {
46
        $nested = $object->getMessage() || $object->getReason();
47
48
        $errors = [];
49
50
        foreach ($object as $item) {
51
            $errors[] = $this->normalizer->normalize($item, $format, $context);
52
        }
53
54
        $links = [];
55
56
        if (\count($object->getRelations())) {
57
            // @phpstan-ignore-next-line
58
            $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
59
        }
60
61
        if (\count($object->getActions())) {
62
            // @phpstan-ignore-next-line
63
            $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
64
        }
65
66
        if ($nested) {
67
            $innerError = new ErrorResource(
68
                $object->getMessage(),
69
                $object->getReason(),
70
                $object->getPath(),
71
                $object->getAttributes(),
72
                $object->getIdentifier()
73
            );
74
75
            /** @var array $data */
76
            $data = $this->normalizer->normalize($innerError, $format, $context);
77
78
            if (\count($links)) {
79
                $data['_links'] = $links;
80
            }
81
82
            $data['_embedded'] = [
83
                'errors' => $errors,
84
            ];
85
86
            return $data;
87
        }
88
89
        $data = [
90
            'total' => \count($errors),
91
            '_embedded' => [
92
                'errors' => $errors,
93
            ],
94
        ];
95
96
        if (\count($links)) {
97
            $data['_links'] = $links;
98
        }
99
100
        return $data;
101
    }
102
}
103