Passed
Pull Request — master (#5)
by David
03:40 queued 13s
created

CommonEvents   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchService() 0 5 1
B canDispatchServiceOrFail() 0 24 5
1
<?php
2
3
4
namespace TheAentMachine;
5
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\Question;
10
use TheAentMachine\Exception\CannotHandleEventException;
11
use TheAentMachine\Service\Service;
12
13
class CommonEvents
14
{
15
    private const NEW_DOCKER_SERVICE_INFO = 'NEW_DOCKER_SERVICE_INFO';
16
17
    /**
18
     * @throws CannotHandleEventException
19
     */
20
    public function dispatchService(Service $service, QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
21
    {
22
        $this->canDispatchServiceOrFail($helper, $input, $output);
23
24
        Hermes::dispatchJson(self::NEW_DOCKER_SERVICE_INFO, $service);
25
    }
26
27
    /**
28
     * @throws CannotHandleEventException
29
     */
30
    public function canDispatchServiceOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
31
    {
32
        $canHandle = Hercule::canHandleEvent(self::NEW_DOCKER_SERVICE_INFO);
33
34
        if (!$canHandle) {
35
            $output->writeln('<error>Heads up!</error>');
36
            $output->writeln('It seems that Aenthill does not know how or where to store this new service. You need to install a dedicated Aent for this.');
37
            $output->writeln('Most of the time, you want to put this service in a docker-compose.yml file. We have a pretty good Aent for this: <info>theaentmachine/aent-docker-compose</info>.');
38
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
39
            $question->setValidator(function (string $value) {
40
                $value = \strtolower(trim($value));
41
42
                if ($value !== 'y' && $value !== 'n') {
43
                    throw new \InvalidArgumentException('Please type "y" or "n"');
44
                }
45
46
                return $value;
47
            });
48
            $answer = $helper->ask($input, $output, $question);
49
50
            if ($answer === 'y') {
51
                Hercule::addAent('theaentmachine/aent-docker-compose');
52
            } else {
53
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_DOCKER_SERVICE_INFO);
54
            }
55
        }
56
    }
57
}
58