Completed
Push — master ( c962a4...aa471d )
by Giancarlos
03:16
created

XmlUtils::getXpath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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
 */
14
final class XmlUtils
15
{
16
    const EXT_NAMESPACE = 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2';
17
    const DS_NAMESPACE = 'http://www.w3.org/2000/09/xmldsig#';
18
19
    /**
20
     * @param string $xml
21
     *
22
     * @return string
23
     */
24 8
    public function getHashSign($xml)
25
    {
26 8
        $doc = new \DOMDocument();
27 8
        @$doc->loadXML($xml);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
28
29 8
        return $this->getHashSignFromDoc($doc);
30
    }
31
32
    /**
33
     * @param string $filename
34
     *
35
     * @return string
36
     */
37 8
    public function getHashSignFromFile($filename)
38
    {
39 8
        $doc = new \DOMDocument();
40 8
        @$doc->load($filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
41
42 8
        return $this->getHashSignFromDoc($doc);
43
    }
44
45
    /**
46
     * @param \DOMDocument $document
47
     *
48
     * @return string
49
     */
50 22
    public function getHashSignFromDoc(\DOMDocument $document)
51
    {
52 22
        $xpt = $this->getXpath($document);
53
54 22
        $exts = $xpt->query('ext:UBLExtensions/ext:UBLExtension', $document->documentElement);
55 22
        if ($exts->length == 0) {
56 2
            return '';
57
        }
58
59 20
        return $this->getHash($exts, $xpt);
60
    }
61
62
    /**
63
     * @param \DOMDocument $document
64
     *
65
     * @return \DOMXPath
66
     */
67 22
    public function getXpath(\DOMDocument $document)
68
    {
69 22
        $xpt = new \DOMXPath($document);
70 22
        $xpt->registerNamespace('ext', self::EXT_NAMESPACE);
71 22
        $xpt->registerNamespace('ds', self::DS_NAMESPACE);
72
73 22
        return $xpt;
74
    }
75
76
    /**
77
     * @param \DOMNodeList $exts
78
     * @param \DOMXPath $xpt
79
     * @return string
80
     */
81 20
    public function getHash(\DOMNodeList $exts, \DOMXPath $xpt)
82
    {
83 20
        for ($i = $exts->length; $i-- > 0;) {
84 20
            $nodeSign = $exts->item($i);
85 20
            $hash = $xpt->query('ext:ExtensionContent/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue', $nodeSign);
86
87 20
            if ($hash->length == 0) {
88 8
                continue;
89
            }
90
91 18
            return $hash->item(0)->nodeValue;
92
        }
93
94 2
        return '';
95
    }
96
}
97