UninstallCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A eventDispatcher() 0 3 1
A description() 0 3 1
A doConfigure() 0 8 1
A lazyEnvironmentValues() 0 7 1
A confirm() 0 8 2
A __construct() 0 17 1
A optionProviders() 0 6 1
A doExecute() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Command;
4
5
use Cocotte\Console\AbstractCommand;
6
use Cocotte\Console\DocumentedCommand;
7
use Cocotte\Console\Style;
8
use Cocotte\DigitalOcean\ApiToken;
9
use Cocotte\DigitalOcean\ApiTokenOptionProvider;
10
use Cocotte\DigitalOcean\NetworkingConfigurator;
11
use Cocotte\Environment\LazyEnvironment;
12
use Cocotte\Help\DefaultExamples;
13
use Cocotte\Host\HostMountRequired;
14
use Cocotte\Machine\MachineName;
15
use Cocotte\Machine\MachineNameOptionProvider;
16
use Cocotte\Machine\MachineState;
17
use Cocotte\Machine\MachineStoragePath;
18
use Cocotte\Shell\ProcessRunner;
19
use Cocotte\Template\Traefik\TraefikHostname;
20
use Cocotte\Template\Traefik\TraefikHostnameOptionProvider;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\Process\Process;
25
26
final class UninstallCommand extends AbstractCommand implements LazyEnvironment, HostMountRequired, DocumentedCommand
27
{
28
    /**
29
     * @var ProcessRunner
30
     */
31
    private $processRunner;
32
33
    /**
34
     * @var NetworkingConfigurator
35
     */
36
    private $networkingConfigurator;
37
38
    /**
39
     * @var TraefikHostname
40
     */
41
    private $traefikHostname;
42
43
    /**
44
     * @var Style
45
     */
46
    private $style;
47
48
    /**
49
     * @var MachineName
50
     */
51
    private $machineName;
52
53
    /**
54
     * @var EventDispatcherInterface
55
     */
56
    private $eventDispatcher;
57
58
    /**
59
     * @var MachineState
60
     */
61
    private $machineState;
62
63
    public function __construct(
64
        ProcessRunner $processRunner,
65
        NetworkingConfigurator $networkingConfigurator,
66
        TraefikHostname $traefikHostname,
67
        Style $style,
68
        MachineName $machineName,
69
        EventDispatcherInterface $eventDispatcher,
70
        MachineState $machineState
71
    ) {
72
        $this->processRunner = $processRunner;
73
        $this->networkingConfigurator = $networkingConfigurator;
74
        $this->traefikHostname = $traefikHostname;
75
        $this->style = $style;
76
        $this->machineName = $machineName;
77
        $this->eventDispatcher = $eventDispatcher;
78
        $this->machineState = $machineState;
79
        parent::__construct();
80
    }
81
82
    public function lazyEnvironmentValues(): array
83
    {
84
        return [
85
            ApiToken::class,
86
            MachineName::class,
87
            MachineStoragePath::class,
88
            TraefikHostname::class,
89
        ];
90
    }
91
92
    public function optionProviders(): array
93
    {
94
        return [
95
            ApiTokenOptionProvider::class,
96
            MachineNameOptionProvider::class,
97
            TraefikHostnameOptionProvider::class,
98
        ];
99
    }
100
101
    protected function eventDispatcher(): EventDispatcherInterface
102
    {
103
        return $this->eventDispatcher;
104
    }
105
106
    protected function doConfigure(): void
107
    {
108
        $this->setName('uninstall')
109
            ->setDescription($this->description())
110
            ->setHelp(
111
                $this->formatHelp($this->description(),
112
                    (new DefaultExamples)->uninstall(),
113
                    (new DefaultExamples)->uninstallInteractive()
114
                )
115
            );
116
    }
117
118
    protected function doExecute(InputInterface $input, OutputInterface $output)
119
    {
120
        $this->confirm();
121
        $this->networkingConfigurator->remove(
122
            $this->traefikHostname->toHostnameCollection()
123
        );
124
        if (!$this->machineState->exists()) {
125
            $this->style->verbose("Machine '{$this->machineName->toString()}' did not exist");
126
        } else {
127
            $this->processRunner->mustRun(Process::fromShellCommandline('docker-machine rm -f "${MACHINE_NAME}"'));
128
        }
129
        $this->style->complete("Machine is uninstalled and domain record is removed.");
130
    }
131
132
    private function confirm(): void
133
    {
134
        if (!$this->style->confirm(
135
            "You are about to uninstall a Docker machine named '<options=bold>{$this->machineName->toString()}</>' ".
136
            "on Digital Ocean \n and remove the domain record '<options=bold>{$this->traefikHostname->toString()}</>' ".
137
            "associated with \n this machine."
138
        )) {
139
            throw new \Exception('Cancelled');
140
        };
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    private function description(): string
147
    {
148
        return 'Destroy the Docker machine on Digital Ocean and remove the Traefik subdomain.';
149
    }
150
}
151