Completed
Push — master ( 6ebd4b...5a00d9 )
by Thijs
03:05
created

KeyDescriptor::toXML()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 1
dl 23
loc 23
rs 9.0856
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 View Code Duplication
    public function toXML(\DOMElement $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        assert('is_null($this->use) || is_string($this->use)');
80
        assert('$this->KeyInfo instanceof \SAML2\XML\ds\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