Completed
Push — 42-formatter ( cff7a4 )
by Nicolas
32:10 queued 28:41
created

Reader::setDefaultEnvironment()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Karma\Configuration;
4
5
class Reader extends AbstractReader
6
{
7
    const
8
        DEFAULT_ENVIRONMENT = 'default',
9
        DEFAULT_VALUE_FOR_ENVIRONMENT_PARAMETER = 'prod',
10
        EXTERNAL = '<external>';
11
    
12
    private
13
        $variables,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
14
        $externalReader;
15
    
16
    public function __construct(array $variables, array $externalVariables)
17
    {
18
        parent::__construct();
19
        
20
        $this->defaultEnvironment = self::DEFAULT_VALUE_FOR_ENVIRONMENT_PARAMETER;
21
        
22
        $this->variables = $variables;
23
        
24
        $this->externalReader = null;
25
        if(! empty($externalVariables))
26
        {
27
            $this->externalReader = new Reader($externalVariables, array());
28
        }
29
    }    
30
    
31
    protected function readRaw($variable, $environment = null)
32
    {
33
        if($environment === null)
34
        {
35
            $environment = $this->defaultEnvironment;
36
        }
37
        
38
        return $this->readVariable($variable, $environment);
39
    }
40
    
41
    private function readVariable($variable, $environment)
42
    {
43
        if(! array_key_exists($variable, $this->variables))
44
        {
45
            throw new \RuntimeException(sprintf(
46
                'Unknown variable %s',
47
                $variable
48
            ));   
49
        }
50
        
51
        $envs = $this->variables[$variable]['env'];
52
53
        foreach(array($environment, self::DEFAULT_ENVIRONMENT) as $searchedEnvironment)
54
        {
55
            if(array_key_exists($searchedEnvironment, $envs))
56
            {
57
                $value = $envs[$searchedEnvironment];
58
                
59
                if($value === self::EXTERNAL)
60
                {
61
                    $value = $this->processExternal($variable, $environment);
62
                }
63
                
64
                return $value;
65
            }
66
        }
67
        
68
        throw new \RuntimeException(sprintf(
69
            'Value not found of variable %s in environment %s (and no default value has been provided)',
70
            $variable,
71
            $environment
72
        ));
73
    }
74
    
75
    private function processExternal($variable, $environment)
76
    {
77
        if(! $this->externalReader instanceof Reader)
78
        {
79
            throw new \RuntimeException(sprintf(
80
                'There is no external variables. %s can not be resolve for environment %s',
81
                $variable,
82
                $environment
83
            ));    
84
        }
85
        
86
        return $this->externalReader->read($variable, $environment);
87
    }
88
    
89
    public function getAllVariables()
90
    {
91
        return array_keys($this->variables);
92
    }
93
    
94
    public function compareEnvironments($environment1, $environment2)
95
    {
96
        $values1 = $this->getAllValuesForEnvironment($environment1);
97
        $values2 = $this->getAllValuesForEnvironment($environment2);
98
        
99
        $diff = array();
100
        
101
        foreach($values1 as $name => $value1)
102
        {
103
            $value2 = $values2[$name];
104
            
105
            if($value1 !== $value2)
106
            {
107
                $diff[$name] = array($value1, $value2);
108
            }
109
        }
110
        
111
        return $diff;
112
    }
113
}