Passed
Push — master ( 10cdca...5a50a6 )
by Tomasz
03:15
created

marshalObject()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 27
nc 32
nop 2

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
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
5
 * and GN4-2 consortia
6
 *
7
 * License: see the web/copyright.php file in the file structure
8
 *******************************************************************************
9
 */
10
11
/**
12
 * This file contains class definitions and procedures for 
13
 * generation of a generic XML description of a 802.1x configurator
14
 *
15
 * @author Maja Górecka-Wolniewicz <[email protected]>
16
 *
17
 * @package ModuleWriting
18
 */
19
namespace devices\xml;
20
use Exception;
21
22
/**
23
 * base class extended by every element
24
 */
25
class XMLElement {
26
27
    private $attributes;
28
    /**
29
     * The value of the element.
30
     * @var string
31
     */
32
    private $value;
33
34
    protected function getObjectVars($obj) {
35
        return get_object_vars($obj);
36
    }
37
38
    /**
39
     *  @var array $AuthMethodElements is used to limit
40
     *  XML elements present within ServerSideCredentials and
41
     *  ClientSideCredentials to ones which are relevant
42
     *  for a given EAP method.
43
     *  @var array of XLM element names which are allowed
44
     *  EAP method names are defined in core/EAP.php
45
     */
46
    public static $authMethodElements = [
47
        'server' => [
48
            \core\common\EAP::TLS => ['CA', 'ServerID'],
49
            \core\common\EAP::FAST => ['CA', 'ServerID'],
50
            \core\common\EAP::PEAP => ['CA', 'ServerID'],
51
            \core\common\EAP::TTLS => ['CA', 'ServerID'],
52
            \core\common\EAP::PWD => ['ServerID'],
53
        ],
54
        'client' => [
55
            \core\common\EAP::TLS => ['UserName', 'Password', 'ClientCertificate'],
56
            \core\common\EAP::MSCHAP2 => ['UserName', 'Password', 'OuterIdentity'],
57
            \core\common\EAP::GTC => ['UserName', 'OneTimeToken'],
58
            \core\common\EAP::NE_PAP => ['UserName', 'Password', 'OuterIdentity'],
59
            \core\common\EAP::NE_SILVERBULLET => ['UserName', 'ClientCertificate'],
60
        ]
61
    ];
62
63
    public function __construct() {
64
        $this->attributes = [];
65
        $this->value = '';
66
    }
67
68
    public function setAttributes($attributes) {
69
        $this->attributes = $attributes;
70
    }
71
72
    public function getAttributes() {
73
        return $this->attributes;
74
    }
75
76
    /**
77
     * 
78
     * @param scalar $value
79
     * @return void
80
     */
81
    public function setValue($value) {
82
        if (is_scalar($value)) {
83
            $this->value = strval($value);
84
        } else {
85
            throw new Exception("unexpected value type passed" . gettype($value));
86
        }
87
    }
88
89
    /**
90
     * 
91
     * @return string
92
     */
93
    public function getValue() {
94
        return $this->value;
95
    }
96
97
    public function areAttributes() {
98
        return empty($this->attributes) ? 0 : 1;
99
    }
100
101
    /**
102
     * adds an attribute with the given value to the set of attributes
103
     * @param string $attribute
104
     * @param mixed $value
105
     */
106
    public function setAttribute($attribute, $value) {
107
        if (!isset($this->attributes)) {
108
            $this->attributes = [];
109
        }
110
        $this->attributes[$attribute] = $value;
111
    }
112
113
    /**
114
     * 
115
     * @param string $property
116
     * @param mixed $value
117
     */
118
    public function setProperty($property, $value) {
119
        $this->$property = $value;
120
    }
121
122
    public function getAll() {
123
        $elems = get_object_vars($this);
124
        $objvars = [];
125
        foreach ($elems as $key => $val) {
126
            if (($key != 'attributes') && ($key != 'value')) {
127
                $objvars[$key] = $val;
128
            }
129
        }
130
        return $objvars;
131
    }
132
133
}
134
135
class EAPIdentityProvider extends XMLElement {
136
137
    protected $ValidUntil;
138
    protected $AuthenticationMethods;
139
    protected $ProviderInfo;
140
    protected $VendorSpecific;
141
142
}
143
144
class AuthenticationMethods extends XMLElement {
145
146
    protected $AuthenticationMethod;
147
148
}
149
150
class AuthenticationMethod extends XMLElement {
151
152
    protected $EAPMethod;
153
    protected $ServerSideCredential;
154
    protected $ClientSideCredential;
155
    protected $InnerAuthenticationMethod;
156
157
}
158
159
class EAPMethod extends XMLElement {
160
161
    protected $Type;
162
    protected $TypeSpecific;
163
    protected $VendorSpecific;
164
165
}
166
167
class NonEAPAuthMethod extends XMLElement {
168
169
    protected $Type;
170
    protected $TypeSpecific;
171
    protected $VendorSpecific;
172
173
}
174
175
class Type extends XMLElement {
176
    
177
}
178
179
class TypeSpecific extends XMLElement {
180
    
181
}
182
183
class VendorSpecific extends XMLElement {
184
    
185
}
186
187
class ServerSideCredential extends XMLElement {
188
189
    protected $CA; // multi
190
    protected $ServerID; //multi
191
    protected $EAPType;
192
193
    public function getAll() {
194
        if (isset(XMLElement::$authMethodElements['server'][$this->EAPType]) && XMLElement::$authMethodElements['server'][$this->EAPType]) {
195
            $element = XMLElement::$authMethodElements['server'][$this->EAPType];
196
            $objectVariables = get_object_vars($this);
197
            $outArray = [];
198
            foreach ($objectVariables as $o => $v) {
199
                if (in_array($o, $element)) {
200
                    $outArray[$o] = $v;
201
                }
202
            }
203
            return($outArray);
204
        }
205
    }
206
207
}
208
209
class ServerID extends XMLElement {
210
    
211
}
212
213
class ClientSideCredential extends XMLElement {
214
215
    protected $OuterIdentity;
216
    protected $UserName;
217
    protected $Password;
218
    protected $ClientCertificate;
219
    protected $Passphrase;
220
    protected $PAC;
221
    protected $ProvisionPAC;
222
    protected $EAPType;
223
224
225
    public function getAll() {
226
        if (isset(XMLElement::$authMethodElements['client'][$this->EAPType]) && XMLElement::$authMethodElements['client'][$this->EAPType]) {
227
            $element = XMLElement::$authMethodElements['client'][$this->EAPType];
228
            $objectVars = get_object_vars($this);
229
            $outputArray = [];
230
            foreach ($objectVars as $name => $value) {
231
                if (in_array($name, $element)) {
232
                    $outputArray[$name] = $value;
233
                }
234
            }
235
            return($outputArray);
236
        }
237
    }
238
239
}
240
241
class ClientCertificate extends XMLElement {
242
    
243
}
244
245
class CA extends XMLElement {
246
    
247
}
248
249
class InnerAuthenticationMethod extends XMLElement {
250
251
    protected $EAPMethod;
252
    protected $NonEAPAuthMethod;
253
    protected $ServerSideCredential;
254
    protected $ClientSideCredential;
255
256
}
257
258
class ProviderInfo extends XMLElement {
259
260
    protected $DisplayName;
261
    protected $Description;
262
    protected $ProviderLocation;
263
    protected $ProviderLogo;
264
    protected $TermsOfUse;
265
    protected $Helpdesk;
266
267
}
268
269
class DisplayName extends XMLElement {
270
    
271
}
272
273
class Description extends XMLElement {
274
    
275
}
276
277
class ProviderLocation extends XMLElement {
278
279
    protected $Longitude;
280
    protected $Latitude;
281
282
}
283
284
class ProviderLogo extends XMLElement {
285
    
286
}
287
288
class TermsOfUse extends XMLElement {
289
    
290
}
291
292
class Helpdesk extends XMLElement {
293
294
    protected $EmailAddress;
295
    protected $WebAddress;
296
    protected $Phone;
297
298
}
299
300
class EmailAddress extends XMLElement {
301
    
302
}
303
304
class WebAddress extends XMLElement {
305
    
306
}
307
308
class Phone extends XMLElement {
309
    
310
}
311
312
/*
313
  class CompatibleUses  extends XMLElement {
314
  protected $IEEE80211;
315
  protected $IEEE8023;
316
  protected $ABFAB;
317
  }
318
  class IEEE80211 extends XMLElement {
319
  protected $SSID;
320
  protected $ConsortiumOID;
321
  protected $MinRSNProto;
322
  }
323
324
  class IEEE8023 extends XMLElement {
325
  protected $NetworkID;
326
  }
327
  class ABFAB extends XMLElement {
328
  protected $ServiceIdentifier;
329
  }
330
331
 */
332
333
/**
334
 * 
335
 * @param \SimpleXMLElement $key
336
 * @param \SimpleXMLElement $value
337
 */
338
function SimpleXMLElement_append($key, $value) {
339
    if (trim((string) $value) == '') {
340
        $element = $key->addChild($value->getName());
341
        foreach ($value->attributes() as $attKey => $attValue) {
342
            $element->addAttribute($attKey, $attValue);
343
        }
344
        foreach ($value->children() as $child) {
345
            SimpleXMLElement_append($element, $child);
346
        }
347
    } else {
348
        $element = $key->addChild($value->getName(), trim((string) $value));
0 ignored issues
show
Unused Code introduced by
The assignment to $element is dead and can be removed.
Loading history...
349
    }
350
}
351
352
/**
353
 * 
354
 * @param \SimpleXMLElement $node
355
 * @param EAPIdentityProvider $object
356
 * @return void
357
 */
358
function marshalObject($node, $object) {
359
    $qualClassName = get_class($object);
360
    // remove namespace qualifier
361
    $pos = strrpos($qualClassName, '\\');
362
    $className =  substr($qualClassName, $pos + 1);
363
    $name = preg_replace("/_/", "-", $className);
364
    if ($object->getValue()) {
365
        $val = preg_replace('/&/', '&amp;', $object->getValue());
366
        $childNode = $node->addChild($name, $val);
367
    } else {
368
        $childNode = $node->addChild($name);
369
    }
370
    if ($object->areAttributes()) {
371
        $attrs = $object->getAttributes();
372
        foreach ($attrs as $attrt => $attrv) {
373
            $childNode->addAttribute($attrt, $attrv);
374
        }
375
    }
376
    $fields = $object->getAll();
377
    if (empty($fields)) {
378
        return;
379
    }
380
    foreach ($fields as $name => $value) {
381
        if (is_scalar($value)) {
382
            $childNode->addChild($name, strval($value));
383
            continue;
384
        } 
385
        if (gettype($value) == 'array') {
386
            foreach ($value as $insideValue) {
387
                if (is_object($insideValue)) {
388
                    marshalObject($childNode, $insideValue);
389
                }
390
            }
391
            continue;
392
        } 
393
        if (gettype($value) == 'object') {
394
            marshalObject($childNode, $value);
395
        }
396
    }
397
}
398