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

InMemoryReader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 68
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Configuration;
6
7
use Karma\Configuration;
8
9
class InMemoryReader extends AbstractReader
10
{
11
    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...
12
        $values;
13
14 92
    public function __construct(array $values = [])
15
    {
16 92
        parent::__construct();
17
18 92
        $this->values = [];
19 92
        foreach($values as $key => $value)
20
        {
21 92
            $this->values[$this->removeSystemFlag($key)] = [
22 92
                'value' => $value,
23 92
                'system' => stripos($key, Configuration::SYSTEM_VARIABLE_FLAG) === 0,
24
            ];
25
        }
26 92
    }
27
28 66
    protected function readRaw(string $variable, ?string $environment = null)
29
    {
30 66
        if($environment === null)
31
        {
32 4
            $environment = $this->defaultEnvironment;
33
        }
34
35 66
        $key = "$variable:$environment";
36
37 66
        if(array_key_exists($key, $this->values))
38
        {
39 64
            return $this->values[$key]['value'];
40
        }
41
42 5
        throw new \RuntimeException("Variable $variable does not exist");
43
    }
44
45 78
    public function getAllVariables(): array
46
    {
47 78
        $variables = array_map(function($key){
48 78
            return $this->extractVariableName($key);
49 78
        }, array_keys($this->values));
50
51 78
        return array_unique($variables);
52
    }
53
54 7
    public function isSystem(string $variableName): bool
55
    {
56 7
        foreach($this->values as $key => $variable)
57
        {
58 7
            if($this->extractVariableName($key) === $variableName)
59
            {
60 6
                return $variable['system'];
61
            }
62
        }
63
64 1
        return false;
65
    }
66
67 81
    private function extractVariableName(string $key): string
68
    {
69 81
        return explode(':', $key)[0];
70
    }
71
72 92
    private function removeSystemFlag(string $variableName): string
73
    {
74 92
        return ltrim($variableName, Configuration::SYSTEM_VARIABLE_FLAG);
75
    }
76
}
77