AbstractBinarySecurityTokenType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 4
b 0
f 0
dl 0
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 9 2
A __construct() 0 9 1
A getValueType() 0 3 1
A fromXML() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsse;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\Constants as C;
10
use SimpleSAML\WSSecurity\XML\wsu\Type\IDValue;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSchema\Type\StringValue;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
/**
17
 * Class defining the BinarySecurityTokenType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractBinarySecurityTokenType extends AbstractEncodedString
22
{
23
    /** The namespace-attribute for the xs:anyAttribute element */
24
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
25
26
27
    /**
28
     * AbstractBinarySecurityTokenType constructor
29
     *
30
     * @param \SimpleSAML\XMLSchema\Type\StringValue $content
31
     * @param \SimpleSAML\WSSecurity\XML\wsu\Type\IDValue|null $Id
32
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
33
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $valueType
34
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $EncodingType
35
     */
36
    final public function __construct(
37
        StringValue $content,
38
        ?IDValue $Id = null,
39
        array $namespacedAttributes = [],
40
        protected ?AnyURIValue $valueType = null,
41
        ?AnyURIValue $EncodingType = null,
42
    ) {
43
        Assert::validBase64Binary($content->getValue());
44
        parent::__construct($content, $Id, $namespacedAttributes, $EncodingType);
45
    }
46
47
48
    /**
49
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
50
     */
51
    public function getValueType(): ?AnyURIValue
52
    {
53
        return $this->valueType;
54
    }
55
56
57
    /**
58
     * Create an instance of this object from its XML representation.
59
     *
60
     * @param \DOMElement $xml
61
     * @return static
62
     *
63
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
64
     *   if the qualified name of the supplied element is wrong
65
     */
66
    public static function fromXML(DOMElement $xml): static
67
    {
68
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
69
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
70
71
        $nsAttributes = self::getAttributesNSFromXML($xml);
72
73
        $Id = null;
74
        foreach ($nsAttributes as $i => $attr) {
75
            if ($attr->getNamespaceURI() === C::NS_SEC_UTIL && $attr->getAttrName() === 'Id') {
76
                $Id = IDValue::fromString($attr->getAttrValue()->getValue());
77
                unset($nsAttributes[$i]);
78
                break;
79
            }
80
        }
81
82
        return new static(
83
            StringValue::fromString($xml->textContent),
84
            $Id,
85
            $nsAttributes,
86
            self::getOptionalAttribute($xml, 'ValueType', AnyURIValue::class, null),
87
            self::getOptionalAttribute($xml, 'EncodingType', AnyURIValue::class, null),
88
        );
89
    }
90
91
92
    /**
93
     * Add this username token to an XML element.
94
     *
95
     * @param \DOMElement $parent The element we should append this username token to.
96
     * @return \DOMElement
97
     */
98
    public function toXML(?DOMElement $parent = null): DOMElement
99
    {
100
        $e = parent::toXML($parent);
101
102
        if ($this->getValueType() !== null) {
103
            $e->setAttribute('ValueType', $this->getValueType()->getValue());
104
        }
105
106
        return $e;
107
    }
108
}
109