Completed
Push — master ( 52ead2...fd316a )
by David
12s queued 10s
created

DeleteDockerServiceEventCommand::getEventName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Command;
4
5
use Symfony\Component\Yaml\Yaml;
6
use TheAentMachine\AentDockerCompose\Aenthill\Enum\EventEnum;
7
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService;
8
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools;
9
use TheAentMachine\JsonEventCommand;
10
11
class DeleteDockerServiceEventCommand extends JsonEventCommand
12
{
13
    protected function getEventName(): string
14
    {
15
        return EventEnum::DELETE_DOCKER_SERVICE;
16
    }
17
18
    protected function executeJsonEvent(array $payload): void
19
    {
20
        $dockerComposeService = new DockerComposeService($this->log);
21
        $dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames();
22
23
        print_r($payload);
24
25
        foreach ($dockerComposeFilePathnames as $file) {
26
            $ymlData = Yaml::parseFile($file);
27
            if (array_key_exists('serviceName', $ymlData) && array_key_exists($payload['serviceName'], $ymlData['services'])) {
28
                $toDelete = 'services.' . $payload['serviceName'];
29
                $this->log->info('deleting ' . $toDelete . ' in ' . $file);
30
                YamlTools::delete($toDelete, $file, $file);
31
            }
32
            if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) {
33
                foreach ($payload['namedVolumes'] as $namedVolume) {
34
                    if (array_key_exists($namedVolume, $ymlData['volumes'])) {
35
                        $toDelete = 'volumes.' . $namedVolume;
36
                        $this->log->info('deleting ' . $toDelete . ' in ' . $file);
37
                        YamlTools::delete($toDelete, $file, $file);
38
                    }
39
                }
40
            }
41
        }
42
    }
43
}
44