WizardCommand::completeMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 14
rs 9.8666
c 0
b 0
f 0
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\InteractionOperator;
8
use Cocotte\Console\OptionProviderRegistry;
9
use Cocotte\Console\Style;
10
use Cocotte\DigitalOcean\ApiToken;
11
use Cocotte\DigitalOcean\DnsValidated;
12
use Cocotte\Help\DefaultExamples;
13
use Cocotte\Template\Traefik\TraefikHostname;
14
use Cocotte\Template\Traefik\TraefikPassword;
15
use Cocotte\Template\Traefik\TraefikUsername;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
final class WizardCommand extends AbstractCommand implements DocumentedCommand, DnsValidated
21
{
22
    /**
23
     * @var Style
24
     */
25
    private $style;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    private $eventDispatcher;
31
32
    /**
33
     * @var OptionProviderRegistry
34
     */
35
    private $optionProviderRegistry;
36
37
    /**
38
     * @var InteractionOperator
39
     */
40
    private $operator;
41
42
    public function __construct(
43
        Style $style,
44
        EventDispatcherInterface $eventDispatcher,
45
        OptionProviderRegistry $optionProviderRegistry,
46
        InteractionOperator $operator
47
    ) {
48
        $this->style = $style;
49
        $this->eventDispatcher = $eventDispatcher;
50
        $this->optionProviderRegistry = $optionProviderRegistry;
51
        $this->operator = $operator;
52
        parent::__construct();
53
    }
54
55
    public function optionProviders(): array
56
    {
57
        return [];
58
    }
59
60
    protected function eventDispatcher(): EventDispatcherInterface
61
    {
62
        return $this->eventDispatcher;
63
    }
64
65
    protected function doConfigure(): void
66
    {
67
        $this->setName('wizard')
68
            ->setDescription($this->description())
69
            ->setHelp($this->description());
70
    }
71
72
    protected function doExecute(InputInterface $input, OutputInterface $output)
73
    {
74
        $input->setInteractive(true);
75
        $this->style->help(
76
            $this->style->optionHelp("Cocotte Wizard", $this->welcomeMessage())
77
        );
78
        $this->style->pause();
79
80
        $token = $this->ask(ApiToken::OPTION_NAME);
81
        $traefikHostname = $this->ask(TraefikHostname::OPTION_NAME);
82
        $traefikUsername = $this->ask(TraefikUsername::OPTION_NAME);
83
        $traefikPassword = $this->ask(TraefikPassword::OPTION_NAME);
84
85
        $this->style->complete($this->completeMessage());
86
        $this->style->pause();
87
88
        $this->style->writeln(
89
            $this->command($token, $traefikHostname, $traefikPassword, $traefikUsername)
90
        );
91
    }
92
93
    private function ask(string $name): string
94
    {
95
        return $this->operator->ask($this->optionProviderRegistry->providerByOptionName($name));
96
    }
97
98
    private function completeMessage(): array
99
    {
100
        return [
101
            "A command will be printed to the terminal.",
102
            "Run the command from a location on your computer where you usually put\n".
103
            "new project code.",
104
            "Afterwards, two directories will be created:",
105
            "1. A 'machine' directory that you must leave there ".
106
            "and never edit. It is\n".
107
            "   used by Docker Machine to login to your cloud machine.",
108
            "2. A 'traefik' directory ".
109
            "that you can edit all you want and which is\n".
110
            "   ready for version control. This is your new Traefik project.",
111
            "Thank you for trying Cocotte!",
112
        ];
113
    }
114
115
    private function description(): string
116
    {
117
        return /** @lang text */
118
            "Interactively build a simple '<info>install</info>' command for <options=bold>Cocotte</>.";
119
    }
120
121
    private function command(
122
        string $token,
123
        string $traefikHostname,
124
        string $traefikPassword,
125
        string $traefikUsername
126
    ): string {
127
        $command = (new DefaultExamples)->install(
128
            $token,
129
            $traefikHostname,
130
            $traefikPassword,
131
            $traefikUsername
132
        );
133
134
        return <<<EOF
135
<options=bold,underscore>Run this command:</>
136
{$command}
137
138
EOF;
139
    }
140
141
    private function welcomeMessage(): array
142
    {
143
        return [
144
            "This wizard helps you build a simple '<info>install</info>' command for Cocotte.",
145
            "It assumes that you own a domain name and can change its name servers.",
146
            "Cocotte documentation: ".$this->style->link('https://github.com/chrif/cocotte'),
147
        ];
148
    }
149
}