calendarRepository = $calendarRepository; $this->em = $em; $this->rangeGenerator = $rangeGenerator; } /** * Generate calendars instance. * * Warning: this method takes care of clearing the EntityManager at regular interval * * @return iterable|Calendar[] */ public function getCalendars(DateTimeImmutable $at): iterable { ['startDate' => $startDate, 'endDate' => $endDate] = $this->rangeGenerator ->generateRange($at); $offset = 0; $batchSize = 10; $calendars = $this->calendarRepository ->findByNotificationAvailable($startDate, $endDate, $batchSize, $offset); do { foreach ($calendars as $calendar) { ++$offset; yield $calendar; } $this->em->clear(); $calendars = $this->calendarRepository ->findByNotificationAvailable($startDate, $endDate, $batchSize, $offset); } while (count($calendars) === $batchSize); } }