Passed
Push — main ( cf8b8d...c2f1dd )
by Fractal
02:39
created

ValidationFormatter::formatErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\ExceptionFormatter\Formatter;
6
7
use FRZB\Component\RequestMapper\Data\ErrorContract;
8
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
9
use FRZB\Component\RequestMapper\Data\FormattedError;
10
use FRZB\Component\RequestMapper\Exception\ValidationException;
11
use FRZB\Component\RequestMapper\Locator\ExceptionFormatterLocatorInterface as ExceptionFormatterLocator;
12
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
13
use Symfony\Component\HttpFoundation\Response;
14
15
#[AutoconfigureTag(ExceptionFormatterLocator::EXCEPTION_FORMATTERS_TAG)]
16 1
class ValidationFormatter implements FormatterInterface
17
{
18 1
    public function __invoke(ValidationException $e): ErrorContract
19
    {
20 1
        return new FormattedError($e->getMessage(), Response::HTTP_UNPROCESSABLE_ENTITY, self::formatErrors(...$e->getErrors()), $e->getTrace());
21
    }
22
23 1
    public static function getExceptionClass(): string
24
    {
25 1
        return ValidationException::class;
26
    }
27
28 1
    public static function getPriority(): int
29
    {
30 1
        return 1;
31
    }
32
33 1
    private static function formatErrors(Error ...$errors): array
34
    {
35 1
        return array_merge(...array_map(static fn (Error $error) => [$error->getField() => $error->getMessage()], $errors));
36
    }
37
}
38