Test Failed
Push — master ( c1d2e0...874a5e )
by Sven
13:19
created

ToDoValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 2
b 0
f 0
dl 0
loc 22
rs 10
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\ToDo;
6
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
7
use Symfony\Component\Validator\ConstraintViolation;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
class ToDoValidator
11
{
12
    public function __construct(
13
        private ValidatorInterface $validator
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE
Loading history...
14
    ) {
15
    }
16
17
    public function validate(ToDo $toDo): void
18
    {
19
        $errors = $this->validator->validate($toDo);
20
21
        if ($errors->count() === 0) {
22
            return;
23
        }
24
25
        $errorMessages = [];
26
        foreach ($errors as $error) {
27
            assert($error instanceof ConstraintViolation);
28
            $errorMessages[] = sprintf('%s: %s', $error->getPropertyPath(), $error->getMessage());
29
        }
30
31
        throw new BadRequestHttpException(json_encode($errorMessages, JSON_THROW_ON_ERROR));
32
    }
33
}
34