BasePathPrefixer::prefixPathsWithBasePath()   A
last analyzed

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
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Config;
6
7
use function Functional\map;
8
9
class BasePathPrefixer
10
{
11
    private const ELEMENTS_WITH_PATH = ['routes', 'middleware_pipeline'];
12
13 3
    public function __invoke(array $config): array
14
    {
15 3
        $basePath = $config['router']['base_path'] ?? '';
16 3
        $config['url_shortener']['domain']['hostname'] .= $basePath;
17
18 3
        foreach (self::ELEMENTS_WITH_PATH as $configKey) {
19 3
            $config[$configKey] = $this->prefixPathsWithBasePath($configKey, $config, $basePath);
20
        }
21
22 3
        return $config;
23
    }
24
25 3
    private function prefixPathsWithBasePath(string $configKey, array $config, string $basePath): array
26
    {
27
        return map($config[$configKey] ?? [], function (array $element) use ($basePath) {
28 1
            if (! isset($element['path'])) {
29 1
                return $element;
30
            }
31
32 1
            $element['path'] = $basePath . $element['path'];
33 1
            return $element;
34 3
        });
35
    }
36
}
37