Test Failed
Push — develop ( acdfb2...2de2bf )
by nguereza
02:53
created

HtmlErrorRenderer::renderException()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 37
rs 8.9457
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file HtmlErrorRenderer.php
34
 *
35
 *  The HTML error renderer class used to render the errors
36
 * for HTML content type
37
 *
38
 *  @package    Platine\Framework\Handler\Error\Renderer
39
 *  @author Platine Developers team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   http://www.iacademy.cf
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Framework\Handler\Error\Renderer;
50
51
use Platine\Stdlib\Helper\Php;
52
use Throwable;
53
54
/**
55
 * class HtmlErrorRenderer
56
 * @package Platine\Framework\Handler\Error\Renderer
57
 */
58
class HtmlErrorRenderer extends AbstractErrorRenderer
59
{
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function render(Throwable $exception, bool $detail, bool $isLog = false): string
65
    {
66
        if ($isLog) {
67
            return sprintf(
68
                '%s: %s, %s',
69
                $this->getErrorTitle($exception),
70
                $this->getErrorDescription($exception),
71
                Php::exceptionToString($exception, '', true)
72
            );
73
        }
74
75
        if ($detail) {
76
            $html = '<p>The application could not run because of the following error:</p>';
77
            $html .= '<h2>Details</h2>';
78
            $html .= $this->renderException($exception);
79
        } else {
80
            $html = sprintf('<p>%s</p>', $this->getErrorDescription($exception));
81
        }
82
83
        return $this->renderBody($this->getErrorTitle($exception), $html);
84
    }
85
86
    /**
87
     * Render exception data
88
     * @param Throwable $exception
89
     * @return string
90
     */
91
    protected function renderException(Throwable $exception): string
92
    {
93
        $html = sprintf(
94
            '<div><strong>Type:</strong> %s</div>',
95
            get_class($exception)
96
        );
97
98
        $code = $exception->getCode();
99
        if ($code !== null) {
100
            $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
101
        }
102
103
        $message = $exception->getMessage();
104
        if ($message !== null) {
105
            $html .= sprintf(
106
                '<div><strong>Message:</strong> %s</div>',
107
                htmlentities($message)
108
            );
109
        }
110
111
        $file = $exception->getFile();
112
        if ($file !== null) {
113
            $html .= sprintf('<div><strong>File:</strong> %s</div>', $file);
114
        }
115
116
        $line = $exception->getLine();
117
        if ($line !== null) {
118
            $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line);
119
        }
120
121
        $trace = $exception->getTraceAsString();
122
        if ($trace !== null) {
123
            $html .= '<h2>Trace</h2>';
124
            $html .= sprintf('<pre>%s</pre>', htmlentities($trace));
125
        }
126
127
        return $html;
128
    }
129
130
    /**
131
     * Render HTML body content
132
     * @param string $title
133
     * @param string $content
134
     * @return string
135
     */
136
    protected function renderBody(string $title = '', string $content = ''): string
137
    {
138
        return sprintf(
139
            '<html>' .
140
            '   <head>' .
141
            "       <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
142
            '       <title>%s</title>' .
143
            '       <style>' .
144
            '           body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif}' .
145
            '           h1{margin:0;font-size:48px;font-weight:normal;line-height:48px}' .
146
            '           strong{display:inline-block;width:65px}' .
147
            '       </style>' .
148
            '   </head>' .
149
            '   <body>' .
150
            '       <h1>%s</h1>' .
151
            '       <div>%s</div>' .
152
            '       <a href="#" onClick="window.history.go(-1)">Go Back</a>' .
153
            '   </body>' .
154
            '</html>',
155
            $title,
156
            $title,
157
            $content
158
        );
159
    }
160
}
161