Passed
Pull Request — master (#14)
by Nicolas
11:09
created

FormErrorNormalizer::convertFormToArray()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 24
nop 1
dl 0
loc 24
ccs 13
cts 13
cp 1
crap 6
rs 9.2222
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