NativeException::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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
    {
20
        if ($message !== null) {
21
            $this->message = $message;
22
        }
23
        parent::__construct();
24
    }
25
26
    /**
27
     * Display native exception
28
     * @param string $message
29
     * @return string|null
30
     */
31
    public function display($message = null)
32
    {
33
        // if passed message is null get exception msg
34
        if (!$message) {
35
            $message = $this->message;
36
        }
37
        
38
        // hide root path from exception
39
        $message = Str::replace(root, '$DOCUMENT_ROOT', $message);
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Exception\root was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
        $message = strip_tags($message);
41
42
        // generate response based on environment type
43
        switch (env_type) {
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Exception\env_type was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
            case 'html':
45
                return $this->sendHTML($message);
46
            case 'json':
47
                return $this->sendJSON($message);
48
        }
49
        
50
        return $message;
51
    }
52
53
    /**
54
     * Build html response
55
     * @param string|null $message
56
     * @return string
57
     */
58
    protected function sendHTML($message = null)
59
    {
60
        //header('HTTP/1.1 404 Not Found');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
        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>';
62
    }
63
    
64
    /**
65
     * Build json response
66
     * @param string|null $message
67
     * @return string
68
     */
69
    protected function sendJSON($message = null)
70
    {
71
        header('Content-Type: application/json');
72
        return json_encode(['status' => 0, 'message' => $message]);
73
    }
74
}
75