Completed
Pull Request — master (#604)
by Alejandro
11:57 queued 08:51
created

SimplifiedConfigParser::__invoke()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 1
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Config;
6
7
use Laminas\Stdlib\ArrayUtils;
8
use Shlinkio\Shlink\Installer\Util\PathCollection;
9
10
use function array_flip;
11
use function array_intersect_key;
12
use function array_key_exists;
13
use function array_keys;
14
use function Functional\contains;
15
use function Functional\reduce_left;
16
use function uksort;
17
18
class SimplifiedConfigParser
19
{
20
    private const SIMPLIFIED_CONFIG_MAPPING = [
21
        'disable_track_param' => ['app_options', 'disable_track_param'],
22
        'short_domain_schema' => ['url_shortener', 'domain', 'schema'],
23
        'short_domain_host' => ['url_shortener', 'domain', 'hostname'],
24
        'validate_url' => ['url_shortener', 'validate_url'],
25
        'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
26
        'regular_404_redirect_to' => ['not_found_redirects', 'regular_404'],
27
        'base_url_redirect_to' => ['not_found_redirects', 'base_path'],
28
        'db_config' => ['entity_manager', 'connection'],
29
        'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
30
        'redis_servers' => ['cache', 'redis', 'servers'],
31
        'base_path' => ['router', 'base_path'],
32
        'web_worker_num' => ['mezzio-swoole', 'swoole-http-server', 'options', 'worker_num'],
33
        'task_worker_num' => ['mezzio-swoole', 'swoole-http-server', 'options', 'task_worker_num'],
34
        'visits_webhooks' => ['url_shortener', 'visits_webhooks'],
35
    ];
36
    private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
37
        'delete_short_url_threshold' => [
38
            'path' => ['delete_short_urls', 'check_visits_threshold'],
39
            'value' => true,
40
        ],
41
        'redis_servers' => [
42
            'path' => ['dependencies', 'aliases', 'lock_store'],
43
            'value' => 'redis_lock_store',
44
        ],
45
    ];
46
    private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
47
48 1
    public function __invoke(array $config): array
49
    {
50 1
        $configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config);
51
52
        return reduce_left($configForExistingKeys, function ($value, string $key, $c, PathCollection $collection) {
53 1
            $path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
54 1
            if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
55 1
                $value = ArrayUtils::merge($collection->getValueInPath($path), $value);
56
            }
57
58 1
            $collection->setValueInPath($value, $path);
59 1
            if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) {
60 1
                ['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key];
61 1
                $collection->setValueInPath($sideEffectValue, $sideEffectPath);
62
            }
63
64 1
            return $collection;
65 1
        }, new PathCollection($config))->toArray();
66
    }
67
68 1
    private function getConfigForKeysInMappingOrderedByMapping(array $config): array
69
    {
70
        // Ignore any config which is not defined in the mapping
71 1
        $configForExistingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
72
73
        // Order the config by their key, based on the order it was defined in the mapping.
74
        // This mainly allows deprecating keys and defining new ones that will replace the older and always take
75
        // preference, while the old one keeps working for backwards compatibility if the new one is not provided.
76 1
        $simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING));
77 1
        uksort(
78 1
            $configForExistingKeys,
79 1
            fn (string $a, string $b): int => $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b],
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 79 at column 37
Loading history...
80
        );
81
82 1
        return $configForExistingKeys;
83
    }
84
}
85