Passed
Push — master ( f5765f...956756 )
by Mihail
03:05
created

TemplateException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFakePage() 0 14 3
B display() 0 24 4
1
<?php
2
3
namespace Ffcms\Core\Exception;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Controller;
7
use Ffcms\Core\Helper\Type\Str;
8
9
abstract class TemplateException extends \Exception
10
{
11
    public $status = 404;
12
    public $title = '404 Not Found';
13
    public $text = 'An unexpected error occurred: %e%';
14
    public $tpl = 'default';
15
16
    /**
17
     * Display exception template
18
     * @return string
19
     */
20
    public function display()
21
    {
22
        // hide path root and stip tags from exception message
23
        $msg = Str::replace(root, '$DOCUMENT_ROOT', $this->getMessage());
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...
24
        $msg = App::$Security->strip_tags($msg);
25
        // get message translation
26
        $this->text = App::$Translate->get('Default', $this->text, ['e' => $msg]);
27
        
28
        // if exception is throwed not from html-based environment
29
        if (env_type !== 'html') {
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...
30
            if (env_type === 'json') {
31
                return (new JsonException($msg))->display();
32
            } else {
33
                return $this->text;
34
            }
35
        }
36
        
37
        // add notification in debug bar if exist
38
        if (App::$Debug !== null) {
39
            App::$Debug->addException($this);
40
        }
41
42
        // return rendered result
43
        return $this->buildFakePage();
44
    }
45
    
46
    /**
47
     * Build fake page with error based on fake controller initiation
48
     */
49
    protected function buildFakePage()
50
    {
51
        try {
52
            $rawResponse = App::$View->render('native/errors/' . $this->tpl, ['msg' => $this->text]);
53
            if (Str::likeEmpty($rawResponse)) {
54
                $rawResponse = $this->text;
55
            }
56
        } catch (\Exception $e) {
57
            $rawResponse = $this->text;
58
        }
59
        // set status code for header
60
        App::$Response->setStatusCode((int)$this->status);
61
        // return compiled html output
62
        return $rawResponse;
63
    }
64
}
65