Passed
Pull Request — master (#559)
by Alejandro
05:52
created

ConfigProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 4 1
A applyRoutesPrefix() 0 11 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