Completed
Push — master ( 1feeef...279c4f )
by Matthieu
8s
created

EnvironmentVariableDefinition::__toString()   A

Complexity

Conditions 4
Paths 6

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 18
Code Lines 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 18
rs 9.2
1
<?php
2
3
namespace DI\Definition;
4
5
use DI\Definition\Helper\DefinitionHelper;
6
use DI\Scope;
7
8
/**
9
 * Defines a reference to an environment variable, with fallback to a default
10
 * value if the environment variable is not defined.
11
 *
12
 * @author James Harris <[email protected]>
13
 */
14
class EnvironmentVariableDefinition implements CacheableDefinition
15
{
16
    /**
17
     * Entry name.
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * The name of the environment variable.
24
     * @var string
25
     */
26
    private $variableName;
27
28
    /**
29
     * Whether or not the environment variable definition is optional.
30
     *
31
     * If true and the environment variable given by $variableName has not been
32
     * defined, $defaultValue is used.
33
     *
34
     * @var bool
35
     */
36
    private $isOptional;
37
38
    /**
39
     * The default value to use if the environment variable is optional and not provided.
40
     * @var mixed
41
     */
42
    private $defaultValue;
43
44
    /**
45
     * @var string|null
46
     */
47
    private $scope;
48
49
    /**
50
     * @param string $name Entry name
51
     * @param string $variableName The name of the environment variable
52
     * @param bool $isOptional Whether or not the environment variable definition is optional
53
     * @param mixed $defaultValue The default value to use if the environment variable is optional and not provided
54
     */
55
    public function __construct($name, $variableName, $isOptional = false, $defaultValue = null)
56
    {
57
        $this->name = $name;
58
        $this->variableName = $variableName;
59
        $this->isOptional = $isOptional;
60
        $this->defaultValue = $defaultValue;
61
    }
62
63
    /**
64
     * @return string Entry name
65
     */
66
    public function getName()
67
    {
68
        return $this->name;
69
    }
70
71
    /**
72
     * @return string The name of the environment variable
73
     */
74
    public function getVariableName()
75
    {
76
        return $this->variableName;
77
    }
78
79
    /**
80
     * @return bool Whether or not the environment variable definition is optional
81
     */
82
    public function isOptional()
83
    {
84
        return $this->isOptional;
85
    }
86
87
    /**
88
     * @return mixed The default value to use if the environment variable is optional and not provided
89
     */
90
    public function getDefaultValue()
91
    {
92
        return $this->defaultValue;
93
    }
94
95
    /**
96
     * @param string $scope
97
     */
98
    public function setScope($scope)
99
    {
100
        $this->scope = $scope;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getScope()
107
    {
108
        return $this->scope ?: Scope::SINGLETON;
109
    }
110
111
    public function __toString()
112
    {
113
        $str = '    variable = ' . $this->variableName . PHP_EOL
114
            . '    optional = ' . ($this->isOptional ? 'yes' : 'no');
115
116
        if ($this->isOptional) {
117
            if ($this->defaultValue instanceof DefinitionHelper) {
118
                $nestedDefinition = (string) $this->defaultValue->getDefinition('');
119
                $defaultValueStr = str_replace(PHP_EOL, PHP_EOL . '    ', $nestedDefinition);
120
            } else {
121
                $defaultValueStr = var_export($this->defaultValue, true);
122
            }
123
124
            $str .= PHP_EOL . '    default = ' . $defaultValueStr;
125
        }
126
127
        return sprintf('Environment variable (' . PHP_EOL . '%s' . PHP_EOL . ')', $str);
128
    }
129
}
130