Passed
Push — master ( eca6f3...bde58b )
by Fabien
04:26
created

LibXmlDisplayErrors::displayErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Utility;
13
14
class LibXmlDisplayErrors
15
{
16
    private $errors;
17
    private $chainErrors;
18
    private $error;
19
    private $content;
20
21 2
    public function __construct()
22
    {
23 2
        $this->errors      = libxml_get_errors();
24 2
        $this->chainErrors = '';
25 2
    }
26
27 2
    public function displayErrors() {
28 2
        foreach ($this->errors as $error) {
29 2
            $this->error        = $error;
30 2
            $this->chainErrors .= $this->displayError();
31 2
        }
32 2
        libxml_clear_errors();
33
34 2
        return $this->chainErrors;
35
    }
36
37 2
    private function displayError() {
38 2
        $this->setEmptyContent();
39 2
        $this->addErrorLevelContent();
40 2
        $this->addFileContent();
41 2
        $this->addLineAndColumnContent();
42 2
        $this->addErrorMessageContent();
43
44 2
        return preg_replace('/( in\ \/(.*))/', '', strip_tags($this->getContent()))."\n";
45
    }
46
47 2
    private function addErrorLevelContent()
48
    {
49 2
        switch ($this->error->level) {
50 2
            case LIBXML_ERR_WARNING:
51
                $this->addInContent('Warning '.($this->error->code));
52
                break;
53 2
            case LIBXML_ERR_ERROR:
54 2
                $this->addInContent('Error '.($this->error->code));
55 2
                break;
56 1
            case LIBXML_ERR_FATAL:
57 1
                $this->addInContent('Fatal Error '.($this->error->code));
58 1
                break;
59
            default:
60
                $this->addInContent('Info '.($this->error->code));
61 2
        }
62 2
    }
63
64 2
    private function addFileContent()
65
    {
66 2
        if ($this->error->file) {
67 2
            $this->addInContent(' in '.($this->error->file));
68 2
        }
69 2
    }
70
71 2
    private function addLineAndColumnContent()
72
    {
73 2
        $this->addInContent(' on line '.($this->error->line).' column '.($this->error->column).":\n");
74 2
    }
75
76 2
    private function addErrorMessageContent()
77
    {
78 2
        $this->addInContent(trim($this->error->message)."\n");
79 2
    }
80
81
    /**
82
     * @param string $content
83
     */
84 2
    private function addInContent($content)
85
    {
86 2
        $this->content .= $content;
87 2
    }
88
89 2
    private function setEmptyContent()
90
    {
91 2
        $this->content = '';
92 2
    }
93
94 2
    private function getContent()
95
    {
96 2
        return $this->content;
97
    }
98
}
99