Completed
Push — master ( 4f6af7...d55336 )
by Thijs
14s queued 10s
created

KeyDescriptor::toXML()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace SAML2\XML\md;
4
5
use SAML2\Constants;
6
use SAML2\Utils;
7
use SAML2\XML\Chunk;
8
use SAML2\XML\ds\KeyInfo;
9
10
/**
11
 * Class representing a KeyDescriptor element.
12
 *
13
 * @package SimpleSAMLphp
14
 */
15
class KeyDescriptor
16
{
17
    /**
18
     * What this key can be used for.
19
     *
20
     * 'encryption', 'signing' or null.
21
     *
22
     * @var string|null
23
     */
24
    public $use;
25
26
    /**
27
     * The KeyInfo for this key.
28
     *
29
     * @var \SAML2\XML\ds\KeyInfo
30
     */
31
    public $KeyInfo;
32
33
    /**
34
     * Supported EncryptionMethods.
35
     *
36
     * Array of \SAML2\XML\Chunk objects.
37
     *
38
     * @var \SAML2\XML\Chunk[]
39
     */
40
    public $EncryptionMethod = array();
41
42
    /**
43
     * Initialize an KeyDescriptor.
44
     *
45
     * @param \DOMElement|null $xml The XML element we should load.
46
     * @throws \Exception
47
     */
48
    public function __construct(\DOMElement $xml = null)
49
    {
50
        if ($xml === null) {
51
            return;
52
        }
53
54
        if ($xml->hasAttribute('use')) {
55
            $this->use = $xml->getAttribute('use');
56
        }
57
58
        $keyInfo = Utils::xpQuery($xml, './ds:KeyInfo');
59
        if (count($keyInfo) > 1) {
60
            throw new \Exception('More than one ds:KeyInfo in the KeyDescriptor.');
61
        } elseif (empty($keyInfo)) {
62
            throw new \Exception('No ds:KeyInfo in the KeyDescriptor.');
63
        }
64
        $this->KeyInfo = new KeyInfo($keyInfo[0]);
65
66
        foreach (Utils::xpQuery($xml, './saml_metadata:EncryptionMethod') as $em) {
67
            $this->EncryptionMethod[] = new Chunk($em);
68
        }
69
    }
70
71
    /**
72
     * Convert this KeyDescriptor to XML.
73
     *
74
     * @param \DOMElement $parent The element we should append this KeyDescriptor to.
75
     * @return \DOMElement
76
     */
77
    public function toXML(\DOMElement $parent)
78
    {
79
        assert(is_null($this->use) || is_string($this->use));
80
        assert($this->KeyInfo instanceof KeyInfo);
81
        assert(is_array($this->EncryptionMethod));
82
83
        $doc = $parent->ownerDocument;
84
85
        $e = $doc->createElementNS(Constants::NS_MD, 'md:KeyDescriptor');
86
        $parent->appendChild($e);
87
88
        if (isset($this->use)) {
89
            $e->setAttribute('use', $this->use);
90
        }
91
92
        $this->KeyInfo->toXML($e);
93
94
        foreach ($this->EncryptionMethod as $em) {
95
            $em->toXML($e);
96
        }
97
98
        return $e;
99
    }
100
}
101