vendor/zenstruck/schedule-bundle/src/EventListener/SelfSchedulingCommandSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace Zenstruck\ScheduleBundle\EventListener;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent;
  6. use Zenstruck\ScheduleBundle\Schedule\SelfSchedulingCommand;
  7. use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
  8. /**
  9.  * @author Kevin Bond <kevinbond@gmail.com>
  10.  */
  11. final class SelfSchedulingCommandSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var iterable<SelfSchedulingCommand> */
  14.     private $commands;
  15.     /**
  16.      * @param iterable<SelfSchedulingCommand> $commands
  17.      */
  18.     public function __construct(iterable $commands)
  19.     {
  20.         $this->commands $commands;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [BuildScheduleEvent::class => 'build'];
  25.     }
  26.     public function build(BuildScheduleEvent $event): void
  27.     {
  28.         foreach ($this->commands as $command) {
  29.             if (!$command instanceof Command) {
  30.                 throw new \InvalidArgumentException(\sprintf('"%s" is not a console command. "%s" can only be used on commands.'\get_class($command), SelfSchedulingCommand::class));
  31.             }
  32.             $task = new CommandTask((string) $command->getName());
  33.             $command->schedule($task);
  34.             $event->getSchedule()->add($task);
  35.         }
  36.     }
  37. }