Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

AbstractReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Karma\Configuration;
4
5
use Karma\Configuration;
6
7
abstract class AbstractReader implements Configuration
8
{
9
    protected
10
        $defaultEnvironment;
11
    
12
    private
13
        $overridenVariables,
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
        $customData;
15
    
16
    public function __construct()
17
    {
18
        $this->defaultEnvironment = 'dev';
19
        $this->overridenVariables = array();
20
        $this->customData = array();
21
    }
22
    
23
    public function read($variable, $environment = null)
24
    {
25
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        
27
        if(array_key_exists($variable, $this->overridenVariables))
28
        {
29
            $value = $this->overridenVariables[$variable];
30
        }
31
        else
32
        {
33
            $value = $this->readRaw($variable, $environment);
34
        }
35
        
36
        return $this->handleCustomData($value);
37
    }
38
    
39
    abstract protected function readRaw($variable, $environment = null);
40
    
41
    public function setDefaultEnvironment($environment)
42
    {
43
        if(! empty($environment) && is_string($environment))
44
        {
45
            $this->defaultEnvironment = $environment;
46
        }
47
        
48
        return $this;
49
    }
50
    
51
    public function getAllValuesForEnvironment($environment = null)
52
    {
53
        $result = array();
54
        
55
        $variables = $this->getAllVariables();
56
        
57
        foreach($variables as $variable)
58
        {
59
            try
60
            {
61
                $value = $this->read($variable, $environment);
62
            }
63
            catch(\RuntimeException $e)
64
            {
65
                $value = Configuration::NOT_FOUND;
66
            }
67
        
68
            $result[$variable] = $value;
69
        }    
70
        
71
        return $result;
72
    }
73
    
74
    public function overrideVariable($variable, $value)
75
    {
76
        $this->overridenVariables[$variable] = $value;
77
78
        return $this;
79
    }
80
    
81
    public function setCustomData($customDataName, $value)
82
    {
83
        $key = '${' . $customDataName . '}';
84
        $this->customData[$key] = $value;
85
        
86
        return $this;
87
    }
88
    
89
    private function handleCustomData($value)
90
    {
91
        if(! is_string($value))
92
        {
93
            return $value;
94
        }
95
        
96
        return strtr($value, $this->customData);
97
    }
98
}