Completed
Pull Request — master (#224)
by Alejandro
03:34
created

LanguageConfigCustomizer::ask()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Config\Plugin;
5
6
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
7
use Symfony\Component\Console\Style\SymfonyStyle;
8
use function array_diff;
9
use function array_keys;
10
11
class LanguageConfigCustomizer implements ConfigCustomizerInterface
12
{
13
    private const DEFAULT_LANG = 'DEFAULT';
14
    private const CLI_LANG = 'CLI';
15
    private const EXPECTED_KEYS = [
16
        self::DEFAULT_LANG,
17
        self::CLI_LANG,
18
    ];
19
20
    private const SUPPORTED_LANGUAGES = ['en', 'es'];
21
22
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
23
    {
24
        $io->title('LANGUAGE');
25
26
        $lang = $appConfig->getLanguage();
27
        $keysToAskFor = $appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')
28
            ? array_diff(self::EXPECTED_KEYS, array_keys($lang))
29
            : self::EXPECTED_KEYS;
30
31
        if (empty($keysToAskFor)) {
32
            return;
33
        }
34
35
        foreach ($keysToAskFor as $key) {
36
            $lang[$key] = $this->ask($io, $key);
37
        }
38
        $appConfig->setLanguage($lang);
39
    }
40
41
    private function ask(SymfonyStyle $io, string $key)
42
    {
43
        switch ($key) {
44
            case self::DEFAULT_LANG:
45
                return $this->chooseLanguage($io, 'Select default language for the application in general');
46
            case self::CLI_LANG:
47
                return $this->chooseLanguage($io, 'Select default language for CLI executions');
48
        }
49
50
        return '';
51
    }
52
53
    private function chooseLanguage(SymfonyStyle $io, string $message): string
54
    {
55
        return $io->choice($message, self::SUPPORTED_LANGUAGES, self::SUPPORTED_LANGUAGES[0]);
56
    }
57
}
58