isEmptyElement()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200507;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\XsNamespace as NS;
13
use ValueError;
14
15
use function array_pop;
16
use function is_string;
17
use function sprintf;
18
19
/**
20
 * Class representing WS security policy SecureConversationTokenType.
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractSecureConversationTokenType extends AbstractSpElement
25
{
26
    use ExtendableAttributesTrait;
27
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...reConversationTokenType: $namespaceURI, $localName, $childNodes
Loading history...
28
    use IncludeTokenTypeTrait;
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
35
36
    /** The exclusions for the xs:anyAttribute element */
37
    public const XS_ANY_ATTR_EXCLUSIONS = [
38
        [null, 'IncludeToken'],
39
    ];
40
41
42
    /**
43
     * SecureConversationTokenType constructor.
44
     *
45
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null $issuer
46
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\IncludeToken|string|null $includeToken
47
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     */
50
    final public function __construct(
51
        protected ?Issuer $issuer,
52
        IncludeToken|string|null $includeToken = null,
53
        array $elts = [],
54
        array $namespacedAttributes = [],
55
    ) {
56
        $this->setIncludeToken($includeToken);
57
        $this->setElements($elts);
58
        $this->setAttributesNS($namespacedAttributes);
59
    }
60
61
62
    /**
63
     * Collect the value of the Issuer property.
64
     *
65
     * @return \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null
66
     */
67
    public function getIssuer(): ?Issuer
68
    {
69
        return $this->issuer;
70
    }
71
72
73
    /**
74
     * Test if an object, at the state it's in, would produce an empty XML-element
75
     *
76
     * @return bool
77
     */
78
    public function isEmptyElement(): bool
79
    {
80
        return empty($this->getIssuer())
81
            && empty($this->getIncludeToken())
82
            && empty($this->getAttributesNS())
83
            && empty($this->getElements());
84
    }
85
86
87
    /**
88
     * Initialize an SecureConversationTokenType.
89
     *
90
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
91
     *
92
     * @param \DOMElement $xml The XML element we should load.
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
96
     *   if the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        $qualifiedName = static::getClassName(static::class);
101
        Assert::eq(
102
            $xml->localName,
103
            $qualifiedName,
104
            sprintf('Unexpected name for IssuedTokenType: %s. Expected: %s.', $xml->localName, $qualifiedName),
105
            InvalidDOMElementException::class,
106
        );
107
108
        $issuer = Issuer::getChildrenOfClass($xml);
109
110
        $includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
111
        try {
112
            $includeToken = IncludeToken::from($includeToken);
0 ignored issues
show
Bug introduced by
It seems like $includeToken can also be of type null; however, parameter $value of SimpleSAML\WSSecurity\XM...07\IncludeToken::from() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $includeToken = IncludeToken::from(/** @scrutinizer ignore-type */ $includeToken);
Loading history...
113
        } catch (ValueError) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
114
        }
115
116
        return new static(
117
            array_pop($issuer),
118
            $includeToken,
119
            self::getChildElementsFromXML($xml),
120
            self::getAttributesNSFromXML($xml),
121
        );
122
    }
123
124
125
    /**
126
     * Convert this element to XML.
127
     *
128
     * @param \DOMElement|null $parent The element we should append this element to.
129
     * @return \DOMElement
130
     */
131
    public function toXML(?DOMElement $parent = null): DOMElement
132
    {
133
        $e = $this->instantiateParentElement($parent);
134
135
        if ($this->getIncludeToken() !== null) {
136
            $e->setAttribute(
137
                'IncludeToken',
138
                is_string($this->getIncludeToken()) ? $this->getIncludeToken() : $this->getIncludeToken()->value,
139
            );
140
        }
141
142
        if ($this->getIssuer() !== null) {
143
            $this->getIssuer()->toXML($e);
144
        }
145
146
        foreach ($this->getElements() as $elt) {
147
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
148
            $elt->toXML($e);
149
        }
150
151
        foreach ($this->getAttributesNS() as $attr) {
152
            $attr->toXML($e);
153
        }
154
155
        return $e;
156
    }
157
}
158