Passed
Pull Request — master (#280)
by Tim
02:18
created

UnknownRoleDescriptor::toUnsignedXML()   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 1
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
     * This RoleDescriptor as XML
26
     *
27
     * @var \SimpleSAML\XML\Chunk
28
     * @psalm-suppress PropertyNotSetInConstructor
29
     */
30
    protected Chunk $elt;
31
32
33
    /**
34
     * Initialize an unknown RoleDescriptor.
35
     *
36
     * @param \DOMElement $xml The XML element we should load.
37
     * @return \SimpleSAML\SAML2\XML\md\UnknownRoleDescriptor
38
     *
39
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
40
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
41
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
42
     */
43
    public static function fromXML(DOMElement $xml): object
44
    {
45
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
46
47
        $validUntil = self::getAttribute($xml, 'validUntil', null);
48
        $orgs = Organization::getChildrenOfClass($xml);
49
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
50
51
        $extensions = Extensions::getChildrenOfClass($xml);
52
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
53
54
        $object = new self(
55
            preg_split('/[\s]+/', trim($protocols)),
56
            self::getAttribute($xml, 'ID', null),
57
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
58
            self::getAttribute($xml, 'cacheDuration', null),
59
            !empty($extensions) ? $extensions[0] : null,
60
            self::getAttribute($xml, 'errorURL', null),
61
            KeyDescriptor::getChildrenOfClass($xml),
62
            !empty($orgs) ? $orgs[0] : null,
63
            ContactPerson::getChildrenOfClass($xml)
64
        );
65
66
        $object->elt = new Chunk($xml);
67
        $object->setXML($xml);
68
69
        return $object;
70
    }
71
72
73
    /**
74
     * Get the original XML of this descriptor as a Chunk object.
75
     *
76
     * @return \SimpleSAML\XML\Chunk
77
     */
78
    public function getXML(): Chunk
79
    {
80
        return $this->elt;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->elt returns the type SimpleSAML\XML\Chunk which is incompatible with the return type mandated by SimpleSAML\SAML2\XML\md\...adataDocument::getXML() of DOMElement.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
81
    }
82
83
84
    /**
85
     * Convert this descriptor to an unsigned XML document.
86
     * This method does not sign the resulting XML document.
87
     *
88
     * @param \DOMElement|null $parent
89
     * @return \DOMElement The root element of the DOM tree
90
     */
91
    protected function toUnsignedXML(DOMElement $parent = null): DOMElement
92
    {
93
        return $this->elt->toXML($parent);
94
    }
95
}
96