entityRepository = $em->getRepository(Invite::class); } public function find($id): ?Invite { return $this->entityRepository->find($id); } /** * @return array|Invite[] */ public function findAll(): array { return $this->entityRepository->findAll(); } /** * @return array|Invite[] */ public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array { return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset); } public function findOneBy(array $criteria): ?Invite { return $this->entityRepository->findOneBy($criteria); } /** * Find accepted invites for a user within a date range. * * @return array|Invite[] */ public function findAcceptedInvitesByUserAndDateRange(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): array { return $this->buildAcceptedInviteByUserAndDateRangeQuery($user, $from, $to) ->getQuery() ->getResult(); } /** * Count accepted invites for a user within a date range. */ public function countAcceptedInvitesByUserAndDateRange(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): int { return $this->buildAcceptedInviteByUserAndDateRangeQuery($user, $from, $to) ->select('COUNT(c)') ->getQuery() ->getSingleScalarResult(); } public function buildAcceptedInviteByUserAndDateRangeQuery(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to) { $qb = $this->entityRepository->createQueryBuilder('i'); return $qb ->join('i.calendar', 'c') ->where( $qb->expr()->andX( $qb->expr()->eq('i.user', ':user'), $qb->expr()->eq('i.status', ':status'), $qb->expr()->gte('c.startDate', ':startDate'), $qb->expr()->lte('c.endDate', ':endDate'), $qb->expr()->isNull('c.cancelReason') ) ) ->setParameters([ 'user' => $user, 'status' => Invite::ACCEPTED, 'startDate' => $from, 'endDate' => $to, ]); } public function getClassName(): string { return Invite::class; } }