mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 23:04:58 +00:00
command for sending bulk sms with tests
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user