Passed
Push — master ( 20f36e...862b31 )
by Marcel
10:03
created

DateLessonInSectionValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 22
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A validate() 0 18 4
1
<?php
2
3
namespace App\Validator;
4
5
use App\Entity\DateLesson;
6
use App\Entity\Section;
7
use App\Repository\SectionRepositoryInterface;
8
use App\Section\SectionResolverInterface;
9
use DateTime;
10
use Symfony\Component\Validator\Constraint;
11
use Symfony\Component\Validator\ConstraintValidator;
12
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
13
14
class DateLessonInSectionValidator extends ConstraintValidator {
15
16
    public function __construct(private readonly SectionResolverInterface $sectionResolver, private readonly SectionRepositoryInterface $sectionRepository) { }
17
18
    public function validate(mixed $value, Constraint $constraint) {#
19
        if(!$value instanceof DateLesson) {
20
            throw new UnexpectedTypeException($value, DateLesson::class);
21
        }
22
23
        if(!$constraint instanceof DateLessonInSection) {
24
            throw new UnexpectedTypeException($constraint, DateLessonInSection::class);
25
        }
26
27
        $section = $this->sectionResolver->getSectionForDate($value->getDate());
0 ignored issues
show
Bug introduced by
It seems like $value->getDate() can also be of type null; however, parameter $dateTime of App\Section\SectionResol...ce::getSectionForDate() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $section = $this->sectionResolver->getSectionForDate(/** @scrutinizer ignore-type */ $value->getDate());
Loading history...
28
29
        if($section === null) {
30
            $sections = $this->sectionRepository->findAll();
31
32
            $this->context
33
                ->buildViolation($constraint->message)
34
                ->setParameter('{{ sections }}', implode(', ', array_map(fn(Section $section) => $section->getDisplayName(), $sections)))
35
                ->addViolation();
36
        }
37
    }
38
}