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

SunatErrorHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 41
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessageByCode() 0 14 2
A setXmlErrorFile() 0 8 2
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
}