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

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