Passed
Pull Request — master (#39)
by
unknown
04:19 queued 01:47
created

ConfigFactory::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 3
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Runner;
6
7
use ErrorException;
8
use Yiisoft\Config\Config;
9
use Yiisoft\Config\ConfigPaths;
10
use Yiisoft\Config\Modifier\RecursiveMerge;
11
use Yiisoft\Config\Modifier\ReverseMerge;
12
13
/**
14
 * Creates a Config instance for a given environment.
15
 */
16
final class ConfigFactory
17
{
18
    /**
19
     * @throws ErrorException If the environment does not exist.
20
     */
21 11
    public static function create(
22
        ConfigPaths $paths,
23
        ?string $environment,
24
        ?string $configGroupPostfix = null,
25
    ): Config {
26 11
        $paramsGroups = ['params'];
27 11
        if ($configGroupPostfix !== null) {
28 9
            $paramsGroups[] = 'params-' . $configGroupPostfix;
29
        }
30
31 11
        $eventGroups = ['events'];
32 11
        if ($configGroupPostfix !== null) {
33 9
            $eventGroups[] = 'events-' . $configGroupPostfix;
34
        }
35
36 11
        return new Config(
37 11
            $paths,
38 11
            $environment,
39 11
            [
40 11
                ReverseMerge::groups(...$eventGroups),
41 11
                RecursiveMerge::groups(...$paramsGroups, ...$eventGroups),
42 11
            ],
43 11
            'params',
44 11
        );
45
    }
46
}
47