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

LanguageConfigCustomizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 40
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 4
A ask() 0 8 2
A chooseLanguage() 0 3 1
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