Completed
Pull Request — master (#4286)
by Craig
04:48
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 ParameterHelper
48
     */
49
    private $parameterHelper;
50
51
    public function __construct(
52
        ZikulaHttpKernelInterface $kernel,
53
        string $installed,
54
        PhpHelper $phpHelper,
55
        LocaleApiInterface $localeApi,
56
        ParameterHelper $parameterHelper,
57
        TranslatorInterface $translator
58
    ) {
59
        $this->kernel = $kernel;
60
        $this->installed = '0.0.0' !== $installed;
61
        $this->phpHelper = $phpHelper;
62
        $this->parameterHelper = $parameterHelper;
63
        parent::__construct($kernel, $translator, $localeApi);
64
    }
65
66
    protected function configure()
67
    {
68
        $this->setDescription('call this command first');
69
70
        foreach ($this->settings as $name => $setting) {
71
            $this->addOption(
72
                $name,
73
                null,
74
                InputOption::VALUE_REQUIRED,
75
                $setting['description'],
76
                $setting['default']
77
            );
78
        }
79
    }
80
81
    protected function execute(InputInterface $input, OutputInterface $output): int
82
    {
83
        $io = new SymfonyStyle($input, $output);
84
        $io->title($this->translator->trans('Zikula Installer Script'));
85
86
        if (true === $this->installed) {
87
            $io->error($this->translator->trans('Zikula already appears to be installed.'));
88
89
            return 1;
90
        }
91
92
        $iniWarnings = $this->phpHelper->setUp();
93
        if (!empty($iniWarnings)) {
94
            $this->printWarnings($output, $iniWarnings);
95
96
            return 2;
97
        }
98
99
        if ($input->isInteractive()) {
100
            $io->comment($this->translator->trans('Configuring Zikula installation in %env% environment.', ['%env%' => $this->kernel->getEnvironment()]));
101
            $io->comment($this->translator->trans('Please follow the instructions to install Zikula %version%.', ['%version%' => ZikulaKernel::VERSION]));
102
        }
103
104
        // get the settings from user input
105
        $settings = $this->doLocale($input, $output, $io);
106
        $settings = array_merge($settings, $this->doRequestContext($input, $output, $io));
107
        if (!$this->doDBCreds($input, $output, $io)) {
108
            $io->error($this->translator->trans('Cannot write database DSN to %file% file.', ['%file%' => '/.env.local']));
109
        }
110
        if (false === $mailSettings = $this->doMailer($input, $output, $io)) {
111
            $io->error($this->translator->trans('Cannot write mailer DSN to %file% file.', ['%file%' => '/.env.local']));
112
        } else {
113
            $settings = array_merge($settings, $mailSettings);
114
        }
115
        $settings = array_merge($settings, $this->doAdminCreate($input, $output, $io));
116
117
        if ($input->isInteractive()) {
118
            $io->success($this->translator->trans('Configuration successful. Please verify your parameters below:'));
119
            $io->comment($this->translator->trans('(Admin credentials have been encoded to make them json-safe.)'));
120
        }
121
122
        if ($input->isInteractive()) {
123
            $this->printSettings($settings, $io);
124
            $io->newLine();
125
            $confirmation = $io->confirm($this->translator->trans('Start installation?'), true);
126
127
            if (!$confirmation) {
128
                $io->error($this->translator->trans('Installation aborted'));
129
130
                return 3;
131
            }
132
        }
133
134
        // write parameters into config/services_custom.yaml and env vars into .env.local
135
        $this->parameterHelper->initializeParameters($settings);
136
137
        $io->success($this->translator->trans('First stage of installation complete. Run `php bin/console zikula:install:finish` to complete the installation.'));
138
139
        return 0;
140
    }
141
142
    private function doDBCreds(InputInterface $input, OutputInterface $output, StyleInterface $io): bool
143
    {
144
        if ($input->isInteractive()) {
145
            $io->newLine();
146
            $io->section($this->translator->trans('Database information'));
147
            $io->note($this->translator->trans('The database port can be left empty.'));
148
        }
149
        $data = $this->getHelper('form')->interactUsingForm(DbCredsType::class, $input, $output);
150
151
        return (new DbCredsHelper($this->kernel->getProjectDir()))->writeDatabaseDsn($data);
152
    }
153
154
    private function doAdminCreate(InputInterface $input, OutputInterface $output, StyleInterface $io): array
155
    {
156
        if ($input->isInteractive()) {
157
            $io->newLine();
158
            $io->section($this->translator->trans('Create admin account'));
159
        }
160
        $data = $this->getHelper('form')->interactUsingForm(CreateAdminType::class, $input, $output);
161
162
        return $this->encodeArrayValues($data);
163
    }
164
}
165