Completed
Pull Request — master (#15)
by David
07:17
created

CommonEvents::dispatchService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
    private const NEW_VIRTUAL_HOST = 'NEW_VIRTUAL_HOST';
17
18
    /**
19
     * @throws CannotHandleEventException
20
     */
21
    public function dispatchService(Service $service, QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
22
    {
23
        $this->canDispatchServiceOrFail($helper, $input, $output);
24
25
        Hermes::dispatchJson(self::NEW_DOCKER_SERVICE_INFO, $service);
26
    }
27
28
    /**
29
     * @throws CannotHandleEventException
30
     */
31
    public function canDispatchServiceOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
32
    {
33
        $canHandle = Hermes::canHandleEvent(self::NEW_DOCKER_SERVICE_INFO);
34
35
        if (!$canHandle) {
36
            $output->writeln('<error>Heads up!</error>');
37
            $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.');
38
            $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>.');
39
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
40
            $question->setValidator(function (string $value) {
41
                $value = \strtolower(trim($value));
42
43
                if ($value !== 'y' && $value !== 'n') {
44
                    throw new \InvalidArgumentException('Please type "y" or "n"');
45
                }
46
47
                return $value;
48
            });
49
            $answer = $helper->ask($input, $output, $question);
50
51
            if ($answer === 'y') {
52
                Hermes::setDependencies(['theaentmachine/aent-docker-compose']);
53
            } else {
54
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_DOCKER_SERVICE_INFO);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @throws CannotHandleEventException
61
     */
62
    public function dispatchNewVirtualHost(QuestionHelper $helper, InputInterface $input, OutputInterface $output, string $serviceName, string $virtualPort, string $virtualHost = null): void
63
    {
64
        $this->canDispatchVirtualHostOrFail($helper, $input, $output);
65
66
        $message = [
67
            'service' => $serviceName,
68
            'virtualPort' => $virtualPort
69
        ];
70
        if ($virtualHost !== null) {
71
            $message['virtualHost'] = $virtualHost;
72
        }
73
74
        Hermes::dispatchJson(self::NEW_DOCKER_SERVICE_INFO, $message);
75
    }
76
77
    /**
78
     * @throws CannotHandleEventException
79
     */
80
    public function canDispatchVirtualHostOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
81
    {
82
        $canHandle = Hermes::canHandleEvent(self::NEW_VIRTUAL_HOST);
83
84
        if (!$canHandle) {
85
            $output->writeln('<error>Heads up!</error>');
86
            $output->writeln('It seems that Aenthill does not know how to bind your container to a domain name. You need to install a reverse proxy for this.');
87
            $output->writeln('Traefik is a good reverse proxy. We have an Aent to add Traefik to your project: <info>theaentmachine/aent-traefik</info>.');
88
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
89
            $question->setValidator(function (string $value) {
90
                $value = \strtolower(trim($value));
91
92
                if ($value !== 'y' && $value !== 'n') {
93
                    throw new \InvalidArgumentException('Please type "y" or "n"');
94
                }
95
96
                return $value;
97
            });
98
            $answer = $helper->ask($input, $output, $question);
99
100
            if ($answer === 'y') {
101
                Hermes::setDependencies(['theaentmachine/aent-traefik']);
102
            } else {
103
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_VIRTUAL_HOST);
104
            }
105
        }
106
    }
107
}
108