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

BasePathPrefixer::prefixPathsWithBasePath()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Config;
5
6
use function Functional\map;
7
8
class BasePathPrefixer
9
{
10
    private const ELEMENTS_WITH_PATH = ['routes', 'middleware_pipeline'];
11
12 3
    public function __invoke(array $config): array
13
    {
14 3
        $basePath = $config['router']['base_path'] ?? '';
15 3
        $config['url_shortener']['domain']['hostname'] .= $basePath;
16
17 3
        foreach (self::ELEMENTS_WITH_PATH as $configKey) {
18 3
            $config[$configKey] = $this->prefixPathsWithBasePath($configKey, $config, $basePath);
19
        }
20
21 3
        return $config;
22
    }
23
24 3
    private function prefixPathsWithBasePath(string $configKey, array $config, string $basePath): array
25
    {
26
        return map($config[$configKey] ?? [], function (array $element) use ($basePath) {
27 1
            if (! isset($element['path'])) {
28 1
                return $element;
29
            }
30
31 1
            $element['path'] = $basePath . $element['path'];
32 1
            return $element;
33 3
        });
34
    }
35
}
36