Completed
Push — master ( 32cb15...ce22ce )
by Alexander
05:39 queued 02:43
created

ErrorBuilder::formatBacktrace()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 5
nop 1
crap 4
1
<?php
2
/**
3
 * Class for building errors in HTML format
4
 *
5
 * @file      ErrorBuilder.php
6
 *
7
 * PHP version 5.4+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info>
11
 * @date      2015-06-06 20:24
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\ErrorHandler\HtmlBuilders;
17
18
use Veles\ErrorHandler\BaseErrorHandler;
19
use Veles\View\View;
20
21
/**
22
 * Class ErrorBuilder
23
 * @author  Yancharuk Alexander <alex at itvault dot info>
24
 */
25
class ErrorBuilder
26
{
27
	/** @var  BaseErrorHandler */
28
	protected $handler;
29
	/** @var  string */
30
	protected $template;
31
	/** @var array  */
32
	protected $types = [
33
		E_ERROR             => 'FATAL ERROR',               // 1
34
		E_WARNING           => 'WARNING',                   // 2
35
		E_PARSE             => 'PARSE ERROR',               // 4
36
		E_NOTICE            => 'NOTICE',                    // 8
37
		E_CORE_ERROR        => 'CORE ERROR',                // 16
38
		E_CORE_WARNING      => 'CORE WARNING',              // 32
39
		E_CORE_ERROR        => 'COMPILE ERROR',             // 64
40
		E_CORE_WARNING      => 'COMPILE WARNING',           // 128
41
		E_USER_ERROR        => 'USER ERROR',                // 256
42
		E_USER_WARNING      => 'USER WARNING',              // 512
43
		E_USER_NOTICE       => 'USER NOTICE',               // 1024
44
		E_STRICT            => 'STRICT NOTICE',             // 2048
45
		E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',         // 4096
46
		E_DEPRECATED        => 'DEPRECATED WARNING',        // 8192
47
		E_USER_DEPRECATED   => 'USER DEPRECATED WARNING',   // 16384
48
		0                   => 'EXCEPTION'
49
	];
50
51
	/**
52
	 * Get error type
53
	 * @param string $type
54
	 */
55 1
	protected function convertTypeToString(&$type)
56
	{
57 1
		$type = (isset($this->types[$type]))
58 1
			? $this->types[$type]
59 1
			: "UNKNOWN ERROR TYPE: $type";
60 1
	}
61
62
	/**
63
	 * Calls backtrace formatting
64
	 *
65
	 * @param array $backtrace Calls array
66
	 */
67 1
	protected function formatBacktrace(array &$backtrace)
68
	{
69 1
		foreach ($backtrace as &$call) {
70 1
			$call['function'] = (isset($call['class']))
71 1
				? $call['class'] . $call['type'] . $call['function']
72 1
				: $call['function'];
73
74 1
			$call['file'] = (isset($call['file']))
75 1
				? $call['file'] . '<b>:</b>' . $call['line']
76 1
				: '';
77 1
		}
78 1
	}
79
80
	/**
81
	 * @return BaseErrorHandler
82
	 */
83 1
	public function getHandler()
84
	{
85 1
		return $this->handler;
86
	}
87
88
	/**
89
	 * @param BaseErrorHandler $handler
90
	 */
91 1
	public function setHandler(BaseErrorHandler $handler)
92
	{
93 1
		$this->handler = $handler;
94 1
	}
95
96
	/**
97
	 * @return string
98
	 */
99 1
	public function getTemplate()
100
	{
101 1
		return $this->template;
102
	}
103
104
	/**
105
	 * @param string $template
106
	 */
107 1
	public function setTemplate($template)
108
	{
109 1
		$this->template = $template;
110 1
	}
111
112
	/**
113
	 * @return string
114
	 */
115 1
	public function getHtml()
116
	{
117 1
		$vars = $this->getHandler()->getVars();
118 1
		$this->formatBacktrace($vars['stack']);
119 1
		$this->convertTypeToString($vars['type']);
120 1
		View::set($vars);
121
122 1
		return View::get($this->getTemplate());
123
	}
124
}
125