LibXmlDisplayErrors   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 83
ccs 45
cts 48
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addFileContent() 0 4 2
A addInContent() 0 3 1
A addLineAndColumnContent() 0 3 1
A getContent() 0 3 1
A addErrorMessageContent() 0 3 1
A setEmptyContent() 0 3 1
A displayError() 0 8 1
A addErrorLevelContent() 0 14 4
A displayErrors() 0 8 2
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
        }
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
        }
62 2
    }
63
64 2
    private function addFileContent()
65
    {
66 2
        if ($this->error->file) {
67 2
            $this->addInContent(' in '.($this->error->file));
68
        }
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