Completed
Pull Request — master (#4286)
by Craig
20:17
created

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