Completed
Push — master ( 4f6af7...d55336 )
by Thijs
14s queued 10s
created

RoleDescriptor   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 189
Duplicated Lines 2.65 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 5
loc 189
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 5 47 11
D toXML() 0 49 13

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
namespace SAML2\XML\md;
4
5
use SAML2\Constants;
6
use SAML2\SignedElementHelper;
7
use SAML2\Utils;
8
9
/**
10
 * Class representing SAML 2 RoleDescriptor element.
11
 *
12
 * @package SimpleSAMLphp
13
 */
14
class RoleDescriptor extends SignedElementHelper
15
{
16
    /**
17
     * The name of this descriptor element.
18
     *
19
     * @var string
20
     */
21
    private $elementName;
22
23
    /**
24
     * The ID of this element.
25
     *
26
     * @var string|null
27
     */
28
    public $ID;
29
30
    /**
31
     * How long this element is valid, as a unix timestamp.
32
     *
33
     * @var int|null
34
     */
35
    public $validUntil;
36
37
    /**
38
     * The length of time this element can be cached, as string.
39
     *
40
     * @var string|null
41
     */
42
    public $cacheDuration;
43
44
    /**
45
     * List of supported protocols.
46
     *
47
     * @var array
48
     */
49
    public $protocolSupportEnumeration = array();
50
51
    /**
52
     * Error URL for this role.
53
     *
54
     * @var string|null
55
     */
56
    public $errorURL;
57
58
    /**
59
     * Extensions on this element.
60
     *
61
     * Array of extension elements.
62
     *
63
     * @var array
64
     */
65
    public $Extensions = array();
66
67
    /**
68
     * KeyDescriptor elements.
69
     *
70
     * Array of \SAML2\XML\md\KeyDescriptor elements.
71
     *
72
     * @var \SAML2\XML\md\KeyDescriptor[]
73
     */
74
    public $KeyDescriptor = array();
75
76
    /**
77
     * Organization of this role.
78
     *
79
     * @var \SAML2\XML\md\Organization|null
80
     */
81
    public $Organization = null;
82
83
    /**
84
     * ContactPerson elements for this role.
85
     *
86
     * Array of \SAML2\XML\md\ContactPerson objects.
87
     *
88
     * @var \SAML2\XML\md\ContactPerson[]
89
     */
90
    public $ContactPerson = array();
91
92
    /**
93
     * Initialize a RoleDescriptor.
94
     *
95
     * @param string          $elementName The name of this element.
96
     * @param \DOMElement|null $xml         The XML element we should load.
97
     * @throws \Exception
98
     */
99
    protected function __construct($elementName, \DOMElement $xml = null)
100
    {
101
        assert(is_string($elementName));
102
103
        parent::__construct($xml);
104
        $this->elementName = $elementName;
105
106
        if ($xml === null) {
107
            return;
108
        }
109
110
        if ($xml->hasAttribute('ID')) {
111
            $this->ID = $xml->getAttribute('ID');
112
        }
113
        if ($xml->hasAttribute('validUntil')) {
114
            $this->validUntil = Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'));
115
        }
116
        if ($xml->hasAttribute('cacheDuration')) {
117
            $this->cacheDuration = $xml->getAttribute('cacheDuration');
118
        }
119
120
        if (!$xml->hasAttribute('protocolSupportEnumeration')) {
121
            throw new \Exception('Missing protocolSupportEnumeration attribute on ' . $xml->localName);
122
        }
123
        $this->protocolSupportEnumeration = preg_split('/[\s]+/', $xml->getAttribute('protocolSupportEnumeration'));
124
125
        if ($xml->hasAttribute('errorURL')) {
126
            $this->errorURL = $xml->getAttribute('errorURL');
127
        }
128
129
        $this->Extensions = Extensions::getList($xml);
130
131
        foreach (Utils::xpQuery($xml, './saml_metadata:KeyDescriptor') as $kd) {
132
            $this->KeyDescriptor[] = new KeyDescriptor($kd);
133
        }
134
135
        $organization = Utils::xpQuery($xml, './saml_metadata:Organization');
136 View Code Duplication
        if (count($organization) > 1) {
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...
137
            throw new \Exception('More than one Organization in the entity.');
138
        } elseif (!empty($organization)) {
139
            $this->Organization = new Organization($organization[0]);
140
        }
141
142
        foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
143
            $this->contactPersons[] = new ContactPerson($cp);
0 ignored issues
show
Bug introduced by
The property contactPersons does not seem to exist. Did you mean ContactPerson?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
144
        }
145
    }
146
147
    /**
148
     * Add this RoleDescriptor to an EntityDescriptor.
149
     *
150
     * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to.
151
     * @return \DOMElement
152
     */
153
    protected function toXML(\DOMElement $parent)
154
    {
155
        assert(is_null($this->ID) || is_string($this->ID));
156
        assert(is_null($this->validUntil) || is_int($this->validUntil));
157
        assert(is_null($this->cacheDuration) || is_string($this->cacheDuration));
158
        assert(is_array($this->protocolSupportEnumeration));
159
        assert(is_null($this->errorURL) || is_string($this->errorURL));
160
        assert(is_array($this->Extensions));
161
        assert(is_array($this->KeyDescriptor));
162
        assert(is_null($this->Organization) || $this->Organization instanceof Organization);
163
        assert(is_array($this->ContactPerson));
164
165
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, $this->elementName);
166
        $parent->appendChild($e);
167
168
        if (isset($this->ID)) {
169
            $e->setAttribute('ID', $this->ID);
170
        }
171
172
        if (isset($this->validUntil)) {
173
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
174
        }
175
176
        if (isset($this->cacheDuration)) {
177
            $e->setAttribute('cacheDuration', $this->cacheDuration);
178
        }
179
180
        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));
181
182
        if (isset($this->errorURL)) {
183
            $e->setAttribute('errorURL', $this->errorURL);
184
        }
185
186
        Extensions::addList($e, $this->Extensions);
187
188
        foreach ($this->KeyDescriptor as $kd) {
189
            $kd->toXML($e);
190
        }
191
192
        if (isset($this->Organization)) {
193
            $this->Organization->toXML($e);
194
        }
195
196
        foreach ($this->ContactPerson as $cp) {
197
            $cp->toXML($e);
198
        }
199
200
        return $e;
201
    }
202
}
203