ValidatorException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
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