Passed
Pull Request — master (#3)
by David
02:18
created

EventCommand::getAllEventNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheAentMachine;
5
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Logger\ConsoleLogger;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
abstract class EventCommand extends Command
14
{
15
    /**
16
     * @var LoggerInterface
17
     */
18
    protected $log;
19
    /**
20
     * @var InputInterface
21
     */
22
    protected $input;
23
    /**
24
     * @var OutputInterface
25
     */
26
    protected $output;
27
28
    abstract protected function getEventName(): string;
29
    abstract protected function executeEvent(?string $payload): void;
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setName($this->getEventName())
35
            ->setDescription('Handle the "' . $this->getEventName() . '" event')
36
            ->addArgument('payload', InputArgument::OPTIONAL, 'The event payload');
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output): void
40
    {
41
        // Let's send the list of caught events to Hercule
42
        Hercule::setHandledEvents($this->getAllEventNames());
43
44
        $logLevelConfigurator = new LogLevelConfigurator($output);
45
        $logLevelConfigurator->configureLogLevel();
46
47
        $this->log = new ConsoleLogger($output);
48
49
        $payload = $input->getArgument('payload');
50
        $this->input = $input;
51
        $this->output = $output;
52
53
        $this->executeEvent($payload);
54
    }
55
56
    /**
57
     * @return string[]
58
     */
59
    private function getAllEventNames(): array
60
    {
61
        return array_map(function (EventCommand $event) {
62
            return $event->getEventName();
63
        }, \array_filter($this->getApplication()->all(), function (Command $command) {
64
            return $command instanceof EventCommand;
65
        }));
66
    }
67
}
68