*/ class QuizRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry, private readonly LoggerInterface $logger) { parent::__construct($registry, Quiz::class); } /** @throws ErrorClearingQuizException */ public function clearQuiz(Quiz $quiz): void { $em = $this->getEntityManager(); $em->beginTransaction(); try { $em->createQuery(<<setParameter('quiz', $quiz) ->execute(); $em->createQuery(<<setParameter('quiz', $quiz) ->execute(); $em->createQuery(<<setParameter('quiz', $quiz) ->execute(); } // @codeCoverageIgnoreStart catch (\Throwable $throwable) { $this->logger->error($throwable->getMessage()); $em->rollback(); throw new ErrorClearingQuizException(previous: $throwable); } // @codeCoverageIgnoreEnd $em->commit(); } public function deleteQuiz(Quiz $quiz): void { $this->getEntityManager()->remove($quiz); $this->getEntityManager()->flush(); } /** * @throws DatetimeException * * @return list */ public function getScores(Quiz $quiz): array { $result = $this->getEntityManager()->createQuery(<<setParameter('quiz', $quiz)->getResult(); return array_map(static fn (array $row): Result => new Result( id: $row['id'], name: $row['name'], correct: (int) $row['correct'], corrections: $row['corrections'], time: $row['start_time']->diff(new DateTimeImmutable($row['end_time'])), score: $row['score'], ), $result); } }