Lines 92.30% 24 / 26
Methods 77.77% 7 / 9
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 addDistrict 100.00% 5 / 5 100.00% 1 / 1 3
 updateDistrict 100.00% 6 / 6 100.00% 1 / 1 4
 removeDistrict 100.00% 2 / 2 100.00% 1 / 1 1
 removeAllDistricts 0.00% 0 / 1 0.00% 0 / 1 2
 getId 0.00% 0 / 1 0.00% 0 / 1 2
 getName 100.00% 1 / 1 100.00% 1 / 1 1
 hasDistrictWithName 100.00% 4 / 4 100.00% 1 / 1 2
 getDistrictByName 100.00% 4 / 4 100.00% 1 / 1 3
16class City
17{
18    #[ORM\Id]
19    #[ORM\Column(type: "integer")]
20    #[ORM\GeneratedValue]
21    private int $id;
22
23    #[ORM\Column(type: "string", options: ["collation" => "utf8_polish_ci"])]
24    private string $name;
25
26    /** @var Collection<int|string, District> */
27    #[ORM\OneToMany(targetEntity: District::class, mappedBy: "city", cascade: ["persist"], orphanRemoval: true)]
28    private Collection $districts;
29
30    public function __construct(string $name)
31    {
32        $this->name = $name;
33        $this->districts = new ArrayCollection();
34    }
35
36    public function addDistrict(
37        Name $name,
38        Area $area,
39        Population $population
40    ): void {
41        foreach ($this->districts as $district) {
42            if ($district->getName()->equals($name)) {
43                throw new DuplicateDistrictNameException();
44            }
45        }
46        $district = new District($this, $name, $area, $population);
47        $this->districts[] = $district;
48    }
49
50    public function updateDistrict(
51        Name $currentName,
52        Name $updatedName,
53        Area $updatedArea,
54        Population $updatedPopulation,
55    ): void {
56        if (!$updatedName->equals($currentName)) {
57            foreach ($this->districts as $district) {
58                if ($district->getName()->equals($updatedName)) {
59                    throw new DuplicateDistrictNameException();
60                }
61            }
62        }
63        $district = $this->getDistrictByName($currentName);
64        $district->update($updatedName, $updatedArea, $updatedPopulation);
65    }
66
67    public function removeDistrict(Name $name): void
68    {
69        $district = $this->getDistrictByName($name);
70        $this->districts->removeElement($district);
71    }
72
73    public function removeAllDistricts(): void
74    {
75        $this->districts->clear();
76    }
77
78    public function getId(): int
79    {
80        return $this->id;
81    }
82
83    public function getName(): string
84    {
85        return $this->name;
86    }
87
88    public function hasDistrictWithName(Name $name): bool
89    {
90        try {
91            $this->getDistrictByName($name);
92            return true;
93        } catch (DistrictNotFoundException) {
94            return false;
95        }
96    }
97
98    private function getDistrictByName(Name $name): District
99    {
100        foreach ($this->districts as $district) {
101            if ($district->getName()->equals($name)) {
102                return $district;
103            }
104        }
105
106        throw new DistrictNotFoundException();
107    }
108}