Completed
Pull Request — master (#4286)
by Craig
05:35
created

AbstractCoreInstallerCommand::doRequestContext()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 14
rs 9.9666
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;
15
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
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\CoreInstallerBundle\Form\Type\LocaleType;
24
use Zikula\Bundle\CoreInstallerBundle\Form\Type\RequestContextType;
25
use Zikula\MailerModule\Form\Type\MailTransportConfigType;
26
use Zikula\MailerModule\Helper\MailTransportHelper;
27
28
abstract class AbstractCoreInstallerCommand extends Command
29
{
30
    /**
31
     * @var TranslatorInterface
32
     */
33
    protected $translator;
34
35
    /**
36
     * @var ZikulaHttpKernelInterface
37
     */
38
    protected $kernel;
39
40
    /**
41
     * @var array
42
     * @see \Zikula\Bundle\CoreInstallerBundle\Command\Install\StartCommand
43
     */
44
    protected $settings = [
45
        /* Database */
46
        'database_host' => [
47
            'description' => 'The location of your database, most of the times "localhost".',
48
            'default' => 'localhost'
49
        ],
50
        'database_port' => [
51
            'description' => 'Optional custom database port number.',
52
            'default' => null
53
        ],
54
        'database_user' => [
55
            'description' => 'The database user.',
56
            'default' => null
57
        ],
58
        'database_password' => [
59
            'description' => 'Your database user\'s password.',
60
            'default' => null,
61
        ],
62
        'database_name' => [
63
            'description' => 'The name of the database.',
64
            'default' => null
65
        ],
66
        'database_driver' => [
67
            'description' => 'Your database driver.',
68
            'default' => 'mysql'
69
        ],
70
        /* Admin user */
71
        'username' => [
72
            'description' => 'Username of the new Zikula admin user.',
73
            'default' => 'admin'
74
        ],
75
        'password' => [
76
            'description' => 'Password of the new Zikula admin user.',
77
            'default' => null,
78
        ],
79
        'email' => [
80
            'description' => 'Email of the new Zikula admin user.',
81
            'default' => null
82
        ],
83
        /* Http settings */
84
        'router:request_context:host' => [
85
            'description' => 'The root domain where you install Zikula, e.g. "example.com". Do not include subdirectories.',
86
            'default' => null,
87
        ],
88
        'router:request_context:scheme' => [
89
            'description' => 'The scheme of where you install Zikula, can be either "http" or "https".',
90
            'default' => 'http',
91
        ],
92
        'router:request_context:base_url' => [
93
            'description' => 'The url path of the directory where you install Zikula, leave empty if you install it at the top level. Example: /my/sub-dir',
94
            'default' => '',
95
        ],
96
        'locale' => [
97
            'description' => 'The locale to use.',
98
            'default' => 'en'
99
        ],
100
        /* mailer settings */
101
        'transport' => [
102
            'description' => 'The mailer transport to use.',
103
            'default' => 'test'
104
        ],
105
        'mailer_id' => [
106
            'description' => 'The ACCESS_KEY, USERNAME, ID or apikey for the selected transport.',
107
            'default' => null
108
        ],
109
        'mailer_key' => [
110
            'description' => 'The SECRET_KEY, PASSWORD, ID or KEY for the selected transport.',
111
            'default' => null
112
        ],
113
        'host' => [
114
            'description' => 'SMTP host server',
115
            'default' => null
116
        ],
117
        'port' => [
118
            'description' => 'SMTP port',
119
            'default' => null
120
        ],
121
        'customParameters' => [
122
            'description' => 'Use query parameters syntax, for example: <code>?param1=value1&amp;param2=value2</code>.',
123
            'default' => null
124
        ],
125
        'enableLogging' => [
126
            'description' => 'Enable logging of sent mail.',
127
            'default' => false
128
        ],
129
    ];
130
131
    public function __construct(
132
        ZikulaHttpKernelInterface $kernel,
133
        TranslatorInterface $translator
134
    ) {
135
        parent::__construct();
136
        $this->kernel = $kernel;
137
        $this->translator = $translator;
138
    }
139
140
    protected function printWarnings(OutputInterface $output, $warnings): void
141
    {
142
        foreach ($warnings as $warning) {
143
            $output->writeln('<error>' . $warning . '</error>');
144
        }
145
    }
146
147
    protected function doRequestContext(InputInterface $input, OutputInterface $output, StyleInterface $io): array
148
    {
149
        if ($input->isInteractive()) {
150
            $io->newLine();
151
            $io->section($this->translator->trans('Request context'));
152
        }
153
        $data = $this->getHelper('form')->interactUsingForm(RequestContextType::class, $input, $output);
154
        foreach ($data as $k => $v) {
155
            $newKey = str_replace(':', '.', $k);
156
            $data[$newKey] = $v;
157
            unset($data[$k]);
158
        }
159
160
        return $data;
161
    }
162
163
    protected function doLocale(InputInterface $input, OutputInterface $output, StyleInterface $io): array
164
    {
165
        if ($input->isInteractive()) {
166
            $io->newLine();
167
            $io->section($this->translator->trans('Locale'));
168
        }
169
170
        return $this->getHelper('form')->interactUsingForm(LocaleType::class, $input, $output, [
171
            'choices' => $this->localeApi->getSupportedLocaleNames(),
0 ignored issues
show
Bug Best Practice introduced by
The property localeApi does not exist on Zikula\Bundle\CoreInstal...actCoreInstallerCommand. Did you maybe forget to declare it?
Loading history...
172
            'choice_loader' => null
173
        ]);
174
    }
175
176
    protected function doMailer(InputInterface $input, OutputInterface $output, StyleInterface $io) // bool|array
177
    {
178
        if ($input->isInteractive()) {
179
            $io->newLine();
180
            $io->section($this->translator->trans('Mailer transport'));
181
            $io->note($this->translator->trans('Empty values are allowed for all except Mailer transport.'));
182
        }
183
        $data = $this->getHelper('form')->interactUsingForm(MailTransportConfigType::class, $input, $output);
184
        $mailDsnWrite = (new MailTransportHelper($this->kernel->getProjectDir()))->handleFormData($data);
185
        if ($mailDsnWrite) {
186
            return $this->encodeArrayValues($data);
187
        }
188
189
        return false;
190
    }
191
192
    protected function encodeArrayValues(array $data): array
193
    {
194
        foreach ($data as $k => $v) {
195
            $data[$k] = is_string($v) ? base64_encode($v) : $v; // encode so values are 'safe' for json
196
        }
197
198
        return $data;
199
    }
200
201
    protected function printSettings($givenSettings, SymfonyStyle $io): void
202
    {
203
        $rows = [];
204
        foreach ($givenSettings as $name => $givenSetting) {
205
            if (isset($this->settings[$name]['password']) && $this->settings[$name]['password']) {
206
                $givenSetting = str_repeat('*', mb_strlen($givenSetting));
207
            }
208
            $rows[] = [$name, $givenSetting];
209
        }
210
        $io->table([$this->translator->trans('Parameter'), $this->translator->trans('Value')], $rows);
211
    }
212
}
213