XmlLoadException::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Fazland\SkebbyRestClient\Exception;
4
5
/**
6
 * Represents an exception thrown on XML load.
7
 *
8
 * @author Massimiliano Braglia <[email protected]>
9
 */
10
class XmlLoadException extends Exception
11
{
12
    /**
13
     * @var string
14
     */
15
    private $response;
16
17
    /**
18
     * XmlLoadException constructor.
19
     *
20
     * @param string $response
21
     * @param array  $errors
22
     */
23
    public function __construct(string $response, array $errors)
24
    {
25
        $this->response = $response;
26
        $this->message = '';
27
28
        foreach ($errors as $error) {
29
            $this->message .= $this->decodeXmlError($error, $response);
30
        }
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 View Code Duplication
    public function __toString(): string
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...
37
    {
38
        return '['.get_class($this).'] '.$this->message."\n".
39
            'Response: '."\n".
40
            $this->response
41
        ;
42
    }
43
44
    /**
45
     * @param \LibXMLError $error
46
     * @param string       $xml
47
     *
48
     * @return string
49
     */
50
    private function decodeXmlError(\LibXMLError $error, string $xml): string
51
    {
52
        $return = $xml[$error->line - 1]."\n";
53
54
        switch ($error->level) {
55
            case LIBXML_ERR_WARNING:
56
                $return .= "Warning $error->code: ";
57
                break;
58
59
            case LIBXML_ERR_ERROR:
60
                $return .= "Error $error->code: ";
61
                break;
62
63
            case LIBXML_ERR_FATAL:
64
                $return .= "Fatal Error $error->code: ";
65
                break;
66
        }
67
68
        $return .= trim($error->message).
69
            "\n  Line: $error->line".
70
            "\n  Column: $error->column";
71
72
        if ($error->file) {
73
            $return .= "\n  File: $error->file";
74
        }
75
76
        return $return."\n\n";
77
    }
78
}
79