Completed
Pull Request — master (#25)
by David
02:34
created

EventCommand::getAentHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
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
     * @var AentHelper
29
     */
30
    private $aentHelper;
31
32
    abstract protected function getEventName(): string;
33
    abstract protected function executeEvent(?string $payload): ?string;
34
35
    protected function configure()
36
    {
37
        $this
38
            ->setName($this->getEventName())
39
            ->setDescription('Handle the "' . $this->getEventName() . '" event')
40
            ->addArgument('payload', InputArgument::OPTIONAL, 'The event payload');
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output): void
44
    {
45
        $this->aentHelper = new AentHelper($input, $output, $this->getHelper('question'), $this->getHelper('formatter'));
46
47
        // Let's send the list of caught events to Hercule
48
        Hermes::setHandledEvents($this->getAllEventNames());
49
50
        $logLevelConfigurator = new LogLevelConfigurator($output);
51
        $logLevelConfigurator->configureLogLevel();
52
53
        $this->log = new ConsoleLogger($output);
54
55
        $payload = $input->getArgument('payload');
56
        $this->input = $input;
57
        $this->output = $output;
58
59
        $result = $this->executeEvent($payload);
60
61
        // Now, let's send a "reply" event
62
        if ($result !== null) {
63
            Hermes::reply('reply', $result);
64
        }
65
    }
66
67
    /**
68
     * @return string[]
69
     */
70
    private function getAllEventNames(): array
71
    {
72
        return array_map(function (EventCommand $event) {
73
            return $event->getEventName();
74
        }, \array_filter($this->getApplication()->all(), function (Command $command) {
75
            return $command instanceof EventCommand && !$command->isHidden();
76
        }));
77
    }
78
79
    protected function getAentHelper(): AentHelper
80
    {
81
        if ($this->aentHelper === null) {
82
            throw new \BadMethodCallException('Function getAentHelper can only be called inside "execute(xxx)" functions.');
83
        }
84
        return $this->aentHelper;
85
    }
86
}
87