Completed
Push — master ( 3a75ac...d68dc3 )
by Alejandro
23s
created

ApplicationConfigCustomizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 18
cts 19
cp 0.9474
wmc 8
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 15 4
A ask() 0 17 4
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
    public const SECRET = 'SECRET';
17
    public const DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM';
18
    private const EXPECTED_KEYS = [
19
        self::SECRET,
20
        self::DISABLE_TRACK_PARAM,
21
    ];
22
23 3
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
24
    {
25 3
        $app = $appConfig->getApp();
26 3
        $keysToAskFor = $appConfig->hasApp() ? array_diff(self::EXPECTED_KEYS, array_keys($app)) : self::EXPECTED_KEYS;
27
28 3
        if (empty($keysToAskFor)) {
29 1
            return;
30
        }
31
32 2
        $io->title('APPLICATION');
33 2
        foreach ($keysToAskFor as $key) {
34 2
            $app[$key] = $this->ask($io, $key);
35
        }
36 2
        $appConfig->setApp($app);
37 2
    }
38
39 2
    private function ask(SymfonyStyle $io, string $key)
40
    {
41
        switch ($key) {
42 2
            case self::SECRET:
43 1
                return $io->ask(
44
                    'Define a secret string that will be used to sign API tokens (leave empty to autogenerate one) '
45 1
                    . '<fg=red>[DEPRECATED. TO BE REMOVED]</>'
46 1
                ) ?: $this->generateRandomString(32);
47 2
            case self::DISABLE_TRACK_PARAM:
48 2
                return $io->ask(
49
                    'Provide a parameter name that you will be able to use to disable tracking on specific request to '
50 2
                    . 'short URLs (leave empty and this feature won\'t be enabled)'
51
                );
52
        }
53
54
        return '';
55
    }
56
}
57