Passed
Push — master ( 55ada1...4e08ca )
by Giancarlos
02:39
created

XmlUtils::extractSignFromDoc()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3.072
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Administrador
5
 * Date: 04/10/2017
6
 * Time: 04:41 PM
7
 */
8
9
namespace Greenter\Report;
10
11
/**
12
 * Class XmlUtils
13
 * @package Greenter\Report
14
 */
15
final class XmlUtils
16
{
17
    /**
18
     * @param string $xml
19
     * @return string
20
     */
21 4
    public static function extractSign($xml)
22
    {
23 4
        $doc = new \DOMDocument();
24 4
        $doc->loadXML($xml);
25
26 4
        return self::extractSignFromDoc($doc);
27
    }
28
29
    /**
30
     * @param \DOMDocument $document
31
     * @return string
32
     */
33 4
    public static function extractSignFromDoc(\DOMDocument $document)
34
    {
35 4
        $xpt = new \DOMXPath($document);
36
37 4
        $exts = $xpt->query('ext:UBLExtensions/ext:UBLExtension', $document->documentElement);
38 4
        if ($exts->length == 0) {
39
            return '';
40
        }
41 4
        $nodeSign = $exts->item($exts->length - 1);
42
43 4
        $hash = $xpt->query('ext:ExtensionContent/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue', $nodeSign);
44
45 4
        if ($hash->length == 0) {
46
            return '';
47
        }
48
49 4
        return $hash->item(0)->nodeValue;
50
    }
51
}