Passed
Pull Request — master (#485)
by Alejandro
06:11 queued 14s
created

SimplifiedConfigParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 47
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Config;
5
6
use Shlinkio\Shlink\Installer\Util\PathCollection;
7
use Zend\Stdlib\ArrayUtils;
8
9
use function array_intersect_key;
10
use function array_key_exists;
11
use function Functional\contains;
12
use function Functional\reduce_left;
13
14
class SimplifiedConfigParser
15
{
16
    private const SIMPLIFIED_CONFIG_MAPPING = [
17
        'disable_track_param' => ['app_options', 'disable_track_param'],
18
        'short_domain_schema' => ['url_shortener', 'domain', 'schema'],
19
        'short_domain_host' => ['url_shortener', 'domain', 'hostname'],
20
        'validate_url' => ['url_shortener', 'validate_url'],
21
        'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'redirect_to'],
22
        'db_config' => ['entity_manager', 'connection'],
23
        'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
24
        'redis_servers' => ['redis', 'servers'],
25
        'base_path' => ['router', 'base_path'],
26
    ];
27
    private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
28
        'not_found_redirect_to' => [
29
            'path' => ['url_shortener', 'not_found_short_url', 'enable_redirection'],
30
            'value' => true,
31
        ],
32
        'delete_short_url_threshold' => [
33
            'path' => ['delete_short_urls', 'check_visits_threshold'],
34
            'value' => true,
35
        ],
36
        'redis_servers' => [
37
            'path' => ['dependencies', 'aliases', 'lock_store'],
38
            'value' => 'redis_lock_store',
39
        ],
40
    ];
41
    private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
42
43 1
    public function __invoke(array $config): array
44
    {
45 1
        $existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
46
47
        return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
48 1
            $path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
49 1
            if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
50 1
                $value = ArrayUtils::merge($collection->getValueInPath($path), $value);
0 ignored issues
show
Bug introduced by
It seems like $collection->getValueInPath($path) can also be of type null; however, parameter $a of Zend\Stdlib\ArrayUtils::merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
                $value = ArrayUtils::merge(/** @scrutinizer ignore-type */ $collection->getValueInPath($path), $value);
Loading history...
51
            }
52
53 1
            $collection->setValueInPath($value, $path);
54 1
            if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) {
55 1
                ['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key];
56 1
                $collection->setValueInPath($sideEffectValue, $sideEffectPath);
57
            }
58
59 1
            return $collection;
60 1
        }, new PathCollection($config))->toArray();
61
    }
62
}
63