ErrorBuilder::getHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class for building errors in HTML format
4
 *
5
 * @file      ErrorBuilder.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
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 3
	protected function convertTypeToString(&$type)
56
	{
57 3
		$type = (isset($this->types[$type]))
58 3
			? $this->types[$type]
59 3
			: "UNKNOWN ERROR TYPE: $type";
60
	}
61
62
	/**
63
	 * Calls backtrace formatting
64
	 *
65
	 * @param array $backtrace Calls array
66
	 */
67 3
	protected function formatBacktrace(array &$backtrace)
68
	{
69 3
		foreach ($backtrace as &$call) {
70 3
			$call['function'] = (isset($call['class']))
71 3
				? $call['class'] . $call['type'] . $call['function']
72 2
				: $call['function'];
73
74 3
			$call['file'] = (isset($call['file']))
75 3
				? $call['file'] . '<b>:</b>' . $call['line']
76 1
				: '';
77
		}
78
	}
79
80
	/**
81
	 * @return BaseErrorHandler
82
	 */
83 4
	public function getHandler()
84
	{
85 4
		return $this->handler;
86
	}
87
88
	/**
89
	 * @param BaseErrorHandler $handler
90
	 */
91 4
	public function setHandler(BaseErrorHandler $handler)
92
	{
93 4
		$this->handler = $handler;
94
	}
95
96
	/**
97
	 * @return string
98
	 */
99 4
	public function getTemplate()
100
	{
101 4
		return $this->template;
102
	}
103
104
	/**
105
	 * @param string $template
106
	 */
107 4
	public function setTemplate($template)
108
	{
109 4
		$this->template = $template;
110
	}
111
112
	/**
113
	 * Build error in HTML format
114
	 *
115
	 * @return string
116
	 * @throws \Exception
117
	 */
118 3
	public function getHtml()
119
	{
120 3
		$vars = $this->getHandler()->getVars();
121 3
		$this->formatBacktrace($vars['stack']);
122 3
		$this->convertTypeToString($vars['type']);
123 3
		View::set($vars);
124
125 3
		return View::get($this->getTemplate());
126
	}
127
}
128