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

UrlShortenerConfigCustomizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
ccs 25
cts 26
cp 0.9615
wmc 10
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 4
B ask() 0 21 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Config\Plugin;
5
6
use Shlinkio\Shlink\Core\Service\UrlShortener;
7
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
8
use Shlinkio\Shlink\Installer\Util\AskUtilsTrait;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
use function array_diff;
11
use function array_keys;
12
use function str_shuffle;
13
14
class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
15
{
16
    use AskUtilsTrait;
17
18
    public const SCHEMA = 'SCHEMA';
19
    public const HOSTNAME = 'HOSTNAME';
20
    public const CHARS = 'CHARS';
21
    public const VALIDATE_URL = 'VALIDATE_URL';
22
    private const EXPECTED_KEYS = [
23
        self::SCHEMA,
24
        self::HOSTNAME,
25
        self::CHARS,
26
        self::VALIDATE_URL,
27
    ];
28
29 3
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
30
    {
31 3
        $urlShortener = $appConfig->getUrlShortener();
32 3
        $doImport = $appConfig->hasUrlShortener();
33 3
        $keysToAskFor = $doImport ? array_diff(self::EXPECTED_KEYS, array_keys($urlShortener)) : self::EXPECTED_KEYS;
34
35 3
        if (empty($keysToAskFor)) {
36 1
            return;
37
        }
38
39 2
        $io->title('URL SHORTENER');
40 2
        foreach ($keysToAskFor as $key) {
41 2
            $urlShortener[$key] = $this->ask($io, $key);
42
        }
43 2
        $appConfig->setUrlShortener($urlShortener);
44 2
    }
45
46 2
    private function ask(SymfonyStyle $io, string $key)
47
    {
48
        switch ($key) {
49 2
            case self::SCHEMA:
50 1
                return $io->choice(
51 1
                    'Select schema for generated short URLs',
52 1
                    ['http', 'https'],
53 1
                    'http'
54
                );
55 2
            case self::HOSTNAME:
56 1
                return $this->askRequired($io, 'hostname', 'Hostname for generated URLs');
57 2
            case self::CHARS:
58 2
                return $io->ask(
59 2
                    'Character set for generated short codes (leave empty to autogenerate one)'
60 2
                ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS);
61 2
            case self::VALIDATE_URL:
62 2
                return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
63
        }
64
65
        return '';
66
    }
67
}
68