diff --git a/lib/Cron/Workers/StatsNotifications/Scheduler.php b/lib/Cron/Workers/StatsNotifications/Scheduler.php index a6d15a1bc3..879476f62b 100644 --- a/lib/Cron/Workers/StatsNotifications/Scheduler.php +++ b/lib/Cron/Workers/StatsNotifications/Scheduler.php @@ -65,7 +65,7 @@ class Scheduler { if (!in_array($newsletter->getType(), $this->supported_types)) { return false; } - if ($this->isTaskScheduled($newsletter->getId())) { + if ($this->hasTaskBeenScheduled($newsletter->getId())) { return false; } return true; @@ -91,9 +91,9 @@ class Scheduler { return !(bool)$settings['enabled']; } - private function isTaskScheduled($newsletter_id) { + private function hasTaskBeenScheduled($newsletter_id) { $existing = $this->repository->findByNewsletterId($newsletter_id); - return count($existing) > 0; + return $existing instanceof StatsNotificationEntity; } private function getNextRunDate() { diff --git a/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php b/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php index 4742a3a8c0..361034c17a 100644 --- a/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php +++ b/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php @@ -15,20 +15,17 @@ class StatsNotificationsRepository extends Repository { /** * @param int $newsletter_id - * @return StatsNotificationEntity[] + * @return StatsNotificationEntity|null */ public function findByNewsletterId($newsletter_id) { return $this->doctrine_repository ->createQueryBuilder('stn') - ->join('stn.task', 'tasks') ->join('stn.newsletter', 'n') - ->addSelect('tasks') - ->where('tasks.type = :taskType') - ->setParameter('taskType', Worker::TASK_TYPE) ->andWhere('n.id = :newsletterId') ->setParameter('newsletterId', $newsletter_id) + ->setMaxResults(1) ->getQuery() - ->getResult(); + ->getOneOrNullResult(); } /** diff --git a/tests/unit/Cron/Workers/StatsNotifications/SchedulerTest.php b/tests/unit/Cron/Workers/StatsNotifications/SchedulerTest.php index e372a27ac0..301eff4cac 100644 --- a/tests/unit/Cron/Workers/StatsNotifications/SchedulerTest.php +++ b/tests/unit/Cron/Workers/StatsNotifications/SchedulerTest.php @@ -232,7 +232,7 @@ class SchedulerTest extends \MailPoetUnitTest { ->expects($this->once()) ->method('findByNewsletterId') ->with($newsletter_id) - ->willReturn([new ScheduledTaskEntity()]); + ->willReturn(new StatsNotificationEntity(new NewsletterEntity(), new ScheduledTaskEntity())); $this->entityManager ->expects($this->never()) ->method('persist');