Lines 100.00% 33 / 33
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 fromDomainFilter 100.00% 31 / 31 100.00% 1 / 1 5
 [Districts\Editor\Infrastructure\Doctrine\Filter] __construct 100.00% 1 / 1 100.00% 1 / 1 1
 [Districts\Editor\Infrastructure\Doctrine\Filter] dqlLike 100.00% 1 / 1 100.00% 1 / 1 1
14final readonly class DistrictFilter extends Filter
15{
16    public static function fromDomainFilter(DomainFilter $domainFilter): self
17    {
18        if ($domainFilter instanceof CityNameFilter) {
19            return new self(
20                "c.name LIKE :search",
21                [
22                    "search" => self::dqlLike($domainFilter->cityName),
23                ],
24            );
25        }
26
27        if ($domainFilter instanceof NameFilter) {
28            return new self(
29                "d.name.name LIKE :search",
30                [
31                    "search" => self::dqlLike($domainFilter->name),
32                ],
33            );
34        }
35
36        if ($domainFilter instanceof AreaFilter) {
37            return new self(
38                "d.area.area >= :low AND d.area.area <= :high",
39                [
40                    "low" => $domainFilter->begin,
41                    "high" => $domainFilter->end,
42                ],
43            );
44        }
45
46        if ($domainFilter instanceof PopulationFilter) {
47            return new self(
48                "d.population.population >= :low AND d.population.population <= :high",
49                [
50                    "low" => $domainFilter->begin,
51                    "high" => $domainFilter->end,
52                ],
53            );
54        }
55
56        throw new InvalidArgumentException();
57    }
58}

Inherited from Districts\Editor\Infrastructure\Doctrine\Filter

12    protected function __construct(
13        public string $where,
14        public array $parameters,
15    ) {
16    }
18    protected static function dqlLike(string $string): string
19    {
20        // Doctrine will handle SQL injections, we just need to escape the LIKE syntax
21        return "%" . addcslashes($string, "%_") . "%";
22    }