Passed
Pull Request — master (#17)
by Jindun
02:14
created

DeleteServiceEventCommand::getDeleteConfirmationQuestion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
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\DockerCompose\DockerComposeService;
7
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools;
8
use TheAentMachine\JsonEventCommand;
9
10
class DeleteServiceEventCommand extends JsonEventCommand
11
{
12
    protected function getEventName(): string
13
    {
14
        return 'DELETE_SERVICE';
15
    }
16
17
    protected function executeJsonEvent(array $payload): ?array
18
    {
19
        $dockerComposeService = new DockerComposeService($this->log);
20
        $dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames();
21
22
        // $this->log->debug(json_encode($payload, JSON_PRETTY_PRINT));
23
24
        foreach ($dockerComposeFilePathnames as $file) {
25
            $ymlData = Yaml::parseFile($file);
26
            if (array_key_exists('serviceName', $ymlData) && array_key_exists($payload['serviceName'], $ymlData['services'])) {
27
                $serviceName = $payload['serviceName'];
28
                $elemToDelete = ['services', $serviceName];
29
30
                $doDelete = $this->getAentHelper()
31
                    ->question("Delete the service $serviceName in $file?")
32
                    ->yesNoQuestion()
33
                    ->setDefault('n')
34
                    ->ask();
35
36
                if ($doDelete) {
37
                    $this->log->debug('deleting ' . $elemToDelete . ' in ' . $file);
0 ignored issues
show
Bug introduced by
Are you sure $elemToDelete of type array<integer,mixed|string> can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                    $this->log->debug('deleting ' . /** @scrutinizer ignore-type */ $elemToDelete . ' in ' . $file);
Loading history...
38
                    YamlTools::deleteYamlItem($elemToDelete, $file);
39
                }
40
            }
41
            if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) {
42
                foreach ($payload['namedVolumes'] as $namedVolume) {
43
                    if (array_key_exists($namedVolume, $ymlData['volumes'])) {
44
                        $elemToDelete = ['volumes', $namedVolume];
45
46
                        $doDelete = $this->getAentHelper()
47
                            ->question("Delete the named volume $namedVolume in $file?")
48
                            ->yesNoQuestion()
49
                            ->setDefault('n')
50
                            ->ask();
51
52
                        if ($doDelete) {
53
                            $this->log->debug('deleting ' . $elemToDelete . ' in ' . $file);
54
                            YamlTools::deleteYamlItem($elemToDelete, $file);
55
                        }
56
                    }
57
                }
58
            }
59
        }
60
        return null;
61
    }
62
}
63