ConfigEnv::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * This file is part of the php-utilities package.
5
 *
6
 * (c) Marc Aschmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Asm\Config;
12
13
use Asm\Data\Data;
14
15
/**
16
 * Class ConfigDefault
17
 *
18
 * @package Asm\Config
19
 * @author Marc Aschmann <[email protected]>
20
 */
21
final class ConfigEnv extends AbstractConfig implements ConfigInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $defaultEnv = 'prod';
27
28
    /**
29
     * Default constructor.
30
     *
31
     * @param array $param
32
     */
33
    public function __construct(array $param)
34
    {
35
        if (isset($param[self::FILECHECK])) {
36
            $this->filecheck = (bool)$param[self::FILECHECK];
37
        }
38
39
        if (!empty($param[self::DEFAULT_ENVIRONMENT])) {
40
            $this->defaultEnv = $param[self::DEFAULT_ENVIRONMENT];
41
        }
42
43
        $this->mergeEnvironments($param);
44
    }
45
46
    /**
47
     * Merge environments based on defaults array.
48
     * Merge order is prod -> lesser environment.
49
     *
50
     * @param array $param
51
     */
52
    private function mergeEnvironments(array $param)
53
    {
54
        $config = new Data();
55
        $config->setByArray(
56
            $this->readConfig($param['file'])
57
        );
58
59
        if (!empty($param[self::ENVIRONMENT]) && $this->defaultEnv !== $param[self::ENVIRONMENT]) {
60
            $toMerge = $config->get($param[self::ENVIRONMENT], []);
61
            $merged = array_replace_recursive(
62
                $config->get($this->defaultEnv),
63
                $toMerge
64
            );
65
        } else {
66
            $merged = $config->get($this->defaultEnv, []);
67
        }
68
69
        $this->setByArray(
70
            array_replace_recursive(
71
                $this->default,
72
                $merged
73
            )
74
        );
75
    }
76
}
77