TimezoneValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 10 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Validator/Constraints/TimezoneValidator.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 array_column;
15
use function in_array;
16
use function is_string;
17
18
/**
19
 * Class TimezoneValidator
20
 *
21
 * @package App\Validator\Constraints
22
 * @author TLe, Tarmo Leppänen <[email protected]>
23
 */
24
class TimezoneValidator extends ConstraintValidator
25
{
26 44
    public function __construct(
27
        private readonly Localization $localization,
28
    ) {
29 44
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 44
    public function validate(mixed $value, Constraint $constraint): void
35
    {
36 44
        if (is_string($value)
37 44
            && !in_array($value, array_column($this->localization->getTimezones(), 'identifier'), true)
38
        ) {
39 1
            $this->context
40 1
                ->buildViolation(Timezone::MESSAGE)
41 1
                ->setParameter('{{ timezone }}', $value)
42 1
                ->setCode(Timezone::INVALID_TIMEZONE)
43 1
                ->addViolation();
44
        }
45
    }
46
}
47