Passed
Push — master ( 54bedc...fe2750 )
by Tim
02:40
created

AbstractSignOutType   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 41
dl 0
loc 136
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRealm() 0 3 1
A getSignOutBasis() 0 3 1
A fromXML() 0 18 2
A __construct() 0 11 1
A toXML() 0 24 5
A getId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\WSSecurity\Constants as C;
10
use SimpleSAML\XML\Attribute as XMLAttribute;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
use SimpleSAML\XML\ExtendableAttributesTrait;
15
use SimpleSAML\XML\ExtendableElementTrait;
16
use SimpleSAML\XML\XsNamespace as NS;
17
18
/**
19
 * A SignOutType
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractSignOutType extends AbstractFedElement
24
{
25
    use ExtendableAttributesTrait;
26
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\fed\AbstractSignOutType: $namespaceURI, $localName, $childNodes
Loading history...
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
33
34
    /** The exclusions for the xs:any element */
35
    public const XS_ANY_ELT_EXCLUSIONS = [
36
        ['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id'],
37
        ['http://docs.oasis-open.org/wsfed/federation/200706', 'Realm'],
38
        ['http://docs.oasis-open.org/wsfed/federation/200706', 'SignOutBasis'],
39
    ];
40
41
42
    /**
43
     * SignOutType constructor.
44
     *
45
     * @param \SimpleSAML\WSSecurity\XML\fed\SignOutBasis $signOutBasis
46
     * @param \SimpleSAML\WSSecurity\XML\fed\Realm|null $realm
47
     * @param string|null $Id
48
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
49
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
50
     */
51
    final public function __construct(
52
        protected SignOutBasis $signOutBasis,
53
        protected ?Realm $realm = null,
54
        protected ?string $Id = null,
55
        array $children = [],
56
        array $namespacedAttributes = [],
57
    ) {
58
        Assert::nullOrValidNCName($Id);
59
60
        $this->setElements($children);
61
        $this->setAttributesNS($namespacedAttributes);
62
    }
63
64
65
    /**
66
     * Collect the value of the signOutBasis-property
67
     *
68
     * @return \SimpleSAML\WSSecurity\XML\fed\SignOutBasis
69
     */
70
    public function getSignOutBasis(): SignOutBasis
71
    {
72
        return $this->signOutBasis;
73
    }
74
75
76
    /**
77
     * Collect the value of the realm-property
78
     *
79
     * @return \SimpleSAML\WSSecurity\XML\fed\Realm|null
80
     */
81
    public function getRealm(): ?Realm
82
    {
83
        return $this->realm;
84
    }
85
86
87
    /**
88
     * Collect the value of the Id-property
89
     *
90
     * @return string|null
91
     */
92
    public function getId(): ?string
93
    {
94
        return $this->Id;
95
    }
96
97
98
    /**
99
     * Create an instance of this object from its XML representation.
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
105
     *   if the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
111
112
        $signOutBasis = SignOutBasis::getChildrenOfClass($xml);
113
        Assert::minCount($signOutBasis, 1, MissingElementException::class);
114
        Assert::maxCount($signOutBasis, 1, TooManyElementsException::class);
115
116
        $realm = Realm::getChildrenOfClass($xml);
117
        Assert::maxCount($realm, 1, TooManyElementsException::class);
118
119
        return new static(
120
            array_pop($signOutBasis),
121
            array_pop($realm),
122
            $xml->hasAttributeNS(C::NS_SEC_UTIL, 'Id') ? $xml->getAttributeNS(C::NS_SEC_UTIL, 'Id') : null,
123
            self::getChildElementsFromXML($xml),
124
            self::getAttributesNSFromXML($xml),
125
        );
126
    }
127
128
129
    /**
130
     * Add this SignOutType to an XML element.
131
     *
132
     * @param \DOMElement $parent The element we should append this username token to.
133
     * @return \DOMElement
134
     */
135
    public function toXML(DOMElement $parent = null): DOMElement
136
    {
137
        $e = parent::instantiateParentElement($parent);
138
139
        $attributes = $this->getAttributesNS();
140
        if ($this->getId() !== null) {
141
            $idAttr = new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', $this->getId());
142
            array_unshift($attributes, $idAttr);
143
        }
144
145
        foreach ($attributes as $attr) {
146
            $attr->toXML($e);
147
        }
148
149
        $this->getRealm()?->toXML($e);
150
        $this->getSignOutBasis()->toXML($e);
151
152
        foreach ($this->getElements() as $child) {
153
            if (!$child->isEmptyElement()) {
154
                $child->toXML($e);
155
            }
156
        }
157
158
        return $e;
159
    }
160
}
161