Lines 100.00% 11 / 11
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
CityRepository 100.00% 11 / 11 100.00% 6 / 6 7 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 get 100.00% 4 / 4 100.00% 1 / 1 2
 findByName 100.00% 1 / 1 100.00% 1 / 1 1
 list 100.00% 1 / 1 100.00% 1 / 1 1
 add 100.00% 2 / 2 100.00% 1 / 1 1
 update 100.00% 2 / 2 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Core\Infrastructure\Doctrine;
6
7use Districts\Core\Domain\City;
8use Districts\Core\Domain\CityRepository as CityRepositoryInterface;
9use Districts\Core\Infrastructure\NotFoundInRepositoryException;
10use Doctrine\ORM\EntityManager;
11
12final readonly class CityRepository implements CityRepositoryInterface
13{
14    public function __construct(
15        private EntityManager $entityManager,
16    ) {
17    }
18
19    public function get(int $id): City
20    {
21        $city = $this->entityManager->getRepository(City::class)->find($id);
22        if ($city === null) {
23            throw new NotFoundInRepositoryException();
24        }
25        return $city;
26    }
27
28    public function findByName(string $name): ?City
29    {
30        return $this->entityManager->getRepository(City::class)->findOneBy(["name" => $name]);
31    }
32
33    /**
34     * @return City[]
35     */
36    public function list(): array
37    {
38        return $this->entityManager->getRepository(City::class)->findAll();
39    }
40
41    public function add(City $city): void
42    {
43        $this->entityManager->persist($city);
44        $this->entityManager->flush();
45    }
46
47    public function update(City $city): void
48    {
49        $this->entityManager->persist($city);
50        $this->entityManager->flush();
51    }
52}