Lines
100.00%
29 / 29
Methods
100.00%
6 / 6
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| add | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 2 | ||
| update | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 2 | ||
| remove | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| list | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| get | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 21 | final readonly class DistrictService | |
| 22 | { | |
| 23 | public function __construct( | |
| 24 | private DistrictValidator $districtValidator, | |
| 25 | private DistrictRepository $districtRepository, | |
| 26 | private CityRepository $cityRepository, | |
| 27 | ) { | |
| 28 | } | |
| 29 | ||
| 30 | public function add(AddDistrictCommand $command): void | |
| 31 | { | |
| 32 | $validationResult = $this->districtValidator->validateAdd($command); | |
| 33 | if (!$validationResult->isOk()) { | |
| 34 | throw new ValidationException()->withErrors($validationResult->getErrors()); | |
| 35 | } | |
| 36 | $city = $this->cityRepository->get($command->cityId); | |
| 37 | $city->addDistrict( | |
| 38 | new Name($command->name), | |
| 39 | new Area($command->area), | |
| 40 | new Population($command->population), | |
| 41 | ); | |
| 42 | $this->cityRepository->update($city); | |
| 43 | } | |
| 44 | ||
| 45 | public function update(UpdateDistrictCommand $command): void | |
| 46 | { | |
| 47 | $validationResult = $this->districtValidator->validateUpdate($command); | |
| 48 | if (!$validationResult->isOk()) { | |
| 49 | throw new ValidationException()->withErrors($validationResult->getErrors()); | |
| 50 | } | |
| 51 | $district = $this->districtRepository->get($command->id); | |
| 52 | $city = $district->getCity(); | |
| 53 | $city->updateDistrict( | |
| 54 | $district->getName(), | |
| 55 | new Name($command->name), | |
| 56 | new Area($command->area), | |
| 57 | new Population($command->population), | |
| 58 | ); | |
| 59 | $this->cityRepository->update($city); | |
| 60 | } | |
| 61 | ||
| 62 | public function remove(RemoveDistrictCommand $command): void | |
| 63 | { | |
| 64 | $district = $this->districtRepository->get($command->id); | |
| 65 | $city = $district->getCity(); | |
| 66 | $city->removeDistrict($district->getName()); | |
| 67 | $this->cityRepository->update($city); | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * @return PaginatedResult<District> | |
| 72 | */ | |
| 73 | public function list(ListDistrictsQuery $query): PaginatedResult | |
| 74 | { | |
| 75 | return $this->districtRepository->listWithPagination($query->ordering, $query->pagination, $query->filter); | |
| 76 | } | |
| 77 | ||
| 78 | public function get(GetDistrictQuery $query): District | |
| 79 | { | |
| 80 | return $this->districtRepository->get($query->id); | |
| 81 | } | |
| 82 | } |