Completed
Push — master ( 917d9c...ff7bd8 )
by Craig
04:48
created

StartCommand::execute()   C

Complexity

Conditions 11
Paths 147

Size

Total Lines 83
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 51
nc 147
nop 2
dl 0
loc 83
rs 6.609
c 0
b 0
f 0

How to fix   Long Method    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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreInstallerBundle\Command\Install;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
23
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
24
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
25
use Zikula\Bundle\CoreInstallerBundle\Command\AbstractCoreInstallerCommand;
26
use Zikula\Bundle\CoreInstallerBundle\Form\Type\CreateAdminType;
27
use Zikula\Bundle\CoreInstallerBundle\Form\Type\DbCredsType;
28
use Zikula\Bundle\CoreInstallerBundle\Form\Type\LocaleType;
29
use Zikula\Bundle\CoreInstallerBundle\Form\Type\RequestContextType;
30
use Zikula\Bundle\CoreInstallerBundle\Helper\ControllerHelper;
31
use Zikula\Bundle\CoreInstallerBundle\Helper\DbCredsHelper;
32
use Zikula\Bundle\CoreInstallerBundle\Helper\ParameterHelper;
33
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
34
35
class StartCommand extends AbstractCoreInstallerCommand
36
{
37
    protected static $defaultName = 'zikula:install:start';
38
39
    /**
40
     * @var bool
41
     */
42
    private $installed;
43
44
    /**
45
     * @var ControllerHelper
46
     */
47
    private $controllerHelper;
48
49
    /**
50
     * @var LocaleApiInterface
51
     */
52
    private $localeApi;
53
54
    /**
55
     * @var ParameterHelper
56
     */
57
    private $parameterHelper;
58
59
    public function __construct(
60
        ZikulaHttpKernelInterface $kernel,
61
        string $installed,
62
        ControllerHelper $controllerHelper,
63
        LocaleApiInterface $localeApi,
64
        ParameterHelper $parameterHelper,
65
        TranslatorInterface $translator
66
    ) {
67
        $this->kernel = $kernel;
68
        $this->installed = '0.0.0' !== $installed;
69
        $this->controllerHelper = $controllerHelper;
70
        $this->localeApi = $localeApi;
71
        $this->parameterHelper = $parameterHelper;
72
        parent::__construct($kernel, $translator);
73
    }
74
75
    protected function configure()
76
    {
77
        $this->setDescription('call this command first');
78
79
        foreach ($this->settings as $name => $setting) {
80
            $this->addOption(
81
                $name,
82
                null,
83
                InputOption::VALUE_REQUIRED,
84
                $setting['description'],
85
                $setting['default']
86
            );
87
        }
88
    }
89
90
    protected function execute(InputInterface $input, OutputInterface $output): int
91
    {
92
        $io = new SymfonyStyle($input, $output);
93
        $io->title($this->translator->trans('Zikula Installer Script'));
94
95
        if (true === $this->installed) {
96
            $io->error($this->translator->trans('Zikula already appears to be installed.'));
97
98
            return 1;
99
        }
100
101
        $warnings = $this->controllerHelper->initPhp();
102
        if (!empty($warnings)) {
103
            $this->printWarnings($output, $warnings);
104
105
            return 2;
106
        }
107
        $checks = $this->controllerHelper->requirementsMet();
108
        if (true !== $checks) {
0 ignored issues
show
introduced by
The condition true !== $checks is always true.
Loading history...
109
            $this->printRequirementsWarnings($output, $checks);
110
111
            return 2;
112
        }
113
114
        if ($input->isInteractive()) {
115
            $io->comment($this->translator->trans('Configuring Zikula installation in %env% environment.', ['%env%' => $this->kernel->getEnvironment()]));
116
            $io->comment($this->translator->trans('Please follow the instructions to install Zikula %version%.', ['%version%' => ZikulaKernel::VERSION]));
117
        }
118
119
        // get the settings from user input
120
        $settings = $this->getHelper('form')->interactUsingForm(LocaleType::class, $input, $output, [
121
            'choices' => $this->localeApi->getSupportedLocaleNames(),
122
            'choice_loader' => null
123
        ]);
124
        $data = $this->getHelper('form')->interactUsingForm(RequestContextType::class, $input, $output);
125
        foreach ($data as $k => $v) {
126
            $newKey = str_replace(':', '.', $k);
127
            $data[$newKey] = $v;
128
            unset($data[$k]);
129
        }
130
        $settings = array_merge($settings, $data);
131
        $data = $this->getHelper('form')->interactUsingForm(DbCredsType::class, $input, $output);
132
133
        $dbCredsHelper = new DbCredsHelper();
134
        $databaseUrl = $dbCredsHelper->buildDatabaseUrl($data);
135
        try {
136
            $vars = ['DATABASE_URL' => '!\'' . $databaseUrl . '\''];
137
            $helper = new LocalDotEnvHelper($this->kernel->getProjectDir());
138
            $helper->writeLocalEnvVars($vars);
139
        } catch (IOExceptionInterface $exception) {
140
            $io->error(sprintf('Cannot write to %s file.', $this->kernel->getProjectDir() . '/.env.local') . ' ' . $exception->getMessage());
141
        }
142
143
        $data = $this->getHelper('form')->interactUsingForm(CreateAdminType::class, $input, $output);
144
        foreach ($data as $k => $v) {
0 ignored issues
show
Comprehensibility Bug introduced by
$v is overwriting a variable from outer foreach loop.
Loading history...
Comprehensibility Bug introduced by
$k is overwriting a variable from outer foreach loop.
Loading history...
145
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
146
        }
147
        $settings = array_merge($settings, $data);
148
149
        if ($input->isInteractive()) {
150
            $io->success($this->translator->trans('Configuration successful. Please verify your parameters below:'));
151
            $io->comment($this->translator->trans('(Admin credentials have been encoded to make them json-safe.)'));
152
        }
153
154
        $this->printSettings($settings, $io);
155
        $io->newLine();
156
157
        if ($input->isInteractive()) {
158
            $confirmation = $io->confirm($this->translator->trans('Start installation?'), true);
159
160
            if (!$confirmation) {
161
                $io->error($this->translator->trans('Installation aborted'));
162
163
                return 3;
164
            }
165
        }
166
167
        // write parameters into config/services_custom.yaml and env vars into .env.local
168
        $this->parameterHelper->initializeParameters($settings);
169
170
        $io->success($this->translator->trans('First stage of installation complete. Run `php bin/console zikula:install:finish` to complete the installation.'));
171
172
        return 0;
173
    }
174
}
175