KeyDescriptor   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
dl 0
loc 112
c 0
b 0
f 0
wmc 12
lcom 2
cbo 4
ccs 35
cts 38
cp 0.9211
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setUse() 0 10 4
A getUse() 0 4 1
A setCertificate() 0 6 1
A getCertificate() 0 4 1
A serialize() 0 12 1
A deserialize() 0 21 3
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\Metadata;
13
14
use LightSaml\Error\LightSamlXmlException;
15
use LightSaml\Model\Context\DeserializationContext;
16
use LightSaml\Model\Context\SerializationContext;
17
use LightSaml\Model\AbstractSamlModel;
18
use LightSaml\SamlConstants;
19
use LightSaml\Credential\X509Certificate;
20
21
class KeyDescriptor extends AbstractSamlModel
22
{
23
    const USE_SIGNING = 'signing';
24
    const USE_ENCRYPTION = 'encryption';
25
26
    /** @var string */
27
    protected $use;
28
29
    /** @var X509Certificate */
30
    private $certificate;
31
32
    /**
33
     * @param string|null          $use
34
     * @param X509Certificate|null $certificate
35
     */
36 44
    public function __construct($use = null, X509Certificate $certificate = null)
37
    {
38 44
        $this->use = $use;
39 44
        $this->certificate = $certificate;
40 44
    }
41
42
    /**
43
     * @param string $use
44
     *
45
     * @return KeyDescriptor
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49 33
    public function setUse($use)
50
    {
51 33
        $use = trim($use);
52 33
        if (false != $use && self::USE_ENCRYPTION != $use && self::USE_SIGNING != $use) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $use of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
53
            throw new \InvalidArgumentException(sprintf("Invalid use value '%s'", $use));
54
        }
55 33
        $this->use = $use;
56
57 33
        return $this;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 13
    public function getUse()
64
    {
65 13
        return $this->use;
66
    }
67
68
    /**
69
     * @param X509Certificate $certificate
70
     *
71
     * @return KeyDescriptor
72
     */
73 5
    public function setCertificate(X509Certificate $certificate)
74
    {
75 5
        $this->certificate = $certificate;
76
77 5
        return $this;
78
    }
79
80
    /**
81
     * @return X509Certificate
82
     */
83 14
    public function getCertificate()
84
    {
85 14
        return $this->certificate;
86
    }
87
88
    /**
89
     * @param \DOMNode             $parent
90
     * @param SerializationContext $context
91
     *
92
     * @return void
93
     */
94 3
    public function serialize(\DOMNode $parent, SerializationContext $context)
95
    {
96 3
        $result = $this->createElement('KeyDescriptor', SamlConstants::NS_METADATA, $parent, $context);
97
98 3
        $this->attributesToXml(array('use'), $result);
99
100 3
        $keyInfo = $this->createElement('ds:KeyInfo', SamlConstants::NS_XMLDSIG, $result, $context);
101 3
        $xData = $this->createElement('ds:X509Data', SamlConstants::NS_XMLDSIG, $keyInfo, $context);
102 3
        $xCert = $this->createElement('ds:X509Certificate', SamlConstants::NS_XMLDSIG, $xData, $context);
103
104 3
        $xCert->nodeValue = $this->getCertificate()->getData();
105 3
    }
106
107
    /**
108
     * @param \DOMNode               $node
109
     * @param DeserializationContext $context
110
     */
111 40
    public function deserialize(\DOMNode $node, DeserializationContext $context)
112
    {
113 40
        $this->checkXmlNodeName($node, 'KeyDescriptor', SamlConstants::NS_METADATA);
114
115 40
        $this->attributesFromXml($node, array('use'));
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
116
117 40
        $list = $context->getXpath()->query('./ds:KeyInfo/ds:X509Data/ds:X509Certificate', $node);
118 40
        if (1 != $list->length) {
119
            throw new LightSamlXmlException('Missing X509Certificate node');
120
        }
121
122
        /** @var $x509CertificateNode \DOMElement */
123 40
        $x509CertificateNode = $list->item(0);
124 40
        $certificateData = trim($x509CertificateNode->textContent);
125 40
        if (false == $certificateData) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $certificateData of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
126
            throw new LightSamlXmlException('Missing certificate data');
127
        }
128
129 40
        $this->certificate = new X509Certificate();
130 40
        $this->certificate->setData($certificateData);
131 40
    }
132
}
133