PolicyAttachment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsp;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\wsp\AppliesTo;
10
use SimpleSAML\WSSecurity\XML\wsp\Policy;
11
use SimpleSAML\WSSecurity\XML\wsp\PolicyReference;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Exception\MissingElementException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\ExtendableAttributesTrait;
16
use SimpleSAML\XML\ExtendableElementTrait;
17
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
18
use SimpleSAML\XML\XsNamespace as NS;
19
20
use function array_merge;
21
22
/**
23
 * Class representing a wsp:PolicyAttachment element.
24
 *
25
 * @package simplesamlphp/ws-security
26
 */
27
final class PolicyAttachment extends AbstractWspElement implements SchemaValidatableElementInterface
28
{
29
    use ExtendableAttributesTrait;
30
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsp\PolicyAttachment: $namespaceURI, $localName, $childNodes
Loading history...
31
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsp\PolicyAttachment: $message, $line
Loading history...
32
33
    /** The namespace-attribute for the xs:any element */
34
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
35
36
    /** The namespace-attribute for the xs:anyAttribute element */
37
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
38
39
40
    /**
41
     * Initialize a wsp:PolicyAttachment
42
     *
43
     * @param \SimpleSAML\WSSecurity\XML\wsp\AppliesTo $appliesTo
44
     * @param (\SimpleSAML\WSSecurity\XML\wsp\Policy|\SimpleSAML\WSSecurity\XML\wsp\PolicyReference)[] $policies
45
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
46
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
47
     */
48
    public function __construct(
49
        protected AppliesTo $appliesTo,
50
        protected array $policies,
51
        array $children = [],
52
        array $namespacedAttributes = [],
53
    ) {
54
        $this->setElements($children);
55
        $this->setAttributesNS($namespacedAttributes);
56
    }
57
58
59
    /**
60
     * Collect the value of the AppliesTo property.
61
     *
62
     * @return \SimpleSAML\WSSecurity\XML\wsp\AppliesTo
63
     */
64
    public function getAppliesTo(): AppliesTo
65
    {
66
        return $this->appliesTo;
67
    }
68
69
70
    /**
71
     * Collect the value of the Policies property.
72
     *
73
     * @return (\SimpleSAML\WSSecurity\XML\wsp\Policy|\SimpleSAML\WSSecurity\XML\wsp\PolicyReference)[]
74
     */
75
    public function getPolicies(): array
76
    {
77
        return $this->policies;
78
    }
79
80
81
    /*
82
     * Convert XML into an wsp:PolicyAttachment element
83
     *
84
     * @param \DOMElement $xml The XML element we should load
85
     * @return static
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The method same() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

92
        Assert::/** @scrutinizer ignore-call */ 
93
                same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Loading history...
93
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
94
95
        $appliesTo = AppliesTo::getChildrenOfClass($xml);
96
        Assert::minCount($appliesTo, 1, MissingElementException::class);
0 ignored issues
show
Bug introduced by
The method minCount() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

96
        Assert::/** @scrutinizer ignore-call */ 
97
                minCount($appliesTo, 1, MissingElementException::class);
Loading history...
97
        Assert::maxCount($appliesTo, 1, TooManyElementsException::class);
0 ignored issues
show
Bug introduced by
The method maxCount() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

97
        Assert::/** @scrutinizer ignore-call */ 
98
                maxCount($appliesTo, 1, TooManyElementsException::class);
Loading history...
98
99
        $policy = Policy::getChildrenOfClass($xml);
100
        $policyReference = PolicyReference::getChildrenOfClass($xml);
101
102
        $policies = array_merge($policy, $policyReference);
103
        Assert::minCount($policies, 1, MissingElementException::class);
104
105
        return new static(
106
            $appliesTo[0],
107
            $policies,
108
            self::getChildElementsFromXML($xml),
109
            self::getAttributesNSFromXML($xml),
110
        );
111
    }
112
113
114
    /**
115
     * Convert this wsp:PolicyAttachment to XML.
116
     *
117
     * @param \DOMElement|null $parent The element we should add this wsp:AppliesTo to.
118
     * @return \DOMElement This wsp:AppliesTo element.
119
     */
120
    public function toXML(?DOMElement $parent = null): DOMElement
121
    {
122
        $e = $this->instantiateParentElement($parent);
123
124
        $this->getAppliesTo()->toXML($e);
125
126
        foreach ($this->getPolicies() as $pol) {
127
            $pol->toXML($e);
128
        }
129
130
        foreach ($this->getAttributesNS() as $attr) {
131
            $attr->toXML($e);
132
        }
133
134
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
135
        foreach ($this->getElements() as $child) {
136
            if (!$child->isEmptyElement()) {
137
                $child->toXML($e);
138
            }
139
        }
140
141
        return $e;
142
    }
143
}
144