Completed
Pull Request — master (#224)
by Alejandro
04:50
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 SUPPORTED_LANGUAGES = ['en', 'es'];
14
    private const DEFAULT_LANG = 'DEFAULT';
15
    private const CLI_LANG = 'CLI';
16
    private const EXPECTED_KEYS = [
17
        self::DEFAULT_LANG,
18
        self::CLI_LANG,
19
    ];
20
21
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
22
    {
23
        $io->title('LANGUAGE');
24
25
        $lang = $appConfig->getLanguage();
26
        $keysToAskFor = $appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')
27
            ? array_diff(self::EXPECTED_KEYS, array_keys($lang))
28
            : self::EXPECTED_KEYS;
29
30
        if (empty($keysToAskFor)) {
31
            return;
32
        }
33
34
        foreach ($keysToAskFor as $key) {
35
            $lang[$key] = $this->ask($io, $key);
36
        }
37
        $appConfig->setLanguage($lang);
38
    }
39
40
    private function ask(SymfonyStyle $io, string $key)
41
    {
42
        switch ($key) {
43
            case self::DEFAULT_LANG:
44
                return $this->chooseLanguage($io, 'Select default language for the application in general');
45
            case self::CLI_LANG:
46
                return $this->chooseLanguage($io, 'Select default language for CLI executions');
47
        }
48
49
        return '';
50
    }
51
52
    private function chooseLanguage(SymfonyStyle $io, string $message): string
53
    {
54
        return $io->choice($message, self::SUPPORTED_LANGUAGES, self::SUPPORTED_LANGUAGES[0]);
55
    }
56
}
57