LanguageValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 8 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Validator/Constraints/LanguageValidator.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Validator\Constraints;
10
11
use App\Service\Localization;
0 ignored issues
show
Bug introduced by
The type App\Service\Localization was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Validator\Constraint;
13
use Symfony\Component\Validator\ConstraintValidator;
14
use function in_array;
15
16
/**
17
 * Class LanguageValidator
18
 *
19
 * @package App\Validator\Constraints
20
 * @author TLe, Tarmo Leppänen <[email protected]>
21
 */
22
class LanguageValidator extends ConstraintValidator
23
{
24 1
    public function __construct(
25
        private readonly Localization $localization,
26
    ) {
27 1
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function validate(mixed $value, Constraint $constraint): void
33
    {
34 1
        if (in_array($value, $this->localization->getLanguages(), true) !== true) {
35 1
            $this->context
36 1
                ->buildViolation(Language::MESSAGE)
37 1
                ->setParameter('{{ language }}', (string)$value)
38 1
                ->setCode(Language::INVALID_LANGUAGE)
39 1
                ->addViolation();
40
        }
41
    }
42
}
43