Completed
Pull Request — master (#224)
by Alejandro
04:50
created

ApplicationConfigCustomizer::ask()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 17
rs 9.7
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\Common\Util\StringUtilsTrait;
7
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
8
use Symfony\Component\Console\Style\SymfonyStyle;
9
use function array_diff;
10
use function array_keys;
11
12
class ApplicationConfigCustomizer implements ConfigCustomizerInterface
13
{
14
    use StringUtilsTrait;
15
16
    private const SECRET = 'SECRET';
17
    private const DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM';
18
    private const EXPECTED_KEYS = [
19
        self::SECRET,
20
        self::DISABLE_TRACK_PARAM,
21
    ];
22
23
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
24
    {
25
        $io->title('APPLICATION');
26
27
        $app = $appConfig->getApp();
28
        $keysToAskFor = $appConfig->hasApp() && $io->confirm('Do you want to keep imported application config?')
29
            ? array_diff(self::EXPECTED_KEYS, array_keys($app))
30
            : self::EXPECTED_KEYS;
31
32
        if (empty($keysToAskFor)) {
33
            return;
34
        }
35
36
        foreach ($keysToAskFor as $key) {
37
            $app[$key] = $this->ask($io, $key);
38
        }
39
        $appConfig->setApp($app);
40
    }
41
42
    private function ask(SymfonyStyle $io, string $key)
43
    {
44
        switch ($key) {
45
            case self::SECRET:
46
                return $io->ask(
47
                    'Define a secret string that will be used to sign API tokens (leave empty to autogenerate one) '
48
                    . '<fg=red>[DEPRECATED. TO BE REMOVED]</>'
49
                ) ?: $this->generateRandomString(32);
50
            case self::DISABLE_TRACK_PARAM:
51
                return $io->ask(
52
                    'Provide a parameter name that you will be able to use to disable tracking on specific request to '
53
                    . 'short URLs (leave empty and this feature won\'t be enabled)'
54
                );
55
        }
56
57
        return '';
58
    }
59
}
60