Passed
Pull Request — master (#30)
by Jindun
02:40
created

CommonEvents::canDispatchServiceOrFail()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 3
dl 0
loc 24
rs 9.2248
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_SERVICE = 'NEW_SERVICE';
16
    private const NEW_VIRTUAL_HOST = 'NEW_VIRTUAL_HOST';
17
    private const NEW_IMAGE = 'NEW_IMAGE';
18
19
    /**
20
     * @throws CannotHandleEventException
21
     */
22
    public function dispatchService(Service $service, QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
23
    {
24
        $this->canDispatchServiceOrFail($helper, $input, $output);
25
26
        Hermes::dispatchJson(self::NEW_SERVICE, $service);
27
    }
28
29
    /**
30
     * @throws CannotHandleEventException
31
     */
32
    public function canDispatchServiceOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
33
    {
34
        $canHandle = Hermes::canHandleEvent(self::NEW_SERVICE);
35
36
        if (!$canHandle) {
37
            $output->writeln('<error>Heads up!</error>');
38
            $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.');
39
            $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>.');
40
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
41
            $question->setValidator(function (string $value) {
42
                $value = \strtolower(trim($value));
43
44
                if ($value !== 'y' && $value !== 'n') {
45
                    throw new \InvalidArgumentException('Please type "y" or "n"');
46
                }
47
48
                return $value;
49
            });
50
            $answer = $helper->ask($input, $output, $question);
51
52
            if ($answer === 'y') {
53
                Hermes::setDependencies(['theaentmachine/aent-docker-compose']);
54
            } else {
55
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_SERVICE);
56
            }
57
        }
58
    }
59
60
    /**
61
     * @throws CannotHandleEventException
62
     * @return array[] Returns the responses
63
     */
64
    public function dispatchNewVirtualHost(QuestionHelper $helper, InputInterface $input, OutputInterface $output, string $serviceName, int $virtualPort = 80, string $virtualHost = null): ?array
65
    {
66
        $this->canDispatchVirtualHostOrFail($helper, $input, $output);
67
68
        $message = [
69
            'service' => $serviceName,
70
            'virtualPort' => $virtualPort
71
        ];
72
        if ($virtualHost !== null) {
73
            $message['virtualHost'] = $virtualHost;
74
        }
75
76
        return Hermes::dispatchJson(self::NEW_VIRTUAL_HOST, $message);
77
    }
78
79
    /**
80
     * @throws CannotHandleEventException
81
     */
82
    public function canDispatchVirtualHostOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
83
    {
84
        $canHandle = Hermes::canHandleEvent(self::NEW_VIRTUAL_HOST);
85
86
        if (!$canHandle) {
87
            $output->writeln('<error>Heads up!</error>');
88
            $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.');
89
            $output->writeln('Traefik is a good reverse proxy. We have an Aent to add Traefik to your project: <info>theaentmachine/aent-traefik</info>.');
90
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
91
            $question->setValidator(function (string $value) {
92
                $value = \strtolower(trim($value));
93
94
                if ($value !== 'y' && $value !== 'n') {
95
                    throw new \InvalidArgumentException('Please type "y" or "n"');
96
                }
97
98
                return $value;
99
            });
100
            $answer = $helper->ask($input, $output, $question);
101
102
            if ($answer === 'y') {
103
                Hermes::setDependencies(['theaentmachine/aent-traefik']);
104
            } else {
105
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_VIRTUAL_HOST);
106
            }
107
        }
108
    }
109
110
    /**
111
     * @throws CannotHandleEventException
112
     */
113
    public function dispatchImage(Service $service, QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
114
    {
115
        $this->canDispatchImageOrFail($helper, $input, $output);
116
117
        Hermes::dispatchJson(self::NEW_IMAGE, $service);
118
    }
119
120
    /**
121
     * @throws CannotHandleEventException
122
     */
123
    public function canDispatchImageOrFail(QuestionHelper $helper, InputInterface $input, OutputInterface $output): void
124
    {
125
        $canHandle = Hermes::canHandleEvent(self::NEW_IMAGE);
126
127
        if (!$canHandle) {
128
            $output->writeln('<error>Heads up!</error>');
129
            $output->writeln('It seems that Aenthill does not know how to handle the creation of a new image. You need to install a dedicated Aent for this.');
130
            $output->writeln('Most of the time, you want to put the commands in a Dockerfile. We have a pretty good Aent for this: <info>theaentmachine/aent-dockerfile</info>.');
131
            $question = new Question('Do you want me to add this Aent for you? (y/n) ', 'y');
132
            $question->setValidator(function (string $value) {
133
                $value = \strtolower(trim($value));
134
135
                if ($value !== 'y' && $value !== 'n') {
136
                    throw new \InvalidArgumentException('Please type "y" or "n"');
137
                }
138
139
                return $value;
140
            });
141
            $answer = $helper->ask($input, $output, $question);
142
143
            if ($answer === 'y') {
144
                Hermes::setDependencies(['theaentmachine/aent-dockerfile']);
145
            } else {
146
                throw CannotHandleEventException::cannotHandleEvent(self::NEW_IMAGE);
147
            }
148
        }
149
    }
150
}
151