Lines 100.00% 14 / 14
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Importer 100.00% 14 / 14 100.00% 2 / 2 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 import 100.00% 13 / 13 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Scraper\Application;
6
7use Districts\Core\Domain\Area;
8use Districts\Core\Domain\City;
9use Districts\Core\Domain\CityRepository;
10use Districts\Core\Domain\Name;
11use Districts\Core\Domain\Population;
12use Districts\Scraper\Domain\CityDTO;
13
14readonly class Importer
15{
16    public function __construct(
17        private CityRepository $cityRepository,
18    ) {
19    }
20
21    public function import(CityDTO $cityDTO): void
22    {
23        $city = $this->cityRepository->findByName($cityDTO->name);
24        if ($city !== null) {
25            $city->removeAllDistricts();
26            $this->cityRepository->update($city);
27        } else {
28            $city = new City($cityDTO->name);
29            $this->cityRepository->add($city);
30        }
31        foreach ($cityDTO->districts as $districtDTO) {
32            $city->addDistrict(
33                new Name($districtDTO->name),
34                new Area($districtDTO->area),
35                new Population($districtDTO->population),
36            );
37        }
38        $this->cityRepository->update($city);
39    }
40}