Completed
Pull Request — master (#4191)
by Craig
04:57
created

StartCommand::doDBCreds()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 16
rs 9.8333
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\StyleInterface;
20
use Symfony\Component\Console\Style\SymfonyStyle;
21
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
24
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
25
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
26
use Zikula\Bundle\CoreInstallerBundle\Command\AbstractCoreInstallerCommand;
27
use Zikula\Bundle\CoreInstallerBundle\Form\Type\CreateAdminType;
28
use Zikula\Bundle\CoreInstallerBundle\Form\Type\DbCredsType;
29
use Zikula\Bundle\CoreInstallerBundle\Form\Type\LocaleType;
30
use Zikula\Bundle\CoreInstallerBundle\Form\Type\RequestContextType;
31
use Zikula\Bundle\CoreInstallerBundle\Helper\ControllerHelper;
32
use Zikula\Bundle\CoreInstallerBundle\Helper\DbCredsHelper;
33
use Zikula\Bundle\CoreInstallerBundle\Helper\ParameterHelper;
34
use Zikula\MailerModule\Form\Type\MailTransportConfigType;
35
use Zikula\MailerModule\Helper\MailTransportHelper;
36
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
37
38
class StartCommand extends AbstractCoreInstallerCommand
39
{
40
    protected static $defaultName = 'zikula:install:start';
41
42
    /**
43
     * @var bool
44
     */
45
    private $installed;
46
47
    /**
48
     * @var ControllerHelper
49
     */
50
    private $controllerHelper;
51
52
    /**
53
     * @var LocaleApiInterface
54
     */
55
    private $localeApi;
56
57
    /**
58
     * @var ParameterHelper
59
     */
60
    private $parameterHelper;
61
62
    public function __construct(
63
        ZikulaHttpKernelInterface $kernel,
64
        string $installed,
65
        ControllerHelper $controllerHelper,
66
        LocaleApiInterface $localeApi,
67
        ParameterHelper $parameterHelper,
68
        TranslatorInterface $translator
69
    ) {
70
        $this->kernel = $kernel;
71
        $this->installed = '0.0.0' !== $installed;
72
        $this->controllerHelper = $controllerHelper;
73
        $this->localeApi = $localeApi;
74
        $this->parameterHelper = $parameterHelper;
75
        parent::__construct($kernel, $translator);
76
    }
77
78
    protected function configure()
79
    {
80
        $this->setDescription('call this command first');
81
82
        foreach ($this->settings as $name => $setting) {
83
            $this->addOption(
84
                $name,
85
                null,
86
                InputOption::VALUE_REQUIRED,
87
                $setting['description'],
88
                $setting['default']
89
            );
90
        }
91
    }
92
93
    protected function execute(InputInterface $input, OutputInterface $output): int
94
    {
95
        $io = new SymfonyStyle($input, $output);
96
        $io->title($this->translator->trans('Zikula Installer Script'));
97
98
        if (true === $this->installed) {
99
            $io->error($this->translator->trans('Zikula already appears to be installed.'));
100
101
            return 1;
102
        }
103
104
        $warnings = $this->controllerHelper->initPhp();
105
        if (!empty($warnings)) {
106
            $this->printWarnings($output, $warnings);
107
108
            return 2;
109
        }
110
        $checks = $this->controllerHelper->requirementsMet();
111
        if (true !== $checks) {
0 ignored issues
show
introduced by
The condition true !== $checks is always true.
Loading history...
112
            $this->printRequirementsWarnings($output, $checks);
113
114
            return 2;
115
        }
116
117
        if ($input->isInteractive()) {
118
            $io->comment($this->translator->trans('Configuring Zikula installation in %env% environment.', ['%env%' => $this->kernel->getEnvironment()]));
119
            $io->comment($this->translator->trans('Please follow the instructions to install Zikula %version%.', ['%version%' => ZikulaKernel::VERSION]));
120
        }
121
122
        // get the settings from user input
123
        $settings = $this->doLocale($input, $output, $io);
124
        $settings = array_merge($settings, $this->doRequestContext($input, $output, $io));
125
        if (!$this->doDBCreds($input, $output, $io)) {
126
            $io->error($this->translator->trans('Cannot write database DSN to %file% file.', ['%file%' => '/.env.local']));
127
        }
128
        if (!$this->doMailer($input, $output, $io)) {
129
            $io->error($this->translator->trans('Cannot write mailer DSN to %file% file.', ['%file%' => '/.env.local']));
130
        }
131
        $settings = array_merge($settings, $this->doAdmin($input, $output, $io));
132
133
        if ($input->isInteractive()) {
134
            $io->success($this->translator->trans('Configuration successful. Please verify your parameters below:'));
135
            $io->comment($this->translator->trans('(Admin credentials have been encoded to make them json-safe.)'));
136
        }
137
138
        $this->printSettings($settings, $io);
139
        $io->newLine();
140
141
        if ($input->isInteractive()) {
142
            $confirmation = $io->confirm($this->translator->trans('Start installation?'), true);
143
144
            if (!$confirmation) {
145
                $io->error($this->translator->trans('Installation aborted'));
146
147
                return 3;
148
            }
149
        }
150
151
        // write parameters into config/services_custom.yaml and env vars into .env.local
152
        $this->parameterHelper->initializeParameters($settings);
153
154
        $io->success($this->translator->trans('First stage of installation complete. Run `php bin/console zikula:install:finish` to complete the installation.'));
155
156
        return 0;
157
    }
158
159
    private function doLocale(InputInterface $input, OutputInterface $output, StyleInterface $io): array
160
    {
161
        $io->newLine();
162
        $io->section($this->translator->trans('Locale'));
163
164
        return $this->getHelper('form')->interactUsingForm(LocaleType::class, $input, $output, [
165
            'choices' => $this->localeApi->getSupportedLocaleNames(),
166
            'choice_loader' => null
167
        ]);
168
    }
169
170
    private function doRequestContext(InputInterface $input, OutputInterface $output, StyleInterface $io): array
171
    {
172
        $io->newLine();
173
        $io->section($this->translator->trans('Request context'));
174
        $data = $this->getHelper('form')->interactUsingForm(RequestContextType::class, $input, $output);
175
        foreach ($data as $k => $v) {
176
            $newKey = str_replace(':', '.', $k);
177
            $data[$newKey] = $v;
178
            unset($data[$k]);
179
        }
180
181
        return $data;
182
    }
183
184
    private function doDBCreds(InputInterface $input, OutputInterface $output, StyleInterface $io): bool
185
    {
186
        $io->newLine();
187
        $io->section($this->translator->trans('Database information'));
188
        $io->note($this->translator->trans('The database port can be left empty.'));
189
        $data = $this->getHelper('form')->interactUsingForm(DbCredsType::class, $input, $output);
190
        $dbCredsHelper = new DbCredsHelper();
191
        $databaseUrl = $dbCredsHelper->buildDatabaseUrl($data);
192
        try {
193
            $vars = ['DATABASE_URL' => '!\'' . $databaseUrl . '\''];
194
            $helper = new LocalDotEnvHelper($this->kernel->getProjectDir());
195
            $helper->writeLocalEnvVars($vars);
196
197
            return true;
198
        } catch (IOExceptionInterface $exception) {
199
            return false;
200
        }
201
    }
202
203
    private function doMailer(InputInterface $input, OutputInterface $output, StyleInterface $io): bool
204
    {
205
        $io->newLine();
206
        $io->section($this->translator->trans('Mailer transport'));
207
        $io->note($this->translator->trans('Empty values are allowed for all except Mailer transport.'));
208
        $data = $this->getHelper('form')->interactUsingForm(MailTransportConfigType::class, $input, $output);
209
210
        return (new MailTransportHelper($this->kernel->getProjectDir()))->handleFormData($data);
211
    }
212
213
    private function doAdmin(InputInterface $input, OutputInterface $output, StyleInterface $io): array
214
    {
215
        $io->newLine();
216
        $io->section($this->translator->trans('Create admin account'));
217
        $data = $this->getHelper('form')->interactUsingForm(CreateAdminType::class, $input, $output);
218
        foreach ($data as $k => $v) {
219
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
220
        }
221
222
        return $data;
223
    }
224
}
225