Passed
Push — main ( cf8b8d...c2f1dd )
by Fractal
02:39
created

ValidationException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A fromConstraintViolationList() 0 3 1
A __construct() 0 5 1
A fromConstraintViolations() 0 3 1
A fromErrors() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Exception;
6
7
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
8
use FRZB\Component\RequestMapper\Data\ValidationError;
9
use JetBrains\PhpStorm\Pure;
10
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation;
11
use Symfony\Component\Validator\ConstraintViolationListInterface as ConstraintViolationList;
12
13
final class ValidationException extends \LogicException
14
{
15
    public const DEFAULT_MESSAGE = 'Validation error';
16
17
    /** @var Error[] */
18
    private array $errors;
19
20 4
    #[Pure]
21
    private function __construct(Error ...$errors)
22
    {
23 4
        parent::__construct(self::DEFAULT_MESSAGE);
24 4
        $this->errors = $errors;
25 4
    }
26
27
    /** @noinspection PhpParamsInspection */
28 2
    public static function fromConstraintViolationList(ConstraintViolationList $violationList): self
29
    {
30 2
        return self::fromConstraintViolations(...$violationList);
31
    }
32
33 2
    public static function fromConstraintViolations(ConstraintViolation ...$violations): self
34
    {
35 2
        return self::fromErrors(...array_map(static fn (ConstraintViolation $violation) => ValidationError::fromConstraint($violation), $violations));
36
    }
37
38 4
    #[Pure]
39
    public static function fromErrors(Error ...$errors): self
40
    {
41 4
        return new self(...$errors);
42
    }
43
44
    /** @return Error[] */
45 3
    public function getErrors(): array
46
    {
47 3
        return $this->errors;
48
    }
49
}
50