Lines 100.00% 34 / 34
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DistrictValidator 100.00% 34 / 34 100.00% 7 / 7 18 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 validateAdd 100.00% 6 / 6 100.00% 1 / 1 3
 validateUpdate 100.00% 7 / 7 100.00% 1 / 1 4
 validate 100.00% 8 / 8 100.00% 1 / 1 4
 validateName 100.00% 4 / 4 100.00% 1 / 1 2
 validateArea 100.00% 4 / 4 100.00% 1 / 1 2
 validatePopulation 100.00% 4 / 4 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Editor\Application;
6
7use Districts\Core\Domain\Area;
8use Districts\Core\Domain\CityRepository;
9use Districts\Core\Domain\Exception\InvalidAreaException;
10use Districts\Core\Domain\Exception\InvalidNameException;
11use Districts\Core\Domain\Exception\InvalidPopulationException;
12use Districts\Core\Domain\Name;
13use Districts\Core\Domain\Population;
14use Districts\Editor\Application\Command\AddDistrictCommand;
15use Districts\Editor\Application\Command\UpdateDistrictCommand;
16use Districts\Editor\Domain\DistrictRepository;
17
18readonly class DistrictValidator
19{
20    public function __construct(
21        private CityRepository $cityRepository,
22        private DistrictRepository $districtRepository,
23    ) {
24    }
25
26    public function validateAdd(AddDistrictCommand $command): ValidationResult
27    {
28        $result = $this->validate($command);
29
30        $city = $this->cityRepository->get($command->cityId);
31
32        if (
33            $this->validateName($command->name)
34            && $city->hasDistrictWithName(new Name($command->name))
35        ) {
36            $result->addError("name");
37        }
38
39        return $result;
40    }
41
42    public function validateUpdate(UpdateDistrictCommand $command): ValidationResult
43    {
44        $result = $this->validate($command);
45
46        $district = $this->districtRepository->get($command->id);
47
48        if (
49            $this->validateName($command->name)
50            && !$district->getName()->equals(new Name($command->name))
51            && $district->getCity()->hasDistrictWithName(new Name($command->name))
52        ) {
53            $result->addError("name");
54        }
55
56        return $result;
57    }
58
59    private function validate(AddDistrictCommand | UpdateDistrictCommand $command): ValidationResult
60    {
61        $result = new ValidationResult();
62        if (!$this->validateName($command->name)) {
63            $result->addError("name");
64        }
65        if (!$this->validateArea($command->area)) {
66            $result->addError("area");
67        }
68        if (!$this->validatePopulation($command->population)) {
69            $result->addError("population");
70        }
71        return $result;
72    }
73
74    private function validateName(string $name): bool
75    {
76        try {
77            new Name($name);
78            return true;
79        } catch (InvalidNameException) {
80            return false;
81        }
82    }
83
84    private function validateArea(float $area): bool
85    {
86        try {
87            new Area($area);
88            return true;
89        } catch (InvalidAreaException) {
90            return false;
91        }
92    }
93
94    private function validatePopulation(int $population): bool
95    {
96        try {
97            new Population($population);
98            return true;
99        } catch (InvalidPopulationException) {
100            return false;
101        }
102    }
103}