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

UrlShortenerConfigCustomizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 73
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 6
B ask() 0 32 8
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
    public const ENABLE_NOT_FOUND_REDIRECTION = 'ENABLE_NOT_FOUND_REDIRECTION';
23
    public const NOT_FOUND_REDIRECT_TO = 'NOT_FOUND_REDIRECT_TO';
24
    private const EXPECTED_KEYS = [
25
        self::SCHEMA,
26
        self::HOSTNAME,
27
        self::CHARS,
28
        self::VALIDATE_URL,
29
        self::ENABLE_NOT_FOUND_REDIRECTION,
30
        self::NOT_FOUND_REDIRECT_TO,
31
    ];
32
33 4
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
34
    {
35 4
        $urlShortener = $appConfig->getUrlShortener();
36 4
        $doImport = $appConfig->hasUrlShortener();
37 4
        $keysToAskFor = $doImport ? array_diff(self::EXPECTED_KEYS, array_keys($urlShortener)) : self::EXPECTED_KEYS;
38
39 4
        if (empty($keysToAskFor)) {
40 1
            return;
41
        }
42
43 3
        $io->title('URL SHORTENER');
44 3
        foreach ($keysToAskFor as $key) {
45
            // Skip not found redirect URL when the user decided not to redirect
46 3
            if ($key === self::NOT_FOUND_REDIRECT_TO && ! $urlShortener[self::ENABLE_NOT_FOUND_REDIRECTION]) {
47 1
                continue;
48
            }
49
50 3
            $urlShortener[$key] = $this->ask($io, $key);
51
        }
52 3
        $appConfig->setUrlShortener($urlShortener);
53
    }
54
55 3
    private function ask(SymfonyStyle $io, string $key)
56
    {
57
        switch ($key) {
58 3
            case self::SCHEMA:
59 1
                return $io->choice(
60 1
                    'Select schema for generated short URLs',
61 1
                    ['http', 'https'],
62 1
                    'http'
63
                );
64 3
            case self::HOSTNAME:
65 1
                return $this->askRequired($io, 'hostname', 'Hostname for generated URLs');
66 3
            case self::CHARS:
67 2
                return $io->ask(
68 2
                    'Character set for generated short codes (leave empty to autogenerate one)'
69 2
                ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS);
70 3
            case self::VALIDATE_URL:
71 2
                return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
72 2
            case self::ENABLE_NOT_FOUND_REDIRECTION:
73 2
                return $io->confirm(
74
                    'Do you want to enable a redirection to a custom URL when a user hits an invalid short URL? ' .
75 2
                    '(If not enabled, the user will see a default "404 not found" page)',
76 2
                    false
77
                );
78 1
            case self::NOT_FOUND_REDIRECT_TO:
79 1
                return $this->askRequired(
80 1
                    $io,
81 1
                    'redirect URL',
82 1
                    'Custom URL to redirect to when a user hits an invalid short URL'
83
                );
84
        }
85
86
        return '';
87
    }
88
}
89