BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace BaclucEventPackage; |
| 5 | |
| 6 | |
| 7 | use BaclucC5Crud\Entity\Repository; |
| 8 | use Doctrine\ORM\EntityManager; |
| 9 | use Doctrine\ORM\EntityManagerInterface; |
BacLuc | b133398 | 2020-04-29 21:27:52 +0200 | [diff] [blame] | 10 | use Doctrine\ORM\NonUniqueResultException; |
| 11 | use Doctrine\ORM\NoResultException; |
| 12 | use RuntimeException; |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 13 | |
| 14 | class CancellationsRepository implements Repository |
| 15 | { |
| 16 | /** |
| 17 | * @var Repository |
| 18 | */ |
| 19 | private $standardRepository; |
| 20 | /** |
| 21 | * @var EntityManagerInterface |
| 22 | */ |
| 23 | private $entityManager; |
| 24 | |
| 25 | |
| 26 | public function __construct(Repository $standardRepository, EntityManager $entityManager) |
| 27 | { |
| 28 | $this->standardRepository = $standardRepository; |
| 29 | $this->entityManager = $entityManager; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | public function create() |
| 34 | { |
| 35 | return $this->standardRepository->create(); |
| 36 | } |
| 37 | |
| 38 | public function persist($entity) |
| 39 | { |
| 40 | return $this->standardRepository->persist($entity); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | */ |
BacLuc | e20c51b | 2020-04-29 20:55:33 +0200 | [diff] [blame] | 46 | public function getAll(int $offset = 0, int $limit = null) |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 47 | { |
BacLuc | e20c51b | 2020-04-29 20:55:33 +0200 | [diff] [blame] | 48 | return $this->standardRepository->getAll($offset, $limit); |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public function getById(int $id) |
| 52 | { |
| 53 | return $this->standardRepository->getById($id); |
| 54 | } |
| 55 | |
| 56 | public function delete($toDeleteEntity) |
| 57 | { |
| 58 | return $this->standardRepository->delete($toDeleteEntity); |
| 59 | } |
| 60 | |
BacLuc | 9a3bc69 | 2020-04-29 23:06:05 +0200 | [diff] [blame^] | 61 | public function getCancellationsOfEvent(int $eventId, int $offset, int $limit) |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 62 | { |
| 63 | $qb = $this->entityManager->createQueryBuilder(); |
| 64 | $qb->select('cancellation') |
| 65 | ->from(EventCancellation::class, "cancellation") |
| 66 | ->join("cancellation.event", "event") |
| 67 | ->where($qb->expr()->eq("event.id", ":eventId")) |
BacLuc | 9a3bc69 | 2020-04-29 23:06:05 +0200 | [diff] [blame^] | 68 | ->setFirstResult($offset) |
| 69 | ->setMaxResults($limit) |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 70 | ->orderBy('cancellation.name') |
| 71 | ->setParameter("eventId", $eventId); |
| 72 | $query = $qb->getQuery(); |
| 73 | return $query->getResult(); |
| 74 | } |
BacLuc | e20c51b | 2020-04-29 20:55:33 +0200 | [diff] [blame] | 75 | |
BacLuc | b133398 | 2020-04-29 21:27:52 +0200 | [diff] [blame] | 76 | public function countCancellationsOfEvent(int $eventId) |
| 77 | { |
| 78 | $qb = $this->entityManager->createQueryBuilder(); |
| 79 | $qb->select('count(cancellation)') |
| 80 | ->from(EventCancellation::class, "cancellation") |
| 81 | ->join("cancellation.event", "event") |
| 82 | ->where($qb->expr()->eq("event.id", ":eventId")) |
| 83 | ->orderBy('cancellation.name') |
| 84 | ->setParameter("eventId", $eventId); |
| 85 | $query = $qb->getQuery(); |
| 86 | try { |
| 87 | return $query->getSingleScalarResult(); |
| 88 | } catch (NoResultException | NonUniqueResultException $e) { |
| 89 | throw new RuntimeException("Error getting count of result " . $e->getMessage()); |
| 90 | } |
| 91 | } |
| 92 | |
BacLuc | e20c51b | 2020-04-29 20:55:33 +0200 | [diff] [blame] | 93 | public function count() |
| 94 | { |
| 95 | $this->standardRepository->count(); |
| 96 | } |
BacLuc | 4b5de06 | 2020-04-05 16:03:46 +0200 | [diff] [blame] | 97 | } |