AdminValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 19 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Validation\Validator;
6
7
use LAG\AdminBundle\Metadata\AdminResource;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11
12
class AdminValidator extends ConstraintValidator
13
{
14
    public function validate(mixed $value, Constraint $constraint): void
15
    {
16
        if (!$value instanceof AdminResource) {
17
            throw new UnexpectedTypeException($value, AdminResource::class);
18
        }
19
20
        if (!$value->getName()) {
21
            $this->context->addViolation('The admin has an empty name');
22
        }
23
24
        if (!$value->getTitle()) {
25
            $this->context->addViolation('The admin has an empty title');
26
        }
27
28
        if (!class_exists($value->getDataClass())) {
0 ignored issues
show
Bug introduced by
It seems like $value->getDataClass() can also be of type null; however, parameter $class of class_exists() does only seem to accept string, 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

28
        if (!class_exists(/** @scrutinizer ignore-type */ $value->getDataClass())) {
Loading history...
29
            $this->context->addViolation(sprintf(
30
                'The admin "%s" has an invalid data class "%s"',
31
                $value->getName(),
32
                $value->getDataClass(),
33
            ));
34
        }
35
    }
36
}
37