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

ValidationFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExceptionClass() 0 3 1
A formatErrors() 0 4 1
A __invoke() 0 7 1
A getPriority() 0 3 1
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