Completed
Push — master ( 79c132...ccb7c8 )
by Alejandro
25s
created

UrlShortenerConfigCustomizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 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 Shlinkio\Shlink\Installer\Util\AskUtilsTrait;
8
use Symfony\Component\Console\Style\SymfonyStyle;
9
use function array_diff;
10
use function array_keys;
11
use function count;
12
use function Functional\contains;
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
    /** @var callable */
34
    private $randomCharsGenerator;
35
36 4
    public function __construct(callable $randomCharsGenerator)
37
    {
38 4
        $this->randomCharsGenerator = $randomCharsGenerator;
39
    }
40
41 4
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
42
    {
43 4
        $urlShortener = $appConfig->getUrlShortener();
44 4
        $doImport = $appConfig->hasUrlShortener();
45 4
        $keysToAskFor = $doImport ? array_diff(self::EXPECTED_KEYS, array_keys($urlShortener)) : self::EXPECTED_KEYS;
46
47 4
        if (empty($keysToAskFor)) {
48 1
            return;
49
        }
50
51
        // Print title if there are keys other than "chars"
52 3
        $onlyKeyIsChars = count($keysToAskFor) === 1 && contains($keysToAskFor, self::CHARS);
53 3
        if (! $onlyKeyIsChars) {
54 3
            $io->title('URL SHORTENER');
55
        }
56 3
        foreach ($keysToAskFor as $key) {
57
            // Skip not found redirect URL when the user decided not to redirect
58 3
            if ($key === self::NOT_FOUND_REDIRECT_TO && ! $urlShortener[self::ENABLE_NOT_FOUND_REDIRECTION]) {
59 1
                continue;
60
            }
61
62 3
            $urlShortener[$key] = $this->ask($io, $key);
63
        }
64 3
        $appConfig->setUrlShortener($urlShortener);
65
    }
66
67 3
    private function ask(SymfonyStyle $io, string $key)
68
    {
69
        switch ($key) {
70 3
            case self::SCHEMA:
71 1
                return $io->choice(
72 1
                    'Select schema for generated short URLs',
73 1
                    ['http', 'https'],
74 1
                    'http'
75
                );
76 3
            case self::HOSTNAME:
77 2
                return $this->askRequired($io, 'hostname', 'Hostname for generated URLs');
78 3
            case self::CHARS:
79
                // This won't actually ask anything, just generate the chars. Asking for this was confusing for users
80 2
                return ($this->randomCharsGenerator)();
81 3
            case self::VALIDATE_URL:
82 2
                return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
83 2
            case self::ENABLE_NOT_FOUND_REDIRECTION:
84 2
                return $io->confirm(
85
                    'Do you want to enable a redirection to a custom URL when a user hits an invalid short URL? ' .
86 2
                    '(If not enabled, the user will see a default "404 not found" page)',
87 2
                    false
88
                );
89 1
            case self::NOT_FOUND_REDIRECT_TO:
90 1
                return $this->askRequired(
91 1
                    $io,
92 1
                    'redirect URL',
93 1
                    'Custom URL to redirect to when a user hits an invalid short URL'
94
                );
95
        }
96
97
        return '';
98
    }
99
}
100