Completed
Push — master ( b463b0...ee9a04 )
by Giancarlos
02:48
created

SunatErrorHelper::setXmlErrorFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.2559
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 19/07/2017
6
 * Time: 19:46
7
 */
8
9
namespace Greenter\Helper;
10
11
/**
12
 * Class SunatErrorHelper
13
 * @package Greenter\Helper
14
 */
15
class SunatErrorHelper
16
{
17
    private $xmlErrorFile = __DIR__ . '/../Resources/CodeErrors.xml';
18
19
    /**
20
     * Get Message by Code.
21
     *
22
     * @param string $code
23
     * @return string
24
     */
25 12
    public function getMessageByCode($code)
26
    {
27
28 12
        $doc = new \DOMDocument();
29 12
        $doc->load($this->xmlErrorFile);
30 12
        $xpath = new \DOMXPath($doc);
31 12
        $nodes = $xpath->query("/errors/error[@code='$code']");
32
33 12
        if ($nodes->length !== 1) {
34 2
            return '';
35
        }
36
37 10
        return $nodes[0]->nodeValue;
38
    }
39
40
    /**
41
     * Cambia el archivo de busqueda de errores por defecto.
42
     *
43
     * @param $xmlErrorFile
44
     * @return $this
45
     * @throws \Exception
46
     */
47 2
    public function setXmlErrorFile($xmlErrorFile)
48
    {
49 2
        if (!file_exists($xmlErrorFile)) {
50 2
            throw new \Exception("Archivo de errores no existe");
51
        }
52
        $this->xmlErrorFile = $xmlErrorFile;
53
        return $this;
54
    }
55
}