Completed
Pull Request — master (#404)
by Alejandro
06:26
created

ConfigPostProcessor::__invoke()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 3
nc 1
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core;
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 ConfigPostProcessor
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
        'locale' => ['translator', 'locale'],
25
    ];
26
    private const SIMPLIFIED_CONFIG_TOGGLES = [
27
        'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'enable_redirection'],
28
        'delete_short_url_threshold' => ['delete_short_urls', 'check_visits_threshold'],
29
    ];
30
    private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
31
32 1
    public function __invoke(array $config): array
33
    {
34 1
        $existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
35
36
        return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
37 1
            $path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
38 1
            if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
39 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

39
                $value = ArrayUtils::merge(/** @scrutinizer ignore-type */ $collection->getValueInPath($path), $value);
Loading history...
40
            }
41
42 1
            $collection->setValueInPath($value, $path);
43 1
            if (array_key_exists($key, self::SIMPLIFIED_CONFIG_TOGGLES)) {
44 1
                $collection->setValueInPath(true, self::SIMPLIFIED_CONFIG_TOGGLES[$key]);
45
            }
46
47 1
            return $collection;
48 1
        }, new PathCollection($config))->toArray();
49
    }
50
}
51