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

ValidationFormatter::format()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 10
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