blob: f8ebd106c15aee72afbc11a669f64db86da68cf8 [file] [log] [blame]
BacLuc4b5de062020-04-05 16:03:46 +02001<?php
2
3
4namespace BaclucEventPackage;
5
6
7use BaclucC5Crud\Entity\Repository;
8use Doctrine\ORM\EntityManager;
9use Doctrine\ORM\EntityManagerInterface;
BacLucb1333982020-04-29 21:27:52 +020010use Doctrine\ORM\NonUniqueResultException;
11use Doctrine\ORM\NoResultException;
12use RuntimeException;
BacLuc4b5de062020-04-05 16:03:46 +020013
14class 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 */
BacLuce20c51b2020-04-29 20:55:33 +020046 public function getAll(int $offset = 0, int $limit = null)
BacLuc4b5de062020-04-05 16:03:46 +020047 {
BacLuce20c51b2020-04-29 20:55:33 +020048 return $this->standardRepository->getAll($offset, $limit);
BacLuc4b5de062020-04-05 16:03:46 +020049 }
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
BacLuc9a3bc692020-04-29 23:06:05 +020061 public function getCancellationsOfEvent(int $eventId, int $offset, int $limit)
BacLuc4b5de062020-04-05 16:03:46 +020062 {
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"))
BacLuc9a3bc692020-04-29 23:06:05 +020068 ->setFirstResult($offset)
69 ->setMaxResults($limit)
BacLuc4b5de062020-04-05 16:03:46 +020070 ->orderBy('cancellation.name')
71 ->setParameter("eventId", $eventId);
72 $query = $qb->getQuery();
73 return $query->getResult();
74 }
BacLuce20c51b2020-04-29 20:55:33 +020075
BacLucb1333982020-04-29 21:27:52 +020076 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
BacLuce20c51b2020-04-29 20:55:33 +020093 public function count()
94 {
95 $this->standardRepository->count();
96 }
BacLuc4b5de062020-04-05 16:03:46 +020097}