From 5b6b21b23f4b67effd4a069f28d644225c445ecb Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Wed, 30 Oct 2019 10:53:25 +0100 Subject: [PATCH] Make method more clear [MAILPOET-2439] --- lib/Cron/Workers/StatsNotifications/Scheduler.php | 6 +++--- .../StatsNotifications/StatsNotificationsRepository.php | 9 +++------ .../Cron/Workers/StatsNotifications/SchedulerTest.php | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) 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');