AbstractIssuedTokenType::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
rs 9.2222
cc 6
nc 16
nop 1
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\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\XsNamespace as NS;
15
use ValueError;
16
17
use function array_pop;
18
use function is_string;
19
use function sprintf;
20
21
/**
22
 * Class representing WS security policy IssuedTokenType.
23
 *
24
 * @package simplesamlphp/ws-security
25
 */
26
abstract class AbstractIssuedTokenType extends AbstractSpElement
27
{
28
    use ExtendableAttributesTrait;
29
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractIssuedTokenType: $namespaceURI, $localName, $childNodes
Loading history...
30
    use IncludeTokenTypeTrait;
31
32
    /** The namespace-attribute for the xs:any element */
33
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
34
35
    /** The namespace-attribute for the xs:anyAttribute element */
36
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
37
38
    /** The exclusions for the xs:anyAttribute element */
39
    public const XS_ANY_ATTR_EXCLUSIONS = [
40
        [null, 'IncludeToken'],
41
    ];
42
43
44
    /**
45
     * IssuedTokenType constructor.
46
     *
47
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\RequestSecurityTokenTemplate $requestSecurityTokenTemplate
48
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null $issuer
49
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\IncludeToken|string|null $includeToken
50
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $elts
51
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\sp_200507\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
     */
53
    final public function __construct(
54
        protected RequestSecurityTokenTemplate $requestSecurityTokenTemplate,
55
        protected ?Issuer $issuer = null,
56
        IncludeToken|string|null $includeToken = null,
57
        array $elts = [],
58
        array $namespacedAttributes = [],
59
    ) {
60
        $this->setIncludeToken($includeToken);
61
        $this->setElements($elts);
62
        $this->setAttributesNS($namespacedAttributes);
63
    }
64
65
66
    /**
67
     * Collect the value of the Issuer property.
68
     *
69
     * @return \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null
70
     */
71
    public function getIssuer(): ?Issuer
72
    {
73
        return $this->issuer;
74
    }
75
76
77
    /**
78
     * Collect the value of the RequestSecurityTokenTemplate property.
79
     *
80
     * @return \SimpleSAML\WSSecurity\XML\sp_200507\RequestSecurityTokenTemplate
81
     */
82
    public function getRequestSecurityTokenTemplate(): RequestSecurityTokenTemplate
83
    {
84
        return $this->requestSecurityTokenTemplate;
85
    }
86
87
88
    /**
89
     * Initialize an IssuedTokenType.
90
     *
91
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
92
     *
93
     * @param \DOMElement $xml The XML element we should load.
94
     * @return static
95
     *
96
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
97
     *   if the qualified name of the supplied element is wrong
98
     */
99
    public static function fromXML(DOMElement $xml): static
100
    {
101
        $qualifiedName = static::getClassName(static::class);
102
        Assert::eq(
103
            $xml->localName,
104
            $qualifiedName,
105
            sprintf('Unexpected name for IssuedTokenType: %s. Expected: %s.', $xml->localName, $qualifiedName),
106
            InvalidDOMElementException::class,
107
        );
108
109
        $issuer = Issuer::getChildrenOfClass($xml);
110
111
        $requestSecurityTokenTemplate = RequestSecurityTokenTemplate::getChildrenOfClass($xml);
112
        Assert::minCount($requestSecurityTokenTemplate, 1, MissingElementException::class);
113
        Assert::maxCount($requestSecurityTokenTemplate, 1, TooManyElementsException::class);
114
115
        $includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
116
        try {
117
            $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

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