Lines
100.00%
8 / 8
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Population | 100.00% | 8 / 8 | 100.00% | 4 / 4 | 6 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| __toString | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| equals | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| validate | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Districts\Core\Domain; | |
| 6 | ||
| 7 | use Districts\Core\Domain\Exception\InvalidPopulationException; | |
| 8 | use Doctrine\ORM\Mapping as ORM; | |
| 9 | ||
| 10 | #[ORM\Embeddable] | |
| 11 | readonly class Population | |
| 12 | { | |
| 13 | #[ORM\Column(type: "integer")] | |
| 14 | private int $population; | |
| 15 | ||
| 16 | public function __construct(int $population) | |
| 17 | { | |
| 18 | if (!self::validate($population)) { | |
| 19 | throw new InvalidPopulationException(); | |
| 20 | } | |
| 21 | $this->population = $population; | |
| 22 | } | |
| 23 | ||
| 24 | public function __toString(): string | |
| 25 | { | |
| 26 | return strval($this->population); | |
| 27 | } | |
| 28 | ||
| 29 | public function equals(self $other): bool | |
| 30 | { | |
| 31 | return $this->population === $other->population; | |
| 32 | } | |
| 33 | ||
| 34 | private static function validate(int $population): bool | |
| 35 | { | |
| 36 | if ($population <= 0) { | |
| 37 | return false; | |
| 38 | } | |
| 39 | return true; | |
| 40 | } | |
| 41 | } |