Completed
Push — master ( 8aa8da...bc3591 )
by Ruud
19:27 queued 08:08
created

InstallCommand::interact()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 34
cp 0
rs 9.232
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Question\Question;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputDefinition;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Sensio\Bundle\GeneratorBundle\Command\Validators;
14
use Symfony\Component\Console\Question\ChoiceQuestion;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Symfony\Component\Process\Exception\RuntimeException;
17
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
18
use Symfony\Component\Debug\Exception\ContextErrorException;
19
20
/**
21
 * Kunstmaan installer
22
 */
23
final class InstallCommand extends Command
24
{
25
    /**
26
     * @var int
27
     */
28
    private $commandSteps = 0;
29
30
    /** @var string */
31
    private $projectDir;
32
33
    /**
34
     * @param string $rootDir
0 ignored issues
show
Bug introduced by
There is no parameter named $rootDir. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     */
36
    public function __construct(string $projectDir)
37
    {
38
        $this->projectDir = $projectDir;
39
40
        parent::__construct();
41
    }
42
43
    protected function configure()
44
    {
45
        $this
46
            ->setName('kuma:install')
47
            ->setDescription('KunstmaanCMS installer.')
48
            ->setDefinition(
49
                new InputDefinition(
50
                    [
51
                        new InputOption('demosite', '', InputOption::VALUE_REQUIRED, 'Do you want to create a "demosite"'),
52
                        new InputOption('create-tests', '', InputOption::VALUE_REQUIRED, 'Do you want to create tests for you pages/pageparts'),
53
                        new InputOption('namespace', '', InputOption::VALUE_OPTIONAL, 'The namespace of the bundle to create (only for SF3)'),
54
                        new InputOption('dir', '', InputOption::VALUE_OPTIONAL, 'The directory where to create the bundle (only for SF3)'),
55
                        new InputOption('bundle-name', '', InputOption::VALUE_OPTIONAL, 'The optional bundle name (only for SF3)'),
56
                    ]
57
                )
58
            );
59
    }
60
61
    protected function interact(InputInterface $input, OutputInterface $output)
62
    {
63
        $questionHelper = new QuestionHelper();
64
65
        $outputStyle = new SymfonyStyle($input, $output);
66
        $outputStyle->writeln('<info>Installing KunstmaanCms...</info>');
67
        $outputStyle->writeln($this->getKunstmaanLogo());
68
69
        // Only ask namespace for Symfony 3
70
        if (Kernel::VERSION_ID < 40000) {
71
            $question = new Question(
72
                $questionHelper->getQuestion('Bundle namespace', $input->getOption('namespace')),
73
                $input->getOption('namespace')
74
            );
75
            $question->setValidator([Validators::class, 'validateBundleNamespace']);
76
            $namespace = $questionHelper->ask($input, $output, $question);
77
            $input->setOption('namespace', $namespace);
78
            $input->setOption('bundle-name', strtr($namespace, ['\\Bundle\\' => '', '\\' => '']));
79
80
            $dir = $input->getOption('dir') ?: $this->projectDir . '/src';
81
            $input->setOption('dir', $dir);
82
        }
83
84
        $question = new ChoiceQuestion(
85
            'Do you want to create a "demosite"',
86
            ['No (default)', 'Yes'],
87
            0
88
        );
89
        $question->setErrorMessage('Option "%s" is invalid.');
90
        $demoSiteOption = $questionHelper->ask($input, $output, $question);
91
        $input->setOption('demosite', $demoSiteOption);
92
93
        $question = new ChoiceQuestion(
94
            'Do you want to create tests for you pages/pageparts?',
95
            ['No (default)', 'Yes'],
96
            0
97
        );
98
        $question->setErrorMessage('Option "%s" is invalid.');
99
        $createTests = $questionHelper->ask($input, $output, $question);
100
        $input->setOption('create-tests', $createTests);
101
102
        $output->writeln('<info>Installation start</info>');
103
    }
104
105
    protected function execute(InputInterface $input, OutputInterface $output)
106
    {
107
        $defaultSiteOptions = [];
108
        if ($input->getOption('demosite') === 'Yes') {
109
            $defaultSiteOptions['--demosite'] = true;
110
        }
111
112
        if (Kernel::VERSION_ID < 40000) {
113
            $defaultSiteOptions = ['--namespace' => $input->getOption('namespace')];
114
115
            $this->executeCommand($output, 'kuma:generate:bundle', [
116
                '--namespace' => $input->getOption('namespace'),
117
                '--dir' => $input->getOption('dir'),
118
                '--bundle-name' => $input->getOption('bundle-name'),
119
            ]);
120
        }
121
122
        $this
123
            ->executeCommand($output, 'kuma:generate:default-site', $defaultSiteOptions)
124
            ->executeCommand($output, 'doctrine:database:create')
125
            ->executeCommand($output, 'doctrine:schema:drop', ['--force' => true])
126
            ->executeCommand($output, 'doctrine:schema:create')
127
            ->executeCommand($output, 'doctrine:fixtures:load')
128
        ;
129
130
        if ($input->getOption('create-tests') === 'Yes') {
131
            $adminTestOptions = [];
132
            if (Kernel::VERSION_ID < 40000) {
133
                $adminTestOptions = ['--namespace' => $input->getOption('namespace')];
134
            }
135
136
            $this->executeCommand($output, 'kuma:generate:admin-tests', $adminTestOptions);
137
        }
138
    }
139
140
    protected function executeCommand(OutputInterface $output, $command, array $options = [])
141
    {
142
        $options = array_merge(
143
            [
144
                '--no-debug' => true,
145
            ],
146
            $options
147
        );
148
149
        ++$this->commandSteps;
150
151
        try {
152
            $updateInput = new ArrayInput($options);
153
            $updateInput->setInteractive(true);
154
            $this->getApplication()->find($command)->run($updateInput, $output);
155
            $output->writeln(sprintf('<info>Step %d: "%s" - [OK]</info>', $this->commandSteps, $command));
156
        } catch (RuntimeException $exception) {
157
            $output->writeln(sprintf('<error>Step %d: "%s" - [FAILED]</error>', $this->commandSteps, $command));
158
        } catch (ContextErrorException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Debug\...n\ContextErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
159
            $output->writeln(sprintf('<error>Step %d: "%s" - [FAILED]</error>', $this->commandSteps, $command));
160
        }
161
162
        return $this;
163
    }
164
165
    protected function getKunstmaanLogo()
166
    {
167
        return '
168
         /$$   /$$                                 /$$                                                  /$$$$$$                         
169
        | $$  /$$/                                | $$                                                 /$$__  $$                        
170
        | $$ /$$/  /$$   /$$ /$$$$$$$   /$$$$$$$ /$$$$$$   /$$$$$$/$$$$   /$$$$$$   /$$$$$$  /$$$$$$$ | $$  \__/ /$$$$$$/$$$$   /$$$$$$$
171
        | $$$$$/  | $$  | $$| $$__  $$ /$$_____/|_  $$_/  | $$_  $$_  $$ |____  $$ |____  $$| $$__  $$| $$      | $$_  $$_  $$ /$$_____/
172
        | $$  $$  | $$  | $$| $$  \ $$|  $$$$$$   | $$    | $$ \ $$ \ $$  /$$$$$$$  /$$$$$$$| $$  \ $$| $$      | $$ \ $$ \ $$|  $$$$$$ 
173
        | $$\  $$ | $$  | $$| $$  | $$ \____  $$  | $$ /$$| $$ | $$ | $$ /$$__  $$ /$$__  $$| $$  | $$| $$    $$| $$ | $$ | $$ \____  $$
174
        | $$ \  $$|  $$$$$$/| $$  | $$ /$$$$$$$/  |  $$$$/| $$ | $$ | $$|  $$$$$$$|  $$$$$$$| $$  | $$|  $$$$$$/| $$ | $$ | $$ /$$$$$$$/
175
        |__/  \__/ \______/ |__/  |__/|_______/    \___/  |__/ |__/ |__/ \_______/ \_______/|__/  |__/ \______/ |__/ |__/ |__/|_______/ 
176
        ';
177
    }
178
}
179