Passed
Push — master ( a470ba...be54de )
by Tim
02:20
created

UnknownRoleDescriptor::getXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\AbstractXMLElement;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\Utils as XMLUtils;
14
15
use function preg_split;
16
17
/**
18
 * Class representing unknown RoleDescriptors.
19
 *
20
 * @package simplesamlphp/saml2
21
 */
22
final class UnknownRoleDescriptor extends AbstractRoleDescriptor
23
{
24
    /**
25
     * Initialize an unknown RoleDescriptor.
26
     *
27
     * @param \DOMElement $xml The XML element we should load.
28
     * @return \SimpleSAML\SAML2\XML\md\UnknownRoleDescriptor
29
     *
30
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
31
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
32
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
33
     */
34
    public static function fromXML(DOMElement $xml): object
35
    {
36
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
37
38
        $validUntil = self::getAttribute($xml, 'validUntil', null);
39
        $orgs = Organization::getChildrenOfClass($xml);
40
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
41
42
        $extensions = Extensions::getChildrenOfClass($xml);
43
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
44
45
        $object = new self(
46
            preg_split('/[\s]+/', trim($protocols)),
47
            self::getAttribute($xml, 'ID', null),
48
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
49
            self::getAttribute($xml, 'cacheDuration', null),
50
            !empty($extensions) ? $extensions[0] : null,
51
            self::getAttribute($xml, 'errorURL', null),
52
            KeyDescriptor::getChildrenOfClass($xml),
53
            !empty($orgs) ? $orgs[0] : null,
54
            ContactPerson::getChildrenOfClass($xml)
55
        );
56
57
        $object->setXML($xml);
58
59
        return $object;
60
    }
61
62
63
    /**
64
     * Add this RoleDescriptor to an EntityDescriptor.
65
     *
66
     * @param \DOMElement|null $parent The EntityDescriptor we should append this RoleDescriptor to.
67
     * @return \DOMElement
68
     */
69
    public function toXML(DOMElement $parent = null): DOMElement
70
    {
71
        $chunk = new Chunk($this->xml);
72
        return $chunk->toXML($parent);
73
    }
74
}
75