Completed
Push — master ( a0d808...22db96 )
by Giancarlos
03:50
created

XmlErrorReader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessageByCode() 0 13 2
A setXmlErrorFile() 0 8 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 22/07/2017
6
 * Time: 16:09
7
 */
8
9
namespace Greenter\Ws\Reader;
10
11
/**
12
 * Class XmlCodeErrorReader
13
 * @package Greenter\Ws\Reader
14
 */
15
class XmlErrorReader implements ErrorReader
16
{
17
    private $xmlErrorFile = __DIR__ . '/../../Resources/CodeErrors.xml';
18
19
    /**
20
     * Get Error Message by code.
21
     * @param string $code
22
     * @return string
23
     */
24 36
    public function getMessageByCode($code)
25
    {
26 36
        $doc = new \DOMDocument();
27 36
        $doc->load($this->xmlErrorFile);
28 36
        $xpath = new \DOMXPath($doc);
29 36
        $nodes = $xpath->query("/errors/error[@code='$code']");
30
31 36
        if ($nodes->length !== 1) {
32 4
            return '';
33
        }
34
35 32
        return $nodes[0]->nodeValue;
36
    }
37
38
    /**
39
     * Cambia el archivo de busqueda de errores por defecto.
40
     *
41
     * @param $xmlErrorFile
42
     * @return $this
43
     * @throws \Exception
44
     */
45 4
    public function setXmlErrorFile($xmlErrorFile)
46
    {
47 4
        if (!file_exists($xmlErrorFile)) {
48 2
            throw new \Exception("Archivo de errores no existe");
49
        }
50 2
        $this->xmlErrorFile = $xmlErrorFile;
51 2
        return $this;
52
    }
53
}