SelfSchedulingCommandSubscriber   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 10
c 0
b 0
f 0
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 12 3
A __construct() 0 3 1
A getSubscribedEvents() 0 3 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\EventListener;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent;
8
use Zenstruck\ScheduleBundle\Schedule\SelfSchedulingCommand;
9
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class SelfSchedulingCommandSubscriber implements EventSubscriberInterface
15
{
16
    /** @var iterable<SelfSchedulingCommand> */
17
    private $commands;
18
19
    /**
20
     * @param iterable<SelfSchedulingCommand> $commands
21 2
     */
22
    public function __construct(iterable $commands)
23 2
    {
24 2
        $this->commands = $commands;
25
    }
26 2
27
    public static function getSubscribedEvents(): array
28 2
    {
29
        return [BuildScheduleEvent::class => 'build'];
30
    }
31 2
32
    public function build(BuildScheduleEvent $event): void
33 2
    {
34 2
        foreach ($this->commands as $command) {
35 1
            if (!$command instanceof Command) {
36
                throw new \InvalidArgumentException(\sprintf('"%s" is not a console command. "%s" can only be used on commands.', \get_class($command), SelfSchedulingCommand::class));
37
            }
38 1
39
            $task = new CommandTask((string) $command->getName());
40 1
41
            $command->schedule($task);
0 ignored issues
show
Bug introduced by
The method schedule() does not exist on Symfony\Component\Console\Command\Command. It seems like you code against a sub-type of Symfony\Component\Console\Command\Command such as anonymous//tests/Schedul...dulingCommandTest.php$0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $command->/** @scrutinizer ignore-call */ 
42
                      schedule($task);
Loading history...
42 1
43
            $event->getSchedule()->add($task);
44 1
        }
45
    }
46
}
47