Completed
Pull Request — master (#278)
by Alejandro
03:36 queued 01:25
created

LanguageConfigCustomizer::process()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 2
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
ccs 11
cts 11
cp 1
crap 4
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