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

TemplateException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 9
dl 0
loc 71
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B display() 0 24 4
B buildFakePage() 0 28 5
1
<?php
2
3
namespace Ffcms\Core\Exception;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Controller;
8
use Ffcms\Core\Helper\Type\Str;
9
10
abstract class TemplateException extends \Exception
11
{
12
    public $status = 404;
13
    public $title = '404 Not Found';
14
    public $text = 'An unexpected error occurred: %e%';
15
    public $tpl = 'default';
16
17
    /**
18
     * Display exception template
19
     * @throws JsonException
20
     * @throws \Exception
21
     * @return string
22
     */
23
    public function display()
24
    {
25
        // hide path root and stip tags from exception message
26
        $msg = Str::replace(root, '$DOCUMENT_ROOT', $this->getMessage());
27
        $msg = App::$Security->strip_tags($msg);
28
        // get message translation
29
        $this->text = App::$Translate->get('Default', $this->text, ['e' => $msg]);
30
        
31
        // if exception is throwed not from html-based environment
32
        if (env_type !== 'html') {
33
            if (env_type === 'json') {
34
                return (new JsonException($msg))->display();
35
            } else {
36
                return $this->text;
37
            }
38
        }
39
        
40
        // add notification in debug bar if exist
41
        if (App::$Debug !== null) {
42
            App::$Debug->addException($this);
43
        }
44
        // return rendered result
45
        return $this->buildFakePage();
46
    }
47
    
48
    /**
49
     * Build fake page with error based on fake controller initiation
50
     */
51
    protected function buildFakePage()
52
    {
53
        // initialize fake controller to display page with exception
54
        $fakeController = new Controller();
55
        // check if used no layout template
56
        if (defined('env_no_layout') && env_no_layout === true) {
57
            $fakeController->layout = null;
58
        }
59
        
60
        // add global title tag value
61
        $fakeController->setGlobalVar('title', App::$Translate->get('Default', $this->title));
62
        // build error text
63
        $rawResponse = 'error';
64
        try {
65
            $rawResponse = App::$View->render('native/errors/' . $this->tpl, ['msg' => $this->text]);
66
            if (Str::likeEmpty($rawResponse)) {
0 ignored issues
show
Bug introduced by
It seems like $rawResponse defined by \Ffcms\Core\App::$View->...('msg' => $this->text)) on line 65 can also be of type string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
                $rawResponse = $this->text;
68
            }
69
        } catch (SyntaxException $e) {
70
            $rawResponse = $this->text;
71
        }
72
        // set controller body response
73
        $fakeController->setResponse($rawResponse);
74
        // set status code for header
75
        App::$Response->setStatusCode((int)$this->status);
76
        // return compiled html output
77
        return $fakeController->getOutput();
78
    }
79
80
}