Completed
Push — master ( 8cc4d3...038254 )
by Alejandro
16s queued 11s
created

ConfigProvider::applyRoutesPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest;
6
7
use function Shlinkio\Shlink\Common\loadConfigFromGlob;
8
use function sprintf;
9
10
class ConfigProvider
11
{
12
    private const ROUTES_PREFIX = '/rest/v{version:1|2}';
13
14
    /** @var callable */
15
    private $loadConfig;
16
17 2
    public function __construct(?callable $loadConfig = null)
18
    {
19
        $this->loadConfig = $loadConfig ?? function (string $glob) {
20 1
            return loadConfigFromGlob($glob);
21 2
        };
22
    }
23
24 2
    public function __invoke()
25
    {
26 2
        $config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php');
27 2
        return $this->applyRoutesPrefix($config);
28
    }
29
30 2
    private function applyRoutesPrefix(array $config): array
31
    {
32 2
        $routes =& $config['routes'] ?? [];
33
34
        // Prepend the routes prefix to every path
35 2
        foreach ($routes as $key => $route) {
36 2
            ['path' => $path] = $route;
37 2
            $routes[$key]['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
38
        }
39
40 2
        return $config;
41
    }
42
}
43