Test Failed
Pull Request — main (#22)
by Fractal
13:00
created

ValidationFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 28
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 4
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\Exception\ValidationException;
11
use FRZB\Component\RequestMapper\ValueObject\ErrorContract;
12
use FRZB\Component\RequestMapper\ValueObject\ErrorInterface as Error;
13
use FRZB\Component\RequestMapper\ValueObject\FormattedError;
14
use Symfony\Component\HttpFoundation\Response;
15
16
#[AsService, AsTagged(FormatterInterface::class, priority: 0)]
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 getType(): string
30 1
    {
31
        return ValidationException::class;
32
    }
33 1
34
    private static function formatErrors(Error ...$errors): array
35 1
    {
36
        return ArrayList::collect($errors)
37
            ->map(static fn (Error $error) => [$error->getField() => $error->getMessage()])
38 1
            ->reduce(array_merge(...))
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 38 at column 36
Loading history...
39
            ->getOrElse([])
40 1
        ;
41 1
    }
42
}
43