Completed
Push — nln-php7 ( 6680df...1a6b54 )
by Nicolas
02:06
created

Reader::readVariable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 11
cts 11
cp 1
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Configuration;
6
7
class Reader extends AbstractReader
8
{
9
    private const
10
        DEFAULT_ENVIRONMENT = 'default',
11
        DEFAULT_VALUE_FOR_ENVIRONMENT_PARAMETER = 'prod',
12
        EXTERNAL = '<external>';
13
14
    private array
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
15
        $variables,
16
        $groupNames,
17
        $environmentGroups,
18
        $defaultEnvironmentsForGroups;
19
    private ?Reader
20
        $externalReader;
21
22 186
    public function __construct(array $variables, array $externalVariables, array $groups = [], array $defaultEnvironmentsForGroups = [])
23
    {
24 186
        parent::__construct();
25
26 186
        $this->defaultEnvironment = self::DEFAULT_VALUE_FOR_ENVIRONMENT_PARAMETER;
27
28 186
        $this->variables = $variables;
29
30 186
        $this->externalReader = null;
31 186
        if(! empty($externalVariables))
32
        {
33 11
            $this->externalReader = new Reader($externalVariables, [], $groups);
34
        }
35
36 186
        $this->loadGroups($groups);
37 186
        $this->defaultEnvironmentsForGroups = $defaultEnvironmentsForGroups;
38 186
    }
39
40 186
    private function loadGroups(array $groups): void
41
    {
42 186
        $this->environmentGroups = [];
43
44 186
        foreach($groups as $group => $environments)
45
        {
46 5
            foreach($environments as $environment)
47
            {
48 5
                $this->environmentGroups[$environment] = $group;
49
            }
50
        }
51
52 186
        $this->groupNames = array_keys($groups);
53 186
    }
54
55 163
    protected function readRaw(string $variable, ?string $environment = null)
56
    {
57 163
        if($environment === null)
58
        {
59 65
            $environment = $this->defaultEnvironment;
60
        }
61
62 163
        if(in_array($environment, $this->groupNames))
63
        {
64 2
            if(! isset($this->defaultEnvironmentsForGroups[$environment]))
65
            {
66 1
                throw new \RuntimeException(sprintf(
67 1
                   'Group can not be used as environment (try with group %s detected)',
68
                    $environment
69
                ));
70
            }
71
72 1
            $environment = $this->defaultEnvironmentsForGroups[$environment];
73
        }
74
75 162
        return $this->readVariable($variable, $environment);
76
    }
77
78 162
    private function readVariable(string $variableName, string $environment)
79
    {
80 162
        $variable = $this->accessVariable($variableName);
81 160
        $envs = $variable['env'];
82
83 160
        foreach($this->getEnvironmentEntries($environment) as $entry)
84
        {
85 160
            if(array_key_exists($entry, $envs))
86
            {
87 159
                $value = $envs[$entry];
88
89 159
                if($value === self::EXTERNAL)
90
                {
91 5
                    $value = $this->processExternal($variableName, $environment);
92
                }
93
94 156
                return $value;
95
            }
96
        }
97
98 6
        throw new \RuntimeException(sprintf(
99 6
            'Value not found of variable %s in environment %s (and no default value has been provided)',
100
            $variableName,
101
            $environment
102
        ));
103
    }
104
105 166
    private function accessVariable(string $variableName)
106
    {
107 166
        if(! array_key_exists($variableName, $this->variables))
108
        {
109 2
            throw new \RuntimeException(sprintf(
110 2
                'Unknown variable %s',
111
                $variableName
112
            ));
113
        }
114
115 164
        return $this->variables[$variableName];
116
    }
117
118 160
    private function getEnvironmentEntries(string $environment): array
119
    {
120 160
        $entries = array($environment);
121
122 160
        if(isset($this->environmentGroups[$environment]))
123
        {
124 4
            $entries[] = $this->environmentGroups[$environment];
125
        }
126
127 160
        $entries[] = self::DEFAULT_ENVIRONMENT;
128
129 160
        return $entries;
130
    }
131
132 5
    private function processExternal(string $variable, string $environment)
133
    {
134 5
        if(! $this->externalReader instanceof self)
135
        {
136 2
            throw new \RuntimeException(sprintf(
137 2
                'There is no external variables. %s can not be resolve for environment %s',
138
                $variable,
139
                $environment
140
            ));
141
        }
142
143 3
        return $this->externalReader->read($variable, $environment);
144
    }
145
146 23
    public function getAllVariables(): array
147
    {
148 23
        return array_keys($this->variables);
149
    }
150
151 3
    public function compareEnvironments(string $environment1, string $environment2): array
152
    {
153 3
        $values1 = $this->getAllValuesForEnvironment($environment1);
154 3
        $values2 = $this->getAllValuesForEnvironment($environment2);
155
156 3
        $diff = [];
157
158 3
        foreach($values1 as $name => $value1)
159
        {
160 3
            $value2 = $values2[$name];
161
162 3
            if($value1 !== $value2)
163
            {
164 3
                $diff[$name] = [$value1, $value2];
165
            }
166
        }
167
168 3
        return $diff;
169
    }
170
171 7
    public function isSystem(string $variableName): bool
172
    {
173 7
        $variable = $this->accessVariable($variableName);
174
175 7
        return $variable['system'];
176
    }
177
}
178