Completed
Push — master ( 6ebd4b...5a00d9 )
by Thijs
03:05
created

AffiliationDescriptor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 36 8
B toXML() 0 40 5
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 AffiliationDescriptor element.
11
 *
12
 * @package SimpleSAMLphp
13
 */
14
class AffiliationDescriptor extends SignedElementHelper
15
{
16
    /**
17
     * The affiliationOwnerID.
18
     *
19
     * @var string
20
     */
21
    public $affiliationOwnerID;
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
     * Extensions on this element.
46
     *
47
     * Array of extension elements.
48
     *
49
     * @var array
50
     */
51
    public $Extensions = array();
52
53
    /**
54
     * The AffiliateMember(s).
55
     *
56
     * Array of entity ID strings.
57
     *
58
     * @var array
59
     */
60
    public $AffiliateMember = array();
61
62
    /**
63
     * KeyDescriptor elements.
64
     *
65
     * Array of \SAML2\XML\md\KeyDescriptor elements.
66
     *
67
     * @var \SAML2\XML\md\KeyDescriptor[]
68
     */
69
    public $KeyDescriptor = array();
70
71
    /**
72
     * Initialize a AffiliationDescriptor.
73
     *
74
     * @param \DOMElement|null $xml The XML element we should load.
75
     * @throws \Exception
76
     */
77
    public function __construct(\DOMElement $xml = null)
78
    {
79
        parent::__construct($xml);
80
81
        if ($xml === null) {
82
            return;
83
        }
84
85
        if (!$xml->hasAttribute('affiliationOwnerID')) {
86
            throw new \Exception('Missing affiliationOwnerID on AffiliationDescriptor.');
87
        }
88
        $this->affiliationOwnerID = $xml->getAttribute('affiliationOwnerID');
89
90
        if ($xml->hasAttribute('ID')) {
91
            $this->ID = $xml->getAttribute('ID');
92
        }
93
94
        if ($xml->hasAttribute('validUntil')) {
95
            $this->validUntil = Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'));
96
        }
97
98
        if ($xml->hasAttribute('cacheDuration')) {
99
            $this->cacheDuration = $xml->getAttribute('cacheDuration');
100
        }
101
102
        $this->Extensions = Extensions::getList($xml);
103
104
        $this->AffiliateMember = Utils::extractStrings($xml, Constants::NS_MD, 'AffiliateMember');
105
        if (empty($this->AffiliateMember)) {
106
            throw new \Exception('Missing AffiliateMember in AffiliationDescriptor.');
107
        }
108
109
        foreach (Utils::xpQuery($xml, './saml_metadata:KeyDescriptor') as $kd) {
110
            $this->KeyDescriptor[] = new KeyDescriptor($kd);
111
        }
112
    }
113
114
    /**
115
     * Add this AffiliationDescriptor to an EntityDescriptor.
116
     *
117
     * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to.
118
     * @return \DOMElement
119
     */
120
    public function toXML(\DOMElement $parent)
121
    {
122
        assert('is_string($this->affiliationOwnerID)');
123
        assert('is_null($this->ID) || is_string($this->ID)');
124
        assert('is_null($this->validUntil) || is_int($this->validUntil)');
125
        assert('is_null($this->cacheDuration) || is_string($this->cacheDuration)');
126
        assert('is_array($this->Extensions)');
127
        assert('is_array($this->AffiliateMember)');
128
        assert('!empty($this->AffiliateMember)');
129
        assert('is_array($this->KeyDescriptor)');
130
131
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:AffiliationDescriptor');
132
        $parent->appendChild($e);
133
134
        $e->setAttribute('affiliationOwnerID', $this->affiliationOwnerID);
135
136
        if (isset($this->ID)) {
137
            $e->setAttribute('ID', $this->ID);
138
        }
139
140
        if (isset($this->validUntil)) {
141
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
142
        }
143
144
        if (isset($this->cacheDuration)) {
145
            $e->setAttribute('cacheDuration', $this->cacheDuration);
146
        }
147
148
        Extensions::addList($e, $this->Extensions);
149
150
        Utils::addStrings($e, Constants::NS_MD, 'md:AffiliateMember', false, $this->AffiliateMember);
151
152
        foreach ($this->KeyDescriptor as $kd) {
153
            $kd->toXML($e);
154
        }
155
156
        $this->signElement($e, $e->firstChild);
0 ignored issues
show
Documentation introduced by
$e->firstChild is of type object<DOMNode>, but the function expects a null|object<DOMElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
158
        return $e;
159
    }
160
}
161