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

BasePathPrefixer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 11
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 10
c 2
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
A prefixPathsWithBasePath() 0 9 2
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