Completed
Push — master ( f0c4e3...468cfb )
by Tim
9s
created

UpstreamJsonConfiguration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 10.43 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 0
dl 12
loc 115
ccs 0
cts 46
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
B prepareServers() 12 28 4
A getName() 0 4 1
A getType() 0 4 1
A getServers() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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    Michael Doehler <[email protected]>
15
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
16
 * @link      https://github.com/appserver-io/server
17
 * @link      http://www.appserver.io
18
 */
19
20
namespace AppserverIo\Server\Configuration;
21
22
use AppserverIo\Server\Interfaces\UpstreamConfigurationInterface;
23
24
/**
25
 * Class UpstreamXmlConfiguration
26
 *
27
 * @author    Michael Doehler <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/server
30
 * @link      http://www.appserver.io
31
 */
32
class UpstreamJsonConfiguration implements UpstreamConfigurationInterface
33
{
34
    /**
35
     * Holds the name of the upstream node
36
     *
37
     * @var string
38
     */
39
    public $name;
40
41
    /**
42
     * Holds the type of the upstream node
43
     *
44
     * @var string
45
     */
46
    public $type;
47
48
    /**
49
     * Holds the channel of the upstream node
50
     *
51
     * @var string
52
     */
53
    public $channel;
54
55
    /**
56
     * Holds the servers for the upstream node
57
     *
58
     * @var array
59
     */
60
    public $servers;
61
62
    /**
63
     * Constructs config
64
     *
65
     * @param \stdClass $node The JSON node used to build config
66
     */
67
    public function __construct($node)
68
    {
69
        // prepare properties
70
        $this->name = (string)$node->name;
71
        $this->type = (string)$node->type;
72
73
        if (isset($node->channel)) {
74
            $this->channel = (string)$node->channel;
75
        }
76
77
        // prepare servers
78
        $this->servers = $this->prepareServers($node);
79
    }
80
81
    /**
82
     * Prepares server nodes configured for upstream
83
     *
84
     * @param \stdClass $node The JSON node for servers to prepare
85
     *
86
     * @return array
87
     */
88
    protected function prepareServers($node)
89
    {
90
        $servers = array();
91
        foreach ($node->servers->server as $serverNode) {
92
            $name = (string)$serverNode->name;
93
            $type = (string)$serverNode->type;
94
            $params = array();
95 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...
96
                $paramName = (string)$paramNode->name;
97
                $paramType = (string)$paramNode->type;
98
                $paramValue = (string)$paramNode;
99
                // check if type boolen and transform true and false strings to int
100
                if ($paramType === 'boolean') {
101
                    $paramValue = str_replace(array('true', 'false', '1', '0'), array(1, 0, 1, 0), $paramValue);
102
                }
103
                // set correct value type
104
                settype($paramValue, $paramType);
105
                $params[$paramName] = $paramValue;
106
            }
107
            $servers[$name] = array(
108
                'name' => $name,
109
                'type' => $type,
110
                'params' => $params
111
            );
112
        }
113
114
        return $servers;
115
    }
116
117
    /**
118
     * Returns name
119
     *
120
     * @return string
121
     */
122
    public function getName()
123
    {
124
        return $this->name;
125
    }
126
127
    /**
128
     * Returns type
129
     *
130
     * @return string
131
     */
132
    public function getType()
133
    {
134
        return $this->type;
135
    }
136
137
    /**
138
     * Returns servers
139
     *
140
     * @return array
141
     */
142
    public function getServers()
143
    {
144
        return $this->servers;
145
    }
146
}
147