vendor/zenstruck/schedule-bundle/src/EventListener/TaskConfigurationSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the zenstruck/schedule-bundle package.
  4.  *
  5.  * (c) Kevin Bond <kevinbond@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Zenstruck\ScheduleBundle\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent;
  13. use Zenstruck\ScheduleBundle\Schedule;
  14. use Zenstruck\ScheduleBundle\Schedule\Task;
  15. use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
  16. use Zenstruck\ScheduleBundle\Schedule\Task\CompoundTask;
  17. use Zenstruck\ScheduleBundle\Schedule\Task\PingTask;
  18. use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask;
  19. /**
  20.  * @author Kevin Bond <kevinbond@gmail.com>
  21.  */
  22. final class TaskConfigurationSubscriber implements EventSubscriberInterface
  23. {
  24.     private const PROCESS_TASK_PREFIX 'bash:';
  25.     private const PING_TASK_PREFIX 'ping:';
  26.     /** @var array */
  27.     private $config;
  28.     public function __construct(array $config)
  29.     {
  30.         $this->config $config;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [BuildScheduleEvent::class => 'configureTasks'];
  35.     }
  36.     public function configureTasks(BuildScheduleEvent $event): void
  37.     {
  38.         foreach ($this->config as $taskConfig) {
  39.             $this->addTask($event->getSchedule(), $taskConfig);
  40.         }
  41.     }
  42.     private function addTask(Schedule $schedule, array $config): void
  43.     {
  44.         $task $this->createTask($config['task']);
  45.         $task->cron($config['frequency']);
  46.         if ($config['description']) {
  47.             $task->description($config['description']);
  48.         }
  49.         if ($config['timezone']) {
  50.             $task->timezone($config['timezone']);
  51.         }
  52.         if ($config['without_overlapping']['enabled']) {
  53.             $task->withoutOverlapping($config['without_overlapping']['ttl']);
  54.         }
  55.         if ($config['only_between']['enabled']) {
  56.             $task->onlyBetween($config['only_between']['start'], $config['only_between']['end']);
  57.         }
  58.         if ($config['unless_between']['enabled']) {
  59.             $task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']);
  60.         }
  61.         if ($config['ping_before']['enabled']) {
  62.             $task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']);
  63.         }
  64.         if ($config['ping_after']['enabled']) {
  65.             $task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']);
  66.         }
  67.         if ($config['ping_on_success']['enabled']) {
  68.             $task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']);
  69.         }
  70.         if ($config['ping_on_failure']['enabled']) {
  71.             $task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']);
  72.         }
  73.         if ($config['email_after']['enabled']) {
  74.             $task->emailAfter($config['email_after']['to'], $config['email_after']['subject']);
  75.         }
  76.         if ($config['email_on_failure']['enabled']) {
  77.             $task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']);
  78.         }
  79.         $schedule->add($task);
  80.     }
  81.     private function createTask(array $commands): Task
  82.     {
  83.         if (=== \count($commands)) {
  84.             return self::createSingleTask(\array_values($commands)[0]);
  85.         }
  86.         $task = new CompoundTask();
  87.         foreach ($commands as $description => $command) {
  88.             $subTask self::createSingleTask($command);
  89.             if (!\is_numeric($description)) {
  90.                 $subTask->description($description);
  91.             }
  92.             $task->add($subTask);
  93.         }
  94.         return $task;
  95.     }
  96.     private static function createSingleTask(string $command): Task
  97.     {
  98.         if (=== \mb_strpos($commandself::PROCESS_TASK_PREFIX)) {
  99.             return new ProcessTask(self::removePrefix($commandself::PROCESS_TASK_PREFIX));
  100.         }
  101.         if (=== \mb_strpos($commandself::PING_TASK_PREFIX)) {
  102.             return new PingTask(self::removePrefix($commandself::PING_TASK_PREFIX));
  103.         }
  104.         return new CommandTask($command);
  105.     }
  106.     private static function removePrefix(string $valuestring $prefix): string
  107.     {
  108.         return \trim(\mb_substr($value\mb_strlen($prefix)));
  109.     }
  110. }