AbstractPGPDataType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A toXML() 0 12 2
A getPGPKeyPacket() 0 3 1
A getPGPKeyID() 0 3 1
A fromXML() 0 15 1
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\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XML\XsNamespace as NS;
16
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
17
18
use function array_pop;
19
20
/**
21
 * Abstract class representing the PGPDataType.
22
 *
23
 * @package simplesamlphp/xml-security
24
 */
25
abstract class AbstractPGPDataType extends AbstractDsElement implements SchemaValidatableElementInterface
26
{
27
    use ExtendableElementTrait;
28
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\AbstractPGPDataType: $message, $line
Loading history...
29
30
    /** @var \SimpleSAML\XML\XsNamespace */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
34
    /**
35
     * Initialize a PGPData element.
36
     *
37
     * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId
38
     * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket
39
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
40
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException
41
     */
42
    final public function __construct(
43
        protected ?PGPKeyID $pgpKeyId = null,
44
        protected ?PGPKeyPacket $pgpKeyPacket = null,
45
        array $children = [],
46
    ) {
47
        if ($pgpKeyId === null && $pgpKeyPacket === null) {
48
            throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null.");
49
        }
50
51
        $this->setElements($children);
52
    }
53
54
55
    /**
56
     * Collect the value of the PGPKeyID-property
57
     *
58
     * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null
59
     */
60
    public function getPGPKeyID(): ?PGPKeyID
61
    {
62
        return $this->pgpKeyId;
63
    }
64
65
66
    /**
67
     * Collect the value of the PGPKeyPacket-property
68
     *
69
     * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null
70
     */
71
    public function getPGPKeyPacket(): ?PGPKeyPacket
72
    {
73
        return $this->pgpKeyPacket;
74
    }
75
76
77
    /**
78
     * Convert XML into a PGPData
79
     *
80
     * @param \DOMElement $xml The XML element we should load
81
     * @return static
82
     *
83
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
84
     *   If the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): static
87
    {
88
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
89
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
90
91
        $pgpKeyId = PGPKeyID::getChildrenOfClass($xml);
92
        Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class);
93
94
        $pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml);
95
        Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class);
96
97
        return new static(
98
            array_pop($pgpKeyId),
99
            array_pop($pgpKeyPacket),
100
            self::getChildElementsFromXML($xml),
101
        );
102
    }
103
104
105
    /**
106
     * Convert this PGPData to XML.
107
     *
108
     * @param \DOMElement|null $parent The element we should append this PGPData to.
109
     * @return \DOMElement
110
     */
111
    public function toXML(?DOMElement $parent = null): DOMElement
112
    {
113
        $e = $this->instantiateParentElement($parent);
114
115
        $this->getPGPKeyId()?->toXML($e);
116
        $this->getPGPKeyPacket()?->toXML($e);
117
118
        foreach ($this->getElements() as $elt) {
119
            $elt->toXML($e);
120
        }
121
122
        return $e;
123
    }
124
}
125