Completed
Push — master ( aa77c9...15a70d )
by Alejandro
27s
created

LanguageConfigCustomizer::ask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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
    public const DEFAULT_LANG = 'DEFAULT';
14
    private const EXPECTED_KEYS = [
15
        self::DEFAULT_LANG,
16
    ];
17
18
    private const SUPPORTED_LANGUAGES = ['en', 'es'];
19
20 3
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
21
    {
22 3
        $lang = $appConfig->getLanguage();
23 3
        $keysToAskFor = $appConfig->hasLanguage()
24 1
            ? array_diff(self::EXPECTED_KEYS, array_keys($lang))
25 3
            : self::EXPECTED_KEYS;
26
27 3
        if (empty($keysToAskFor)) {
28 1
            return;
29
        }
30
31 2
        $io->title('LANGUAGE');
32 2
        foreach ($keysToAskFor as $key) {
33 2
            $lang[$key] = $this->ask($io, $key);
34
        }
35 2
        $appConfig->setLanguage($lang);
36
    }
37
38 2
    private function ask(SymfonyStyle $io, string $key)
39
    {
40
        switch ($key) {
41 2
            case self::DEFAULT_LANG:
42 2
                return $this->chooseLanguage($io, 'Select default language for the application error pages');
43
        }
44
45
        return '';
46
    }
47
48 2
    private function chooseLanguage(SymfonyStyle $io, string $message): string
49
    {
50 2
        return $io->choice($message, self::SUPPORTED_LANGUAGES, self::SUPPORTED_LANGUAGES[0]);
51
    }
52
}
53