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

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\Console\Command\Command;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent;
  14. use Zenstruck\ScheduleBundle\Schedule\SelfSchedulingCommand;
  15. use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
  16. /**
  17.  * @author Kevin Bond <kevinbond@gmail.com>
  18.  */
  19. final class SelfSchedulingCommandSubscriber implements EventSubscriberInterface
  20. {
  21.     /** @var iterable<SelfSchedulingCommand> */
  22.     private $commands;
  23.     /**
  24.      * @param iterable<SelfSchedulingCommand> $commands
  25.      */
  26.     public function __construct(iterable $commands)
  27.     {
  28.         $this->commands $commands;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [BuildScheduleEvent::class => 'build'];
  33.     }
  34.     public function build(BuildScheduleEvent $event): void
  35.     {
  36.         foreach ($this->commands as $command) {
  37.             if (!$command instanceof Command) {
  38.                 throw new \InvalidArgumentException(\sprintf('"%s" is not a console command. "%s" can only be used on commands.'$command::class, SelfSchedulingCommand::class));
  39.             }
  40.             $task = new CommandTask((string) $command->getName());
  41.             $command->schedule($task);
  42.             $event->getSchedule()->add($task);
  43.         }
  44.     }
  45. }