Completed
Pull Request — master (#13)
by
unknown
02:37
created

NewDockerServiceInfoEventCommand::arrayFilterRec()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Command;
4
5
use Symfony\Component\Console\Question\ChoiceQuestion;
6
use Symfony\Component\Yaml\Yaml;
7
use TheAentMachine\AentDockerCompose\Aenthill\Enum\EventEnum;
8
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService;
9
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools;
10
use TheAentMachine\JsonEventCommand;
11
use TheAentMachine\Service\Service;
12
13
class NewDockerServiceInfoEventCommand extends JsonEventCommand
14
{
15
16
    protected function getEventName(): string
17
    {
18
        return EventEnum::NEW_DOCKER_SERVICE_INFO;
19
    }
20
21
    protected function executeJsonEvent(array $payload): void
22
    {
23
        $service = Service::parsePayload($payload);
24
        $formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service);
25
26
        // $this->log->debug(json_encode($formattedPayload, JSON_PRETTY_PRINT));
27
28
        $yml = Yaml::dump($formattedPayload, 256, 4, Yaml::DUMP_OBJECT_AS_MAP);
29
        file_put_contents(YamlTools::TMP_YAML_FILE, $yml);
30
        DockerComposeService::checkDockerComposeFileValidity(YamlTools::TMP_YAML_FILE);
31
32
        $dockerComposeService = new DockerComposeService($this->log);
33
        $dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames();
34
        if (count($dockerComposeFilePathnames) === 1) {
35
            $toMerge = $dockerComposeFilePathnames;
36
        } else {
37
            $helper = $this->getHelper('question');
38
            $question = new ChoiceQuestion(
39
                'Please choose the docker-compose file(s) in which the service will be added (e.g. 0,1) : ',
40
                $dockerComposeFilePathnames,
41
                null
42
            );
43
            $question->setMultiselect(true);
44
45
            $toMerge = $helper->ask($this->input, $this->output, $question);
46
        }
47
48
        foreach ($toMerge as $file) {
49
            YamlTools::merge($file, YamlTools::TMP_YAML_FILE, $file);
50
        }
51
52
        unlink(YamlTools::TMP_YAML_FILE);
53
    }
54
}
55