Passed
Pull Request — master (#2)
by Tim
02:17
created

SignableElementTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A toSignedXML() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
use DOMElement;
8
use DOMNode;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\XML\Utils as XMLUtils;
11
use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils;
12
use SimpleSAML\XMLSecurity\XML\ds\Signature;
13
use SimpleSAML\XMLSecurity\XMLSecurityKey;
14
15
/**
16
 * Helper trait for processing signed elements.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
trait SignableElementTrait
21
{
22
    /**
23
     * Sign the given XML element.
24
     *
25
     * @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key used for signing.
26
     * @param array $certificates  Any public key to be added to the ds:Signature
27
     * @param \DOMNode|null $insertBefore  A specific node in the DOM structure where the ds:Signature should be put in front.
28
     * @return \DOMElement The signed element.
29
     * @throws \Exception If an error occurs while trying to sign.
30
     */
31
    private function toSignedXML(XMLSecurityKey $signingKey, array $certificates, DOMNode $insertBefore = null): DOMElement
32
    {
33
        $root = $this->toXML();
0 ignored issues
show
Bug introduced by
It seems like toXML() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $root = $this->toXML();
Loading history...
34
35
        if ($insertBefore !== null) {
36
            XMLSecurityUtils::insertSignature($signingKey, $certificates, $root, $insertBefore);
37
        } else {
38
            $signature = new Signature($signingKey->getAlgorithm(), $certificates, $signingKey);
39
            $signature->toXML($root);
40
        }
41
42
        return $root;
43
    }
44
}
45