Passed
Push — master ( 2aeba7...f0a64c )
by Tomasz
06:45 queued 14s
created

DeviceXMLmain::marshalObject()   C

Complexity

Conditions 14
Paths 61

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 46
rs 6.2666
cc 14
nc 61
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     *  @var array $AuthMethodElements is used to limit
73
     *  XML elements present within ServerSideCredentials and
74
     *  ClientSideCredentials to ones which are relevant
75
     *  for a given EAP method.
76
     *  @var array of XLM element names which are allowed
77
     *  EAP method names are defined in core/EAP.php
78
     */
79
    public static $authMethodElements = [
80
        'server' => [
81
            \core\common\EAP::TLS => ['CA', 'ServerID'],
82
            \core\common\EAP::FAST => ['CA', 'ServerID'],
83
            \core\common\EAP::PEAP => ['CA', 'ServerID'],
84
            \core\common\EAP::TTLS => ['CA', 'ServerID'],
85
            \core\common\EAP::PWD => ['ServerID'],
86
        ],
87
        'client' => [
88
            \core\common\EAP::TLS => ['UserName', 'Password', 'ClientCertificate'],
89
            \core\common\EAP::MSCHAP2 => ['UserName', 'Password', 'OuterIdentity', 'InnerIdentitySuffix', 'InnerIdentityHint'],
90
            \core\common\EAP::GTC => ['UserName', 'OneTimeToken'],
91
            \core\common\EAP::NE_PAP => ['UserName', 'Password', 'OuterIdentity', 'InnerIdentitySuffix', 'InnerIdentityHint'],
92
            \core\common\EAP::NE_SILVERBULLET => ['UserName', 'ClientCertificate', 'OuterIdentity'],
93
        ]
94
    ];
95
96
    /**
97
     * constructor, initialises empty set of attributes and value
98
     */
99
    public function __construct()
100
    {
101
        $this->attributes = [];
102
        $this->value = '';
103
        $this->children = [];
104
    }
105
    
106
    /**
107
     * sets a list of attributes in the current object instance
108
     * 
109
     * @param array $attributes the list of attributes
110
     * @return void
111
     */
112
    public function setAttributes($attributes)
113
    {
114
        $this->attributes = $attributes;
115
    }
116
117
    /**
118
     * retrieves list of attributes from object instance
119
     * @return array
120
     */
121
    public function getAttributes()
122
    {
123
        return $this->attributes;
124
    }
125
126
    /**
127
     * Used to set scalar values of basic XML elements
128
     * 
129
     * @param scalar $value
130
     * @return void
131
     */
132
    public function setValue($value)
133
    {
134
        if (is_scalar($value)) {
135
            $this->value = strval($value);
136
        } else {
137
            throw new Exception("unexpected value type passed" . gettype($value));
138
        }
139
    }
140
141
    /**
142
     * retrieves value of a basic XML object instance
143
     * 
144
     * @return string
145
     */
146
    public function getValue()
147
    {
148
        return $this->value;
149
    }
150
151
    /**
152
     * does this object have attributes?
153
     * 
154
     * @return boolean
155
     */
156
    public function areAttributes()
157
    {
158
        return empty($this->attributes) ? false : true;
159
    }
160
161
    /**
162
     * adds an attribute with the given value to the set of attributes
163
     * @param string $attribute attribute to set
164
     * @param mixed  $value     value to set
165
     * @return void
166
     */
167
    public function setAttribute($attribute, $value)
168
    {
169
        if (!isset($this->attributes)) {
170
            $this->attributes = [];
171
        }
172
        $this->attributes[$attribute] = $value;
173
    }
174
175
    /**
176
     * adds a child to the current object instance
177
     * 
178
     * @param string $name the child element name to set
179
     * @param mixed  $value    value to set
180
     * @return void
181
     */
182
    public function setChild($name, $value, $namespace = NULL)
183
    {
184
        $this->children[] = ['name' => $name, 'value' => $value, 'namespace' => $namespace];
185
    }
186
187
    /**
188
     * This method is used to generate 
189
     * 
190
     * @var $node an SimpleXMLElement object that will serve as the document root
191
     * 
192
     * marchalObject attaches all children transforming the DeviceXMLmain structure
193
     * to the root
194
     */
195
    public function marshalObject($node, $name, $object, $namespace=NULL, $root=false)
196
    {
197
        if (is_null($object)) {
198
            return;
199
        }
200
        if ($root) {
201
            $childNode = $node;
202
        } else {
203
            if ($object->getValue()) {
204
                $val = preg_replace('/&/', '&amp;', $object->getValue());
205
                $childNode = $node->addChild($name, $val, $namespace);
206
            } else {
207
                $childNode = $node->addChild($name, '', $namespace);
208
            }
209
        }
210
        if ($object->areAttributes()) {
211
            $attrs = $object->getAttributes();
212
            foreach ($attrs as $attrt => $attrv) {
213
                $childNode->addAttribute($attrt, $attrv);
214
            }
215
        }
216
//        $fields = $object->children;
217
        if (empty($object->children)) {
218
            return;
219
        }
220
        foreach ($object->children as $child) {
221
            $nameC = $child['name'];
222
            $value = $child['value'];
223
            $namespace = $child['namespace'];
224
            if (is_scalar($value)) {
225
                $childNode->addChild($nameC, strval($value), $namespace);
226
                continue;
227
            } 
228
            if (gettype($value) == 'array') {
229
                foreach ($value as $insideValue) {
230
                    if (is_object($insideValue)) {
231
                        DeviceXMLmain::marshalObject($childNode, $nameC, $insideValue, $namespace);
0 ignored issues
show
Bug Best Practice introduced by
The method core\DeviceXMLmain::marshalObject() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
                        DeviceXMLmain::/** @scrutinizer ignore-call */ 
232
                                       marshalObject($childNode, $nameC, $insideValue, $namespace);
Loading history...
232
                    }
233
                    if (is_scalar($insideValue)) {
234
                        $childNode->addChild($nameC, strval($insideValue), $namespace);
235
                    }
236
                }
237
                continue;
238
            } 
239
            if (gettype($value) == 'object') {
240
                DeviceXMLmain::marshalObject($childNode, $nameC, $value, $namespace);
241
            }
242
        }
243
    }
244
}
245
246
247