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

UpstreamXmlConfiguration::prepareServers()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 12
Ratio 44.44 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 27
ccs 0
cts 25
cp 0
rs 8.5806
cc 4
eloc 19
nc 4
nop 1
crap 20
1
<?php
2
3
/**
4
 * \AppserverIo\Server\Configuration\UpstreamXmlConfiguration
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\Configuration;
22
23
use AppserverIo\Server\Interfaces\UpstreamConfigurationInterface;
24
25
/**
26
 * Class UpstreamXmlConfiguration
27
 *
28
 * @author    Johann Zelger <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/server
32
 * @link      http://www.appserver.io
33
 */
34
class UpstreamXmlConfiguration implements UpstreamConfigurationInterface
35
{
36
    /**
37
     * Holds the name of the upstream node
38
     *
39
     * @var string
40
     */
41
    public $name;
42
    
43
    /**
44
     * Holds the type of the upstream node
45
     *
46
     * @var string
47
     */
48
    public $type;
49
    
50
    /**
51
     * Holds the servers for the upstream node
52
     *
53
     * @var array
54
     */
55
    public $servers;
56
    
57
    /**
58
     * Constructs config
59
     *
60
     * @param \SimpleXMLElement $node The simple xml element used to build config
61
     */
62
    public function __construct(\SimpleXMLElement $node)
63
    {
64
        // prepare properties
65
        $this->name = (string)$node->attributes()->name;
66
        $this->type = (string)$node->attributes()->type;
67
        
68
        if (isset($node->attributes()->channel)) {
69
            $this->channel = (string)$node->attributes()->channel;
0 ignored issues
show
Bug introduced by
The property channel 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...
70
        }
71
    
72
        // prepare handlers
73
        $this->servers = $this->prepareServers($node);
74
    }
75
    
76
    /**
77
     * Prepares server nodes configured for upstream
78
     *
79
     * @param \SimpleXMLElement $node The xml node for servers to prepare
80
     *
81
     * @return array
82
     */
83
    protected function prepareServers(\SimpleXMLElement $node)
84
    {
85
        $servers = array();
86
        foreach ($node->servers->server as $serverNode) {
87
            $name = (string)$serverNode->attributes()->name;
88
            $type = (string)$serverNode->attributes()->type;
89
            $params = array();
90 View Code Duplication
            foreach ($serverNode->params->param as $paramNode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
                $paramName = (string)$paramNode->attributes()->name;
92
                $paramType = (string)$paramNode->attributes()->type;
93
                $paramValue = (string)$paramNode;
94
                // check if type boolen and transform true and false strings to int
95
                if ($paramType === 'boolean') {
96
                    $paramValue = str_replace(array('true', 'false', '1', '0'), array(1,0,1,0 ), $paramValue);
97
                }
98
                // set correct value type
99
                settype($paramValue, $paramType);
100
                $params[$paramName] = $paramValue;
101
            }
102
            $servers[$name] = array(
103
                'name' => $name,
104
                'type' => $type,
105
                'params' => $params
106
            );
107
        }
108
        return $servers;
109
    }
110
    
111
    /**
112
     * Returns name
113
     *
114
     * @return string
115
     */
116
    public function getName()
117
    {
118
        return $this->name;
119
    }
120
    
121
    /**
122
     * Returns type
123
     *
124
     * @return string
125
     */
126
    public function getType()
127
    {
128
        return $this->type;
129
    }
130
131
    /**
132
     * Returns servers
133
     *
134
     * @return array
135
     */
136
    public function getServers()
137
    {
138
        return $this->servers;
139
    }
140
}
141