Completed
Pull Request — master (#16)
by
unknown
02:43
created

DeleteServiceEventCommand::executeJsonEvent()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 16
nop 1
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Command;
4
5
use Symfony\Component\Console\Question\ConfirmationQuestion;
6
use Symfony\Component\Yaml\Yaml;
7
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService;
8
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools;
9
use TheAentMachine\JsonEventCommand;
10
11
class DeleteServiceEventCommand extends JsonEventCommand
12
{
13
    protected function getEventName(): string
14
    {
15
        return 'DELETE_SERVICE';
16
    }
17
18
    protected function executeJsonEvent(array $payload): ?array
19
    {
20
        $dockerComposeService = new DockerComposeService($this->log);
21
        $dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames();
22
23
        $this->log->debug(json_encode($payload, JSON_PRETTY_PRINT));
24
        $helper = $this->getHelper('question');
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
                $question = $this->getDeleteConfirmationQuestion($toDelete);
30
                $doDelete = $helper->ask($this->input, $this->output, $question);
31
                if ($doDelete) {
32
                    $this->log->info('deleting ' . $toDelete . ' in ' . $file);
33
                    YamlTools::delete($toDelete, $file, $file);
34
                }
35
            }
36
            if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) {
37
                foreach ($payload['namedVolumes'] as $namedVolume) {
38
                    if (array_key_exists($namedVolume, $ymlData['volumes'])) {
39
                        $toDelete = 'volumes.' . $namedVolume;
40
                        $question = $this->getDeleteConfirmationQuestion($toDelete);
41
                        $doDelete = $helper->ask($this->input, $this->output, $question);
42
                        if ($doDelete) {
43
                            $this->log->info('deleting ' . $toDelete . ' in ' . $file);
44
                            YamlTools::delete($toDelete, $file, $file);
45
                        }
46
                    }
47
                }
48
            }
49
        }
50
        return null;
51
    }
52
53
    /**
54
     * @param string $elementToDel
55
     * @param bool $default
56
     * @return ConfirmationQuestion
57
     */
58
    private function getDeleteConfirmationQuestion(string $elementToDel, bool $default = false): ConfirmationQuestion
59
    {
60
        return new ConfirmationQuestion(
61
            "Do you want to delete $elementToDel ? (y/n)\n > ",
62
            $default
63
        );
64
    }
65
}
66