Passed
Push — master ( 1f95e2...a32a1e )
by Mihail
04:19
created

BanException::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Ffcms\Core\Exception;
4
5
use Ffcms\Core\Helper\Type\Str;
6
7
/**
8
 * Class BanException. Work around throwed exception to display it
9
 */
10
class BanException extends \Exception
11
{
12
    protected $message;
13
    protected $title;
14
15
    /**
16
     * BanException constructor
17
     * @param string|null $message
18
     */
19
    public function __construct($message = null, $title = null)
20
    {
21
        if ($message !== null) {
22
            $this->message = $message;
23
        }
24
25
        if ($title !== null) {
26
            $this->title = $title;
27
        }
28
        parent::__construct();
29
    }
30
31
    /**
32
     * Display native exception
33
     * @param string $message
34
     * @return string|null
35
     */
36
    public function display($message = null)
37
    {
38
        // if passed message is null get exception msg
39
        if (!$message) {
40
            $message = $this->message;
41
        }
42
        
43
        // hide root path from exception
44
        $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...
45
        $message = strip_tags($message);
46
47
        // generate response based on environment type
48
        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...
49
            case 'html':
50
                return $this->sendHTML($message, $this->title ?? 'Error');
51
            case 'json':
52
                return $this->sendJSON($message, $this->title ?? 'Error');
0 ignored issues
show
Unused Code introduced by
The call to Ffcms\Core\Exception\BanException::sendJSON() has too many arguments starting with $this->title ?? 'Error'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                return $this->/** @scrutinizer ignore-call */ sendJSON($message, $this->title ?? 'Error');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
        }
54
        
55
        return $message;
56
    }
57
58
    /**
59
     * Build html response
60
     * @param string|null $message
61
     * @return string
62
     */
63
    protected function sendHTML($message = null, $title = null)
64
    {
65
        //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...
66
        return '<!DOCTYPE html><html><head><title>'. $this->title . '</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%">' . $title . '</h1><p>' . $message . '</p></div></body></html>';
67
    }
68
    
69
    /**
70
     * Build json response
71
     * @param string|null $message
72
     * @return string
73
     */
74
    protected function sendJSON($message = null)
75
    {
76
        header('Content-Type: application/json');
77
        return json_encode(['status' => 0, 'message' => $message]);
78
    }
79
}
80