Refactor Sending::saveSubscriberError() to use Doctrine instead of Paris
[MAILPOET-4368]
This commit is contained in:
committed by
Aschepikov
parent
11f8de4176
commit
8b4e747499
@@ -150,6 +150,20 @@ class ScheduledTaskSubscribersRepository extends Repository {
|
||||
}
|
||||
}
|
||||
|
||||
public function saveError(ScheduledTaskEntity $scheduledTask, int $subscriberId, string $errorMessage): void {
|
||||
$scheduledTaskSubscriber = $this->findOneBy(['task' => $scheduledTask, 'subscriber' => $subscriberId]);
|
||||
|
||||
if ($scheduledTaskSubscriber instanceof ScheduledTaskSubscriberEntity) {
|
||||
$scheduledTaskSubscriber->setFailed(ScheduledTaskSubscriberEntity::FAIL_STATUS_FAILED);
|
||||
$scheduledTaskSubscriber->setProcessed(ScheduledTaskSubscriberEntity::STATUS_PROCESSED);
|
||||
$scheduledTaskSubscriber->setError($errorMessage);
|
||||
$this->persist($scheduledTaskSubscriber);
|
||||
$this->flush();
|
||||
|
||||
$this->checkCompleted($scheduledTask);
|
||||
}
|
||||
}
|
||||
|
||||
private function checkCompleted(ScheduledTaskEntity $task): void {
|
||||
$count = $this->countBy(['task' => $task, 'processed' => ScheduledTaskSubscriberEntity::STATUS_UNPROCESSED]);
|
||||
if ($count === 0) {
|
||||
|
@@ -278,13 +278,7 @@ class Sending {
|
||||
public function removeSubscribers(array $subscriberIds) {
|
||||
$this->scheduledTaskSubscribersRepository->deleteByScheduledTaskAndSubscriberIds($this->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 = $this->scheduledTaskEntity->getStatus();
|
||||
if (!is_null($this->scheduledTaskEntity->getProcessedAt())) {
|
||||
$this->task->processedAt = $this->scheduledTaskEntity->getProcessedAt()->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$this->updateTaskStatus();
|
||||
$this->updateCount();
|
||||
}
|
||||
|
||||
@@ -296,10 +290,22 @@ class Sending {
|
||||
}
|
||||
|
||||
public function saveSubscriberError($subcriberId, $errorMessage) {
|
||||
$this->taskSubscribers->saveSubscriberError($subcriberId, $errorMessage);
|
||||
$this->scheduledTaskSubscribersRepository->saveError($this->scheduledTaskEntity, $subcriberId, $errorMessage);
|
||||
|
||||
$this->updateTaskStatus();
|
||||
|
||||
return $this->updateCount()->getErrors() === false;
|
||||
}
|
||||
|
||||
private function updateTaskStatus() {
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
|
||||
public function updateCount(?int $count = null) {
|
||||
if ($count) {
|
||||
// increment/decrement counts based on known subscriber count, don't exceed the bounds
|
||||
|
@@ -17,21 +17,4 @@ class Subscribers {
|
||||
public function getSubscribers() {
|
||||
return ScheduledTaskSubscriber::where('task_id', $this->task->id);
|
||||
}
|
||||
|
||||
public function saveSubscriberError($subcriberId, $errorMessage) {
|
||||
$this->getSubscribers()
|
||||
->where('subscriber_id', $subcriberId)
|
||||
->findResultSet()
|
||||
->set('failed', ScheduledTaskSubscriber::FAIL_STATUS_FAILED)
|
||||
->set('processed', ScheduledTaskSubscriber::STATUS_PROCESSED)
|
||||
->set('error', $errorMessage)
|
||||
->save();
|
||||
$this->checkCompleted();
|
||||
}
|
||||
|
||||
private function checkCompleted($count = null) {
|
||||
if (!$count && !ScheduledTaskSubscriber::getUnprocessedCount($this->task->id)) {
|
||||
$this->task->complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user