Completed
Push — master ( 234de9...b59dba )
by Massimiliano
02:25
created

XmlLoadException::decodeXmlError()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 0
cts 22
cp 0
rs 8.439
cc 5
eloc 18
nc 8
nop 2
crap 30
1
<?php declare(strict_types=1);
2
3
namespace Fazland\SkebbyRestClient\Exception;
4
5
class XmlLoadException extends Exception
6
{
7
    /**
8
     * @var string
9
     */
10
    private $response;
11
12
    public function __construct($response, array $errors)
13
    {
14
        $this->response = $response;
15
        $this->message = '';
16
17
        foreach ($errors as $error) {
18
            $this->message .= $this->decodeXmlError($error, $response);
19
        }
20
    }
21
22 View Code Duplication
    public function __toString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        return '[' . get_class($this) . '] ' . $this->message . "\n" .
25
            'Response: ' . "\n" .
26
            $this->response
27
        ;
28
    }
29
30
    private function decodeXmlError(\LibXMLError $error, $xml)
31
    {
32
        $return = $xml[$error->line - 1] . "\n";
33
34
        switch ($error->level) {
35
            case LIBXML_ERR_WARNING:
36
                $return .= "Warning $error->code: ";
37
                break;
38
39
            case LIBXML_ERR_ERROR:
40
                $return .= "Error $error->code: ";
41
                break;
42
43
            case LIBXML_ERR_FATAL:
44
                $return .= "Fatal Error $error->code: ";
45
                break;
46
        }
47
48
        $return .= trim($error->message) .
49
            "\n  Line: $error->line" .
50
            "\n  Column: $error->column";
51
52
        if ($error->file) {
53
            $return .= "\n  File: $error->file";
54
        }
55
56
        return $return . "\n\n";
57
    }
58
}
59