ValidAttributesArrayValidator::validate()   B
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 42
c 1
b 0
f 1
nc 9
nop 2
dl 0
loc 53
ccs 0
cts 41
cp 0
crap 132
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Validator;
4
5
use App\Entity\ServiceAttribute;
6
use App\Entity\ServiceAttributeType;
7
use App\Repository\ServiceAttributeRepositoryInterface;
8
use App\Utils\ArrayUtils;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
12
13
class ValidAttributesArrayValidator extends ConstraintValidator {
14
15
    public function __construct(private ServiceAttributeRepositoryInterface $attributeRepository)
16
    {
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function validate($value, Constraint $constraint) {
23
        if(!$constraint instanceof ValidAttributesArray) {
24
            throw new UnexpectedTypeException($constraint, ValidAttributesArray::class);
25
        }
26
27
        /** @var ServiceAttribute[] $attributes */
28
        $attributes = ArrayUtils::createArrayWithKeys(
29
            $this->attributeRepository->findAll(),
30
            fn(ServiceAttribute $attribute) => $attribute->getName());
31
        $validAttributeNames = array_keys($attributes);
32
33
        foreach($value as $attributeName => $attributeValue) {
34
            if(!in_array($attributeName, $validAttributeNames)) {
35
                $this->context
36
                    ->buildViolation($constraint->messageNotFound)
37
                    ->setParameter('{{ name }}', $attributeName)
38
                    ->addViolation();
39
                continue;
40
            }
41
42
            $attribute = $attributes[$attributeName];
43
44
            if($attribute->getType() === ServiceAttributeType::Text && $attributeValue !== null &&  !is_scalar($attributeValue)) {
45
                $this->context
46
                    ->buildViolation($constraint->messageInvalidValue)
47
                    ->setParameter('{{ name }}', $attributeName)
48
                    ->setParameter('{{ type }}', 'string')
49
                    ->setParameter('{{ given }}', gettype($attributeValue))
50
                    ->addViolation();
51
            } else if($attribute->getType() === ServiceAttributeType::Select) {
52
                if(!is_array($attributeValue)) {
53
                    $this->context
54
                        ->buildViolation($constraint->messageInvalidValue)
55
                        ->setParameter('{{ name }}', $attributeName)
56
                        ->setParameter('{{ type }}', 'array')
57
                        ->setParameter('{{ given }}', gettype($attributeValue))
58
                        ->addViolation();
59
                } else {
60
                    $i = 0;
61
                    $validValues = array_keys($attribute->getOptions());
62
63
                    foreach($attributeValue as $arrayValue) {
64
                        if(!in_array($arrayValue, $validValues)) {
65
                            $this->context
66
                                ->buildViolation($constraint->messageInvalidArrayItem)
67
                                ->setParameter('{{ name }}', $attributeName)
68
                                ->setParameter('{{ valid }}', json_encode($validValues, JSON_THROW_ON_ERROR))
69
                                ->setParameter('{{ given }}', $arrayValue)
70
                                ->setParameter('{{ pos }}', (string)$i)
71
                                ->addViolation();
72
                        }
73
74
                        $i++;
75
                    }
76
                }
77
            }
78
        }
79
    }
80
81
}