Completed
Push — master ( c3c3c2...6967ab )
by Mihail
02:44
created

NativeException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Ffcms\Core\Exception;
4
5
use Ffcms\Core\Helper\Type\Str;
6
7
/**
8
 * Class NativeException. Work around throwed exception to display it
9
 */
10
class NativeException extends \Exception
11
{
12
    protected $message;
13
14
    /**
15
     * NativeException constructor. Pass message insde the exception
16
     * @param string|null $message
17
     */
18
    public function __construct($message = null) {
19
        if ($message !== null) {
20
            $this->message = $message;
21
        }
22
    }
23
24
    /**
25
     * Display native exception
26
     * @param string $message
27
     * @return string|unknown
28
     */
29
    public function display($message = null)
30
    {
31
        // if passed message is null get exception msg
32
        if ($message === null) {
33
            $message = $this->message;
34
        }
35
36
        // hide root path from exception
37
        $message = Str::replace(root, '$DOCUMENT_ROOT', $message);
38
        $message = htmlentities($message);
39
        
40
        // generate response based on environment type
41
        switch (env_type) {
42
            case 'html':
43
                return $this->rawHTML($message);
0 ignored issues
show
Bug introduced by
The method rawHTML() does not seem to exist on object<Ffcms\Core\Exception\NativeException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            case 'json':
45
                return $this->sendJSON($message);
46
        }
47
        
48
        return $message;
49
    }
50
51
    /**
52
     * Build html response
53
     * @param unknown $message
54
     */
55
    protected function sendHTML($message = null)
56
    {
57
        header('HTTP/1.1 404 Not Found');
58
        return '<!DOCTYPE html><html><head><title>An error has occurred</title></head><body><div style="width:60%; margin: auto; background-color: #fcc;border: 1px solid #faa; padding: 0.5em 1em;"><h1 style="font-size: 120%">Runtime error</h1><p>' . $message . '</p></div></body></html>';
59
    }
60
    
61
    /**
62
     * Build json response
63
     * @param string $message
64
     */
65
    protected function sendJSON($message = NULL) 
66
    {
67
        header('Content-Type: application/json');
68
        return json_encode(['status' => 0, 'message' => $message]);
69
    }
70
}