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

ToDoValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 1
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