From c0a091de0449cf7b71d800a6cc996b6c36aa0a00 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 7 Jul 2023 09:31:03 -0300 Subject: [PATCH] Add the ScheduledTaskEntity as a property to the Sending class Doing this instead of having to get the ScheduledTaskEntity in several methods of this class. [MAILPOET-4368] --- mailpoet/lib/Tasks/Sending.php | 42 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/mailpoet/lib/Tasks/Sending.php b/mailpoet/lib/Tasks/Sending.php index ce17f9c47c..b1f8c0e6b8 100644 --- a/mailpoet/lib/Tasks/Sending.php +++ b/mailpoet/lib/Tasks/Sending.php @@ -66,6 +66,9 @@ class Sending { /** @var ScheduledTasksRepository */ private $scheduledTasksRepository; + /** @var ScheduledTaskEntity */ + private $scheduledTaskEntity; + private function __construct( ScheduledTask $task = null, SendingQueue $queue = null @@ -91,6 +94,17 @@ class Sending { $this->taskSubscribers = new Subscribers($task); $this->scheduledTaskSubscribersRepository = ContainerWrapper::getInstance()->get(ScheduledTaskSubscribersRepository::class); $this->scheduledTasksRepository = ContainerWrapper::getInstance()->get(ScheduledTasksRepository::class); + + // needed to make sure that the task has an ID so that we can retrieve the ScheduledTaskEntity while this class still uses Paris + $this->save(); + + $scheduledTaskEntity = $this->scheduledTasksRepository->findOneById($this->task->id); + + if (!$scheduledTaskEntity instanceof ScheduledTaskEntity) { + throw new InvalidStateException('Scheduled task entity not found'); + } + + $this->scheduledTaskEntity = $scheduledTaskEntity; } public static function create(ScheduledTask $task = null, SendingQueue $queue = null) { @@ -245,29 +259,21 @@ class Sending { } public function setSubscribers(array $subscriberIds) { - $scheduledTaskEntity = $this->scheduledTasksRepository->findOneById($this->task->id); - - if ($scheduledTaskEntity instanceof ScheduledTaskEntity) { - $this->scheduledTaskSubscribersRepository->setSubscribers($scheduledTaskEntity, $subscriberIds); - $this->updateCount(); - } + $this->scheduledTaskSubscribersRepository->setSubscribers($this->scheduledTaskEntity, $subscriberIds); + $this->updateCount(); } public function removeSubscribers(array $subscriberIds) { - $scheduledTaskEntity = $this->scheduledTasksRepository->findOneById($this->task->id); + $this->scheduledTaskSubscribersRepository->deleteByScheduledTaskAndSubscriberIds($this->scheduledTaskEntity, $subscriberIds); - if ($scheduledTaskEntity instanceof ScheduledTaskEntity) { - $this->scheduledTaskSubscribersRepository->deleteByScheduledTaskAndSubscriberIds($scheduledTaskEntity, $subscriberIds); - - // we need to update those fields here as the Sending class is in a mixed state using Paris and Doctrine at the same time - // this probably won't be necessary anymore once https://mailpoet.atlassian.net/browse/MAILPOET-4375 is finished - $this->task->status = $scheduledTaskEntity->getStatus(); - if (!is_null($scheduledTaskEntity->getProcessedAt())) { - $this->task->processedAt = $scheduledTaskEntity->getProcessedAt()->format('Y-m-d H:i:s'); - } - - $this->updateCount(); + // we need to update those fields here as the Sending class is in a mixed state using Paris and Doctrine at the same time + // this probably won't be necessary anymore once https://mailpoet.atlassian.net/browse/MAILPOET-4375 is finished + $this->task->status = $this->scheduledTaskEntity->getStatus(); + if (!is_null($this->scheduledTaskEntity->getProcessedAt())) { + $this->task->processedAt = $this->scheduledTaskEntity->getProcessedAt()->format('Y-m-d H:i:s'); } + + $this->updateCount(); } public function updateProcessedSubscribers(array $processedSubscribers) {