ValidatorException   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusCode() 0 3 1
A __construct() 0 8 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Exception/ValidatorException.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Exception;
10
11
use App\Exception\interfaces\ClientErrorInterface;
12
use App\Exception\models\ValidatorError;
13
use App\Utils\JSON;
14
use JsonException;
15
use Symfony\Component\Validator\ConstraintViolationInterface;
16
use Symfony\Component\Validator\ConstraintViolationListInterface;
17
use Symfony\Component\Validator\Exception\ValidatorException as BaseValidatorException;
18
use function array_map;
19
use function iterator_to_array;
20
21
/**
22
 * Class ValidatorException
23
 *
24
 * @package App\Exception
25
 * @author TLe, Tarmo Leppänen <[email protected]>
26
 */
27
class ValidatorException extends BaseValidatorException implements ClientErrorInterface
28
{
29
    /**
30
     * @throws JsonException
31
     */
32 25
    public function __construct(string $target, ConstraintViolationListInterface $errors)
33
    {
34 25
        parent::__construct(
35 25
            JSON::encode(
36 25
                array_map(
37 25
                    static fn (ConstraintViolationInterface $error): ValidatorError =>
38 25
                        new ValidatorError($error, $target),
39 25
                    iterator_to_array($errors),
40 25
                ),
41 25
            ),
42 25
        );
43
    }
44
45 4
    public function getStatusCode(): int
46
    {
47 4
        return 400;
48
    }
49
}
50