Passed
Pull Request — master (#57)
by Tim
02:07
created

AbstractKeyInfoType::getInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\SchemaViolationException;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XML\SerializableElementInterface;
12
use SimpleSAML\XML\XsNamespace as NS;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
16
17
/**
18
 * Abstract class representing the KeyInfoType.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
abstract class AbstractKeyInfoType extends AbstractDsElement
23
{
24
    use ExtendableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\AbstractKeyInfoType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
    /** @var \SimpleSAML\XML\XsNamespace */
27
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28
29
30
    /**
31
     * Initialize a KeyInfo element.
32
     *
33
     * @param (
34
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyName|
35
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
36
     *     \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
37
     *     \SimpleSAML\XMLSecurity\XML\ds\X509Data|
38
     *     \SimpleSAML\XML\SerializableElementInterface
39
     * )[] $info
40
     * @param string|null $Id
41
     */
42
    final public function __construct(
43
        protected array $info,
44
        protected ?string $Id = null,
45
    ) {
46
        Assert::notEmpty(
47
            $info,
48
            sprintf(
49
                '%s:%s cannot be empty',
50
                static::getNamespacePrefix(),
51
                static::getLocalName(),
52
            ),
53
            InvalidArgumentException::class,
54
        );
55
        Assert::maxCount($info, C::UNBOUNDED_LIMIT);
56
        Assert::allIsInstanceOf(
57
            $info,
58
            SerializableElementInterface::class,
59
            InvalidArgumentException::class,
60
        );
61
        Assert::nullOrValidNCName($Id);
62
63
        foreach ($info as $item) {
64
            if ($item instanceof AbstractDsElement) {
65
                Assert::isInstanceOfAny(
66
                    $item,
67
                    [KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
68
                    SchemaViolationException::class,
69
                );
70
            }
71
        }
72
    }
73
74
75
    /**
76
     * Collect the value of the Id-property
77
     *
78
     * @return string|null
79
     */
80
    public function getId(): ?string
81
    {
82
        return $this->Id;
83
    }
84
85
86
    /**
87
     * Collect the value of the info-property
88
     *
89
     * @return list<\SimpleSAML\XML\SerializableElementInterface>
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\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...
90
     */
91
    public function getInfo(): array
92
    {
93
        return $this->info;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->info returns the type array which is incompatible with the documented return type SimpleSAML\XMLSecurity\XML\ds\list.
Loading history...
94
    }
95
96
97
    /**
98
     * Convert this KeyInfo to XML.
99
     *
100
     * @param \DOMElement|null $parent The element we should append this KeyInfo to.
101
     * @return \DOMElement
102
     */
103
    public function toXML(DOMElement $parent = null): DOMElement
104
    {
105
        $e = $this->instantiateParentElement($parent);
106
107
        if ($this->getId() !== null) {
108
            $e->setAttribute('Id', $this->getId());
109
        }
110
111
        foreach ($this->getInfo() as $elt) {
112
            $elt->toXML($e);
113
        }
114
115
        return $e;
116
    }
117
}
118