Completed
Pull Request — master (#93)
by
unknown
03:16
created

AbstractReader::setCustomData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
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 getDefaultEnvironment()
52
    {
53
        return $this->defaultEnvironment;
54
    }
55
56
    public function getAllValuesForEnvironment($environment = null)
57
    {
58
        $result = array();
59
60
        $variables = $this->getAllVariables();
61
62
        foreach($variables as $variable)
63
        {
64
            try
65
            {
66
                $value = $this->read($variable, $environment);
67
            }
68
            catch(\RuntimeException $e)
69
            {
70
                $value = Configuration::NOT_FOUND;
71
            }
72
73
            $result[$variable] = $value;
74
        }
75
76
        return $result;
77
    }
78
79
    public function overrideVariable($variable, $value)
80
    {
81
        $this->overridenVariables[$variable] = $value;
82
83
        return $this;
84
    }
85
86
    public function setCustomData($customDataName, $value)
87
    {
88
        $key = '${' . $customDataName . '}';
89
        $this->customData[$key] = $value;
90
91
        return $this;
92
    }
93
94
    private function handleCustomData($value)
95
    {
96
        if(! is_string($value))
97
        {
98
            return $value;
99
        }
100
101
        return strtr($value, $this->customData);
102
    }
103
}
104