ConfigFactory::make()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Config;
5
6
use Northwoods\Config\Loader\LoaderFactory;
7
8
class ConfigFactory
9
{
10 3
    public static function make(array $options, LoaderFactory $loader = null): ConfigInterface
11
    {
12 3
        $factory = new static($loader);
13 3
        $type = array_path($options, 'type', 'php');
14
15 3
        if (!empty($options['environment'])) {
16 2
            return $factory->forEnvironment($options['directory'], $options['environment'], $type);
17
        }
18
19 1
        return $factory->forDirectory($options['directory'], $type);
20
    }
21
22
    /** @var LoaderFactory */
23
    private $loader;
24
25 3
    public function __construct(LoaderFactory $loader = null)
26
    {
27 3
        $this->loader = $loader ?: new LoaderFactory();
28 3
    }
29
30 3
    public function forDirectory(string $directory, string $type = 'php'): ConfigDirectory
31
    {
32 3
        return new ConfigDirectory($directory, $this->loader->forType($type));
33
    }
34
35 2
    public function forEnvironment(string $directory, string $environment, string $type = 'php'): ConfigCollection
36
    {
37 2
        return new ConfigCollection(
38 2
            $this->forDirectory("$directory/$environment", $type),
39 2
            $this->forDirectory($directory, $type)
40
        );
41
    }
42
}
43