Completed
Pull Request — master (#4286)
by Craig
04:49
created

StartCommand::doAdminCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - 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\Contracts\Translation\TranslatorInterface;
22
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
23
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
24
use Zikula\Bundle\CoreInstallerBundle\Command\AbstractCoreInstallerCommand;
25
use Zikula\Bundle\CoreInstallerBundle\Form\Type\CreateAdminType;
26
use Zikula\Bundle\CoreInstallerBundle\Form\Type\DbCredsType;
27
use Zikula\Bundle\CoreInstallerBundle\Helper\DbCredsHelper;
28
use Zikula\Bundle\CoreInstallerBundle\Helper\ParameterHelper;
29
use Zikula\Bundle\CoreInstallerBundle\Helper\PhpHelper;
30
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
31
32
class StartCommand extends AbstractCoreInstallerCommand
33
{
34
    protected static $defaultName = 'zikula:install:start';
35
36
    /**
37
     * @var bool
38
     */
39
    private $installed;
40
41
    /**
42
     * @var PhpHelper
43
     */
44
    private $phpHelper;
45
46
    /**
47
     * @var LocaleApiInterface
48
     */
49
    private $localeApi;
50
51
    /**
52
     * @var ParameterHelper
53
     */
54
    private $parameterHelper;
55
56
    public function __construct(
57
        ZikulaHttpKernelInterface $kernel,
58
        string $installed,
59
        PhpHelper $phpHelper,
60
        LocaleApiInterface $localeApi,
61
        ParameterHelper $parameterHelper,
62
        TranslatorInterface $translator
63
    ) {
64
        $this->kernel = $kernel;
65
        $this->installed = '0.0.0' !== $installed;
66
        $this->phpHelper = $phpHelper;
67
        $this->localeApi = $localeApi;
68
        $this->parameterHelper = $parameterHelper;
69
        parent::__construct($kernel, $translator);
70
    }
71
72
    protected function configure()
73
    {
74
        $this->setDescription('call this command first');
75
76
        foreach ($this->settings as $name => $setting) {
77
            $this->addOption(
78
                $name,
79
                null,
80
                InputOption::VALUE_REQUIRED,
81
                $setting['description'],
82
                $setting['default']
83
            );
84
        }
85
    }
86
87
    protected function execute(InputInterface $input, OutputInterface $output): int
88
    {
89
        $io = new SymfonyStyle($input, $output);
90
        $io->title($this->translator->trans('Zikula Installer Script'));
91
92
        if (true === $this->installed) {
93
            $io->error($this->translator->trans('Zikula already appears to be installed.'));
94
95
            return 1;
96
        }
97
98
        $iniWarnings = $this->phpHelper->setUp();
99
        if (!empty($iniWarnings)) {
100
            $this->printWarnings($output, $iniWarnings);
101
102
            return 2;
103
        }
104
105
        if ($input->isInteractive()) {
106
            $io->comment($this->translator->trans('Configuring Zikula installation in %env% environment.', ['%env%' => $this->kernel->getEnvironment()]));
107
            $io->comment($this->translator->trans('Please follow the instructions to install Zikula %version%.', ['%version%' => ZikulaKernel::VERSION]));
108
        }
109
110
        // get the settings from user input
111
        $settings = $this->doLocale($input, $output, $io);
112
        $settings = array_merge($settings, $this->doRequestContext($input, $output, $io));
113
        if (!$this->doDBCreds($input, $output, $io)) {
114
            $io->error($this->translator->trans('Cannot write database DSN to %file% file.', ['%file%' => '/.env.local']));
115
        }
116
        if (false === $mailSettings = $this->doMailer($input, $output, $io)) {
117
            $io->error($this->translator->trans('Cannot write mailer DSN to %file% file.', ['%file%' => '/.env.local']));
118
        } else {
119
            $settings = array_merge($settings, $mailSettings);
120
        }
121
        $settings = array_merge($settings, $this->doAdminCreate($input, $output, $io));
122
123
        if ($input->isInteractive()) {
124
            $io->success($this->translator->trans('Configuration successful. Please verify your parameters below:'));
125
            $io->comment($this->translator->trans('(Admin credentials have been encoded to make them json-safe.)'));
126
        }
127
128
        if ($input->isInteractive()) {
129
            $this->printSettings($settings, $io);
130
            $io->newLine();
131
            $confirmation = $io->confirm($this->translator->trans('Start installation?'), true);
132
133
            if (!$confirmation) {
134
                $io->error($this->translator->trans('Installation aborted'));
135
136
                return 3;
137
            }
138
        }
139
140
        // write parameters into config/services_custom.yaml and env vars into .env.local
141
        $this->parameterHelper->initializeParameters($settings);
142
143
        $io->success($this->translator->trans('First stage of installation complete. Run `php bin/console zikula:install:finish` to complete the installation.'));
144
145
        return 0;
146
    }
147
148
    private function doDBCreds(InputInterface $input, OutputInterface $output, StyleInterface $io): bool
149
    {
150
        if ($input->isInteractive()) {
151
            $io->newLine();
152
            $io->section($this->translator->trans('Database information'));
153
            $io->note($this->translator->trans('The database port can be left empty.'));
154
        }
155
        $data = $this->getHelper('form')->interactUsingForm(DbCredsType::class, $input, $output);
156
157
        return (new DbCredsHelper($this->kernel->getProjectDir()))->writeDatabaseDsn($data);
158
    }
159
160
    private function doAdminCreate(InputInterface $input, OutputInterface $output, StyleInterface $io): array
161
    {
162
        if ($input->isInteractive()) {
163
            $io->newLine();
164
            $io->section($this->translator->trans('Create admin account'));
165
        }
166
        $data = $this->getHelper('form')->interactUsingForm(CreateAdminType::class, $input, $output);
167
168
        return $this->encodeArrayValues($data);
169
    }
170
}
171