settings = $settings; $this->entityManager = $entityManager; $this->repository = $repository; $this->trackingConfig = $trackingConfig; } public function schedule(NewsletterEntity $newsletter) { if (!$this->shouldSchedule($newsletter)) { return false; } $task = new ScheduledTaskEntity(); $task->setType(Worker::TASK_TYPE); $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); $task->setScheduledAt($this->getNextRunDate()); $this->entityManager->persist($task); $this->entityManager->flush(); $statsNotifications = new StatsNotificationEntity($newsletter, $task); $this->entityManager->persist($statsNotifications); $this->entityManager->flush(); } private function shouldSchedule(NewsletterEntity $newsletter) { if ($this->isDisabled()) { return false; } if (!in_array($newsletter->getType(), $this->supportedTypes)) { return false; } if ($this->hasTaskBeenScheduled($newsletter->getId())) { return false; } return true; } private function isDisabled() { $settings = $this->settings->get(Worker::SETTINGS_KEY); if (!is_array($settings)) { return true; } if (!isset($settings['enabled'])) { return true; } if (!isset($settings['address'])) { return true; } if (empty(trim($settings['address']))) { return true; } if (!$this->trackingConfig->isEmailTrackingEnabled()) { return true; } return !(bool)$settings['enabled']; } private function hasTaskBeenScheduled($newsletterId) { $existing = $this->repository->findOneByNewsletterId($newsletterId); return $existing instanceof StatsNotificationEntity; } private function getNextRunDate() { $date = new Carbon(); $date->addHours(self::HOURS_TO_SEND_AFTER_NEWSLETTER); return $date; } }