Passed
Push — main ( 02aafc...c60ca1 )
by Fractal
02:41
created

ValidationFormatter::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\ExceptionFormatter\Formatter;
6
7
use FRZB\Component\RequestMapper\Data\ContractError;
8
use FRZB\Component\RequestMapper\Data\ContractErrorInterface;
9
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
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): ContractErrorInterface
19
    {
20 1
        return new ContractError(
21 1
            $e->getMessage(),
22 1
            Response::HTTP_UNPROCESSABLE_ENTITY,
23 1
            self::formatErrors(...$e->getErrors()),
24 1
            $e->getTrace()
25
        );
26
    }
27
28 1
    public static function getExceptionClass(): string
29
    {
30 1
        return ValidationException::class;
31
    }
32
33 1
    public static function getPriority(): int
34
    {
35 1
        return 1;
36
    }
37
38 1
    private static function formatErrors(Error ...$errors): array
39
    {
40 1
        return array_merge(
41 1
            ...array_map(static fn (Error $error) => [$error->getField() => $error->getMessage()], $errors)
42
        );
43
    }
44
}
45