Completed
Pull Request — master (#1836)
by Ruud
42:41 queued 30:18
created

InstallCommand::interact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 25
cp 0
rs 9.408
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Output\NullOutput;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputDefinition;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Sensio\Bundle\GeneratorBundle\Command\Validators;
15
use Symfony\Component\Console\Question\ChoiceQuestion;
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
class InstallCommand extends Command
24
{
25
    /**
26
     * @var int
27
     */
28
    private $commandSteps = 0;
29
30
    /** @var string */
31
    private $rootDir;
32
33
    /**
34
     * @param string $rootDir
35
     */
36
    public function __construct(string $rootDir)
37
    {
38
        $this->rootDir = $rootDir;
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('namespace', '', InputOption::VALUE_REQUIRED, 'The namespace of the bundle to create'),
52
                        new InputOption('demosite', '', InputOption::VALUE_REQUIRED, 'Do you want create "demosite"'),
53
                        new InputOption('dir', '', InputOption::VALUE_REQUIRED, 'The directory where to create the bundle'),
54
                        new InputOption('bundle-name', '', InputOption::VALUE_REQUIRED, 'The optional bundle name'),
55
                    ]
56
                )
57
            );
58
    }
59
60
    protected function interact(InputInterface $input, OutputInterface $output)
61
    {
62
        $questionHelper = new QuestionHelper();
63
64
        $outputStyle = new SymfonyStyle($input, $output);
65
        $outputStyle->writeln('<info>Installing KunstmaanCms...</info>');
66
        $outputStyle->writeln($this->getKunstmaanLogo());
67
68
        $question = new Question(
69
            $questionHelper->getQuestion('Bundle namespace', $input->getOption('namespace')),
70
            $input->getOption('namespace')
71
        );
72
        $question->setValidator([Validators::class, 'validateBundleNamespace']);
73
        $namespace = $questionHelper->ask($input, $output, $question);
74
        $input->setOption('namespace', $namespace);
75
76
        $question = new ChoiceQuestion(
77
            'Do you want create "demosite"',
78
            ['No', 'Yes'],
79
            0
80
        );
81
        $question->setErrorMessage('Option "%s" is invalid.');
82
        $demositeOption = $questionHelper->ask($input, $output, $question);
83
84
        $input->setOption('demosite', $demositeOption);
85
        $input->setOption('bundle-name', strtr($namespace, ['\\Bundle\\' => '', '\\' => '']));
86
87
        $dir = $input->getOption('dir') ?: dirname($this->rootDir) . '/src';
88
        $input->setOption('dir', $dir);
89
90
        $output->writeln('<info>Installation start</info>');
91
    }
92
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        $demositeOptions = ['--namespace' => $input->getOption('namespace')];
96
        if ($input->getOption('demosite') === 'Yes') {
97
            $demositeOptions['--demosite'] = true;
98
        }
99
100
        $this
101
            ->executeCommand($output, 'kuma:generate:bundle', [
102
                '--namespace' => $input->getOption('namespace'),
103
                '--dir' => $input->getOption('dir'),
104
                '--bundle-name' => $input->getOption('bundle-name'),
105
            ])
106
            ->executeCommand($output, 'kuma:generate:default-site', $demositeOptions)
107
            ->executeCommand($output, 'doctrine:database:create')
108
            ->executeCommand($output, 'doctrine:schema:drop', ['--force' => true])
109
            ->executeCommand($output, 'doctrine:schema:create')
110
            ->executeCommand($output, 'doctrine:fixtures:load')
111
            ->executeCommand($output, 'kuma:generate:admin-tests', [
112
                '--namespace' => $input->getOption('namespace'),
113
            ])
114
        ;
115
    }
116
117
    protected function executeCommand(OutputInterface $output, $command, array $options = [])
118
    {
119
        $options = array_merge(
120
            [
121
                '--no-debug' => true,
122
                '--no-interaction' => true,
123
            ],
124
            $options
125
        );
126
127
        ++$this->commandSteps;
128
129
        try {
130
            $updateInput = new ArrayInput($options);
131
            $updateInput->setInteractive(false);
132
            $this->getApplication()->find($command)->run($updateInput, new NullOutput());
133
            $output->writeln(sprintf('<info>Step %d: "%s" - [OK]</info>', $this->commandSteps, $command));
134
        } catch (RuntimeException $exception) {
135
            $output->writeln(sprintf('<error>Step %d: "%s" - [FAILED]</error>', $this->commandSteps, $command));
136
        } 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...
137
            $output->writeln(sprintf('<error>Step %d: "%s" - [FAILED]</error>', $this->commandSteps, $command));
138
        }
139
140
        return $this;
141
    }
142
143
    protected function getKunstmaanLogo()
144
    {
145
        return '
146
         /$$   /$$                                 /$$                                                  /$$$$$$                         
147
        | $$  /$$/                                | $$                                                 /$$__  $$                        
148
        | $$ /$$/  /$$   /$$ /$$$$$$$   /$$$$$$$ /$$$$$$   /$$$$$$/$$$$   /$$$$$$   /$$$$$$  /$$$$$$$ | $$  \__/ /$$$$$$/$$$$   /$$$$$$$
149
        | $$$$$/  | $$  | $$| $$__  $$ /$$_____/|_  $$_/  | $$_  $$_  $$ |____  $$ |____  $$| $$__  $$| $$      | $$_  $$_  $$ /$$_____/
150
        | $$  $$  | $$  | $$| $$  \ $$|  $$$$$$   | $$    | $$ \ $$ \ $$  /$$$$$$$  /$$$$$$$| $$  \ $$| $$      | $$ \ $$ \ $$|  $$$$$$ 
151
        | $$\  $$ | $$  | $$| $$  | $$ \____  $$  | $$ /$$| $$ | $$ | $$ /$$__  $$ /$$__  $$| $$  | $$| $$    $$| $$ | $$ | $$ \____  $$
152
        | $$ \  $$|  $$$$$$/| $$  | $$ /$$$$$$$/  |  $$$$/| $$ | $$ | $$|  $$$$$$$|  $$$$$$$| $$  | $$|  $$$$$$/| $$ | $$ | $$ /$$$$$$$/
153
        |__/  \__/ \______/ |__/  |__/|_______/    \___/  |__/ |__/ |__/ \_______/ \_______/|__/  |__/ \______/ |__/ |__/ |__/|_______/ 
154
        ';
155
    }
156
}
157