Completed
Push — master ( 2d40c2...8bc0f0 )
by Giancarlos
04:17
created

XmlErrorReader::getMessageByCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 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 34
    public function getMessageByCode($code)
25
    {
26 34
        $doc = new \DOMDocument();
27 34
        $doc->load($this->xmlErrorFile);
28 34
        $xpath = new \DOMXPath($doc);
29 34
        $nodes = $xpath->query("/errors/error[@code='$code']");
30
31 34
        if ($nodes->length !== 1) {
32 4
            return '';
33
        }
34
35 30
        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
}