Completed
Push — master ( 468cfb...a6bc20 )
by Tim
06:21 queued 02:51
created

ServerVarsArrayTrait::unsetServerVar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * \AppserverIo\Server\Traits\ServerVarsArrayTrait
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Johann Zelger <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Traits;
22
23
use AppserverIo\Server\Dictionaries\ServerVars;
24
use AppserverIo\Server\Exceptions\ServerException;
25
26
/**
27
 * Trait ServerVarsArrayTrait
28
 *
29
 * @author    Johann Zelger <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/server
33
 * @link      http://www.appserver.io
34
 */
35
trait ServerVarsArrayTrait
36
{
37
38
    /**
39
     * Maps configuration value types to string values.
40
     *
41
     * @var array
42
     */
43
    protected $typeMappings = array(
44
        ServerVars::SERVER_AUTO_INDEX => array(
45
            true  => ServerVars::VALUE_AUTO_INDEX_ON,
46
            false => ServerVars::VALUE_AUTO_INDEX_OFF
47
        ),
48
        ServerVars::HTTPS => array(
49
            'ssl' => ServerVars::VALUE_HTTPS_ON,
50
            'tcp' => ServerVars::VALUE_HTTPS_OFF
51
        )
52
    );
53
54
    /**
55
     * Sets a value to specific server var
56
     *
57
     * @param string $serverVar The server var to set
58
     * @param string $value     The value to server var
59
     *
60
     * @return void
61
     */
62 1
    public function setServerVar($serverVar, $value)
63
    {
64 1
        if (!is_null($value)) {
65 1
            if (isset($this->typeMappings[$serverVar][$value])) {
66
                $this->serverVars[$serverVar] = $this->typeMappings[$serverVar][$value];
0 ignored issues
show
Bug introduced by
The property serverVars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
            } else {
68 1
                $this->serverVars[$serverVar] = $value;
69
            }
70 1
        }
71 1
    }
72
73
    /**
74
     * Unsets a specific server var
75
     *
76
     * @param string $serverVar The server var to unset
77
     *
78
     * @return void
79
     */
80
    public function unsetServerVar($serverVar)
81
    {
82
        if (isset($this->serverVars[$serverVar])) {
83
            unset($this->serverVars[$serverVar]);
84
        }
85
    }
86
87
    /**
88
     * Returns a value for specific server var
89
     *
90
     * @param string $serverVar The server var to get value for
91
     *
92
     * @throws \AppserverIo\Server\Exceptions\ServerException
93
     *
94
     * @return string The value to given server var
95
     */
96 1
    public function getServerVar($serverVar)
97
    {
98
        // check if server var is set
99 1
        if (isset($this->serverVars[$serverVar])) {
100 1
            return $this->serverVars[$serverVar];
101
        }
102
        // throw exception
103
        throw new ServerException("Server var '$serverVar'' does not exist.", 500);
104
    }
105
106
    /**
107
     * Returns all the server vars as array key value pair format.
108
     *
109
     * @return array The server vars as array
110
     */
111
    public function getServerVars()
112
    {
113
        return $this->serverVars;
114
    }
115
116
    /**
117
     * Checks if value exists for given server var
118
     *
119
     * @param string $serverVar The server var to check
120
     *
121
     * @return bool Weather it has serverVar (true) or not (false)
122
     */
123
    public function hasServerVar($serverVar)
124
    {
125
        // check if server var is set
126
        if (!isset($this->serverVars[$serverVar])) {
127
            return false;
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Clears the server vars storage
135
     *
136
     * @return void
137
     */
138
    public function clearServerVars()
139
    {
140
        foreach (array_keys($this->serverVars) as $key) {
141
            unset($this->serverVars[$key]);
142
        }
143
    }
144
}
145