Passed
Push — main ( 414146...ad9f42 )
by Fractal
02:31
created

ValidationError   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 47
rs 10
ccs 18
cts 18
cp 1
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getField() 0 3 1
A __construct() 0 5 1
A fromTypeAndClassExtractorException() 0 4 1
A getType() 0 3 1
A fromConstraint() 0 9 1
A __toString() 0 3 1
A getMessage() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Data;
6
7
use FRZB\Component\RequestMapper\Exception\ClassExtractorException;
8
use FRZB\Component\RequestMapper\Exception\ErrorInvalidArgumentException;
9
use JetBrains\PhpStorm\Immutable;
10
use JetBrains\PhpStorm\Pure;
11
use Symfony\Component\Validator\ConstraintViolation as Constraint;
12
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation;
13
14
#[Immutable]
15
final class ValidationError implements ErrorInterface
16
{
17 7
    public function __construct(
18
        private string $type,
19
        private string $field,
20
        private string $message
21
    ) {
22 7
    }
23
24 1
    public function __toString()
25
    {
26 1
        return sprintf('type: "%s", field: "%s", message: "%s"', $this->type, $this->field, $this->message);
27
    }
28
29 1
    #[Pure]
30
    public static function fromTypeAndClassExtractorException(string $type, ClassExtractorException $e): self
31
    {
32 1
        return new self($type, $e->getProperty(), $e->getMessage());
33
    }
34
35
    /** @noinspection PhpConditionAlreadyCheckedInspection */
36 2
    public static function fromConstraint(ConstraintViolation $violation): self
37
    {
38
        return match (true) {
39 2
            $violation instanceof Constraint => new self(
40 2
                $violation->getConstraint()::class,
41 2
                $violation->getPropertyPath(),
42 2
                (string) $violation->getMessage()
43
            ),
44 2
            default => throw ErrorInvalidArgumentException::fromConstraintValidation($violation),
45
        };
46
    }
47
48 2
    public function getType(): string
49
    {
50 2
        return $this->type;
51
    }
52
53 6
    public function getField(): string
54
    {
55 6
        return $this->field;
56
    }
57
58 6
    public function getMessage(): ?string
59
    {
60 6
        return $this->message;
61
    }
62
}
63