70 lines
1.8 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Repository\CalendarRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use function count;
class CalendarForShortMessageProvider
{
private CalendarRepository $calendarRepository;
private EntityManagerInterface $em;
private RangeGeneratorInterface $rangeGenerator;
public function __construct(
CalendarRepository $calendarRepository,
EntityManagerInterface $em,
RangeGeneratorInterface $rangeGenerator
) {
$this->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);
}
}