Test Setup Failed
Push — master ( 8251c7...c3d3f3 )
by Tomasz
07:30
created

DeviceXMLmain   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 33
eloc 87
c 4
b 1
f 0
dl 0
loc 225
rs 9.76

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setType() 0 6 2
A setAttribute() 0 6 2
A setChild() 0 3 1
A setValue() 0 6 2
A areAttributes() 0 3 2
F marshalObject() 0 83 19
A getValue() 0 3 1
A __construct() 0 6 1
A getAttributes() 0 3 1
A setAttributes() 0 3 1
A getType() 0 2 1
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This file contains class definitions and procedures for 
24
 * generation of a generic XML description of a 802.1x configurator
25
 *
26
 * @author Maja Górecka-Wolniewicz <[email protected]>
27
 * 
28
 * @contributor Tomasz Wolniewicz <[email protected]>
29
 *
30
 * @package ModuleWriting
31
 */
32
namespace core;
33
use Exception;
34
35
/**
36
 * Base class currently used by XML based devices lile MS profiles
37
 * and eap-config profiles. 
38
 * 
39
 * The leaf objects may have scalar values which are stored as the $value,
40
 * non-leaf objects have children stored as $children array
41
 * 
42
 * Nodes may also have attributes which are stored as elemens of te $attrinutes
43
 * array. That array is indexed with attribute names and holds attibute values.
44
 * 
45
 * The node name is not being set, it is the parent that knows that.
46
 *  
47
 */
48
class DeviceXMLmain
49
{
50
    /**
51
     * attributes of this object instance
52
     * 
53
     * @var array
54
     */
55
    private $attributes;
56
    /**
57
     * children of the current object
58
     * 
59
     * @var array
60
     */
61
    
62
    private $children;
63
    
64
    /**
65
     * The value of a basic element.
66
     * 
67
     * @var string
68
     */
69
    private $value;
70
    
71
72
    /**
73
     * constructor, initialises empty set of attributes and value
74
     */
75
    public function __construct()
76
    {
77
        $this->attributes = [];
78
        $this->value = '';
79
        $this->children = [];
80
        $this->type = NULL;
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
    }
82
    
83
    /**
84
     * sets a list of attributes in the current object instance
85
     * 
86
     * @param array $attributes the list of attributes
87
     * @return void
88
     */
89
    public function setAttributes($attributes)
90
    {
91
        $this->attributes = $attributes;
92
    }
93
94
    /**
95
     * retrieves list of attributes from object instance
96
     * @return array
97
     */
98
    public function getAttributes()
99
    {
100
        return $this->attributes;
101
    }
102
103
    /**
104
     * Used to set scalar values of basic XML elements
105
     * 
106
     * @param scalar $value
107
     * @return void
108
     */
109
    public function setValue($value)
110
    {
111
        if (is_scalar($value)) {
112
            $this->value = strval($value);
113
        } else {
114
            throw new Exception("unexpected value type passed".gettype($value));
115
        }
116
    }
117
    
118
    public function setType($type)
119
    {
120
        if (empty($type)) {
121
            return;
122
        }
123
        $this->type = $type;
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
124
    }
125
    
126
    public function getType() {
127
        return $this->type;
128
    }
129
130
    /**
131
     * retrieves value of a basic XML object instance
132
     * 
133
     * @return string
134
     */
135
    public function getValue()
136
    {
137
        return $this->value;
138
    }
139
140
    /**
141
     * does this object have attributes?
142
     * 
143
     * @return boolean
144
     */
145
    public function areAttributes()
146
    {
147
        return empty($this->attributes) ? false : true;
148
    }
149
150
    /**
151
     * adds an attribute with the given value to the set of attributes
152
     * @param string $attribute attribute to set
153
     * @param mixed  $value     value to set
154
     * @return void
155
     */
156
    public function setAttribute($attribute, $value)
157
    {
158
        if (!isset($this->attributes)) {
159
            $this->attributes = [];
160
        }
161
        $this->attributes[$attribute] = $value;
162
    }
163
164
    /**
165
     * adds a child to the current object instance
166
     * 
167
     * @param string $name the child element name to set
168
     * @param mixed  $value    value to set
169
     * @return void
170
     */
171
    public function setChild($name, $value, $namespace = NULL, $type = NULL)
172
    {
173
        $this->children[] = ['name' => $name, 'value' => $value, 'namespace' => $namespace, 'type' => $type];
174
    }
175
176
    /**
177
     * This method is used to generate 
178
     * 
179
     * @param $node DOM object to which the node is to be attached
0 ignored issues
show
Bug introduced by
The type core\DOM was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
180
     * @param $name the tag name of the child node to be attached
0 ignored issues
show
Bug introduced by
The type core\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
181
     * @param $object the XXX object which is to be transfored to the DOM object
182
     *     and attached as a child to the $node
183
     * @param $namespace of the child
0 ignored issues
show
Bug introduced by
The type core\of was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
184
     * @param $root Boolean - if true treat the node as the tree root, i.e do not
185
     *     attach the resulting object to the parent (as there is none)
186
     * 
187
     * marchalObject attaches all children transforming the DeviceXMLmain structure
188
     * to the root
189
     */
190
    public static function marshalObject($domElement, $node, $name, $object, $namespace = NULL, $root = false)
191
    {
192
        if (is_null($object)) {
193
            return;
194
        }
195
        if ($root) {
196
            $childNode = $node;
197
        } else {
198
            if ($object->getValue()) {
199
                $val = preg_replace('/&/', '&amp;', $object->getValue());
200
                if ($object->getType() === 'cdata') {
201
                    $childNode = $domElement->createElement($name);
202
                    $cdata = $domElement->createCDATASection(strval($val));
203
                    $childNode->appendChild($cdata);
204
                    $node->appendChild($nextChild);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nextChild seems to be never defined.
Loading history...
205
                } else {
206
                    $childNode = $domElement->createElement($name, $val);
207
                }
208
                $node->appendChild($childNode);
209
            } else {
210
                $childNode = $domElement->createElement($name);
211
                $node->appendChild($childNode);
212
            }
213
            if (!empty($namespace)) {
214
                $ns = $domElement->createAttributeNS(null,'xmlns');
215
                $ns->value = $namespace;
216
                $childNode->appendChild($ns);  
217
            }
218
        }
219
        
220
        if ($object->areAttributes()) {
221
            $attrs = $object->getAttributes();
222
            foreach ($attrs as $attrt => $attrv) {
223
                $attrE = $domElement->createAttribute($attrt);
224
                $attrE->value = $attrv;
225
                $childNode->appendChild($attrE);
226
            }
227
        }
228
229
        if (empty($object->children)) {
230
            return;
231
        }
232
        foreach ($object->children as $child) {
233
            $nameC = $child['name'];
234
            $valueC = $child['value'];
235
            $namespaceC = $child['namespace'];
236
            if (is_scalar($valueC)) {
237
                $cl = strval($valueC);
238
                $nextChild = $domElement->createElement($nameC, $cl);    
239
                $childNode->appendChild($nextChild);
240
                if (!empty($namespaceC)) {
241
                    $ns = $domElement->createAttributeNS(null,'xmlns');
242
                    $ns->value = $namespaceC;
243
                    $nextChild->appendChild($ns);  
244
                }
245
                continue;
246
            } 
247
            if (gettype($valueC) == 'array') {
248
                foreach ($valueC as $insideValue) {
249
                    if (is_object($insideValue)) {
250
                        DeviceXMLmain::marshalObject($domElement, $childNode, $nameC, $insideValue, $namespaceC);
251
                    }
252
                    if (is_scalar($insideValue)) {
253
                        if ($child['type'] === 'cdata') {
254
                            $nextChild = $domElement->createElement($nameC);
255
                            $cdata = $domElement->createCDATASection(strval($insideValue));
256
                            $nextChild->appendChild($cdata);
257
                            $childNode->appendChild($nextChild);
258
                        } else {
259
                            $nextChild = $domElement->createElement($nameC, strval($insideValue));
260
                            $childNode->appendChild($nextChild);
261
                        }
262
                        if (!empty($namespaceC)) {
263
                            $ns = $domElement->createAttributeNS(null,'xmlns');
264
                            $ns->value = $namespaceC;
265
                            $nextChild->appendChild($ns);  
266
                        }
267
                    }
268
                }
269
                continue;
270
            } 
271
            if (gettype($valueC) == 'object') {
272
                DeviceXMLmain::marshalObject($domElement, $childNode, $nameC, $valueC, $namespaceC);              
273
            }
274
        }
275
    }
276
}
277
278
279