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

KeyDescriptor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 26.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 23
loc 86
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 6
A toXML() 23 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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