Passed
Push — master ( 9ee3d7...60841a )
by Nicolas
05:56
created

FormErrorNormalizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 23
c 2
b 0
f 1
dl 0
loc 56
ccs 23
cts 24
cp 0.9583
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertFormToArray() 0 24 6
A supportsNormalization() 0 3 3
A normalize() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer\Normalizer;
6
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10
/**
11
 * FormErrorNormalizer.
12
 *
13
 * @see \FOS\RestBundle\Serializer\Normalizer\FormErrorNormalizer
14
 */
15
final class FormErrorNormalizer implements NormalizerInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 6
    public function normalize($object, string $format = null, array $context = []): array
21
    {
22
        $data = [
23 6
            'code' => $context['status_code'] ?? null,
24 6
            'message' => 'Validation Failed',
25 6
            'errors' => $this->convertFormToArray($object),
26
        ];
27
28 6
        if (!\is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
29
            throw new \RuntimeException('Normalized form data should be of type array.');
30
        }
31
32
        /** @var array $data */
33 6
        $data = $data['errors']['children'];
34 6
        $data = \array_filter($data, fn (array $child) => isset($child['errors']) && \count($child['errors']) > 0);
35
36 6
        return \array_map(fn (array $child) => $child['errors'] ?? [], $data);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 34
    public function supportsNormalization($data, string $format = null): bool
43
    {
44 34
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
45
    }
46
47 6
    private function convertFormToArray(FormInterface $data): array
48
    {
49 6
        $form = $errors = [];
50
51 6
        foreach ($data->getErrors() as $error) {
52 6
            $errors[] = $error->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            /** @scrutinizer ignore-call */ 
53
            $errors[] = $error->getMessage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        }
54
55 6
        if ($errors) {
56 6
            $form['errors'] = $errors;
57
        }
58
59 6
        $children = [];
60 6
        foreach ($data->all() as $child) {
61 6
            if ($child instanceof FormInterface) {
62 6
                $children[$child->getName()] = $this->convertFormToArray($child);
63
            }
64
        }
65
66 6
        if ($children) {
67 6
            $form['children'] = $children;
68
        }
69
70 6
        return $form;
71
    }
72
}
73