AbstractSecurityTokenType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
dl 0
loc 74
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromXML() 0 12 1
A toXML() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XML\SerializableElementInterface;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
/**
17
 * Class defining the SecurityTokenType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractSecurityTokenType extends AbstractFedElement
22
{
23
    use ExtendableAttributesTrait;
24
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...stractSecurityTokenType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
27
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
34
    /**
35
     * AbstractSecurityTokenType constructor
36
     *
37
     * @param \SimpleSAML\XML\SerializableElementInterface $child
38
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39
     */
40
    final public function __construct(
41
        protected SerializableElementinterface $child,
42
        array $namespacedAttributes = [],
43
    ) {
44
        $this->setElements([$child]);
45
        $this->setAttributesNS($namespacedAttributes);
46
    }
47
48
49
    /**
50
     * Create an instance of this object from its XML representation.
51
     *
52
     * @param \DOMElement $xml
53
     * @return static
54
     *
55
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
56
     *   if the qualified name of the supplied element is wrong
57
     */
58
    public static function fromXML(DOMElement $xml): static
59
    {
60
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
61
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
62
63
        $children = self::getChildElementsFromXML($xml);
64
        Assert::minCount($children, 1, SchemaViolationException::class);
65
        Assert::maxCount($children, 1, SchemaViolationException::class);
66
67
        return new static(
68
            array_pop($children),
69
            self::getAttributesNSFromXML($xml),
70
        );
71
    }
72
73
74
    /**
75
     * Add this AbstractSecurityTokenType to an XML element.
76
     *
77
     * @param \DOMElement|null $parent The element we should append this username token to.
78
     * @return \DOMElement
79
     */
80
    public function toXML(?DOMElement $parent = null): DOMElement
81
    {
82
        $e = parent::instantiateParentElement($parent);
83
84
        foreach ($this->getAttributesNS() as $attr) {
85
            $attr->toXML($e);
86
        }
87
88
        foreach ($this->getElements() as $child) {
89
            if (!$child->isEmptyElement()) {
90
                $child->toXML($e);
91
            }
92
        }
93
94
        return $e;
95
    }
96
}
97