Test Failed
Push — main ( ab57df...39c21d )
by Fractal
03:48
created

ValidationFormatter::getExceptionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\ExceptionFormatter\Formatter;
6
7
use Fp\Collections\ArrayList;
8
use FRZB\Component\DependencyInjection\Attribute\AsService;
9
use FRZB\Component\DependencyInjection\Attribute\AsTagged;
10
use FRZB\Component\RequestMapper\Data\ErrorContract;
11
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
12
use FRZB\Component\RequestMapper\Data\FormattedError;
13
use FRZB\Component\RequestMapper\Exception\ValidationException;
14
use Symfony\Component\HttpFoundation\Response;
15
16
#[AsService, AsTagged(FormatterInterface::class)]
17
class ValidationFormatter implements FormatterInterface
18 1
{
19
    public function __invoke(ValidationException $e): ErrorContract
20 1
    {
21 1
        return new FormattedError(
22
            $e->getMessage(),
23 1
            Response::HTTP_UNPROCESSABLE_ENTITY,
24 1
            self::formatErrors(...$e->getErrors()),
25
            $e->getTrace(),
26
        );
27
    }
28 1
29
    public static function getExceptionClass(): string
30 1
    {
31
        return ValidationException::class;
32
    }
33 1
34
    public static function getPriority(): int
35 1
    {
36
        return 1;
37
    }
38 1
39
    private static function formatErrors(Error ...$errors): array
40 1
    {
41 1
        return ArrayList::collect($errors)
42 1
            ->map(static fn (Error $error) => [$error->getField() => $error->getMessage()])
43
            ->reduce(array_merge(...))
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 43 at column 36
Loading history...
44
            ->getOrElse([])
45
        ;
46
    }
47
}
48