Kernel::showErrorPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Faulancer;
4
5
use Faulancer\Controller\Dispatcher;
6
use Faulancer\Controller\ErrorController;
7
use Faulancer\Event\Observer;
8
use Faulancer\Event\Type\OnKernelError;
9
use Faulancer\Event\Type\OnKernelErrorException;
10
use Faulancer\Event\Type\OnKernelException;
11
use Faulancer\Event\Type\OnKernelStart;
12
use Faulancer\Exception\Exception;
13
use Faulancer\Http\Request;
14
use Faulancer\Service\Config;
15
16
/**
17
 * Class Kernel
18
 */
19
class Kernel
20
{
21
22
    /**
23
     * The current request object
24
     * @var Request
25
     */
26
    protected $request;
27
28
    /**
29
     * The configuration object
30
     * @var Config
31
     */
32
    protected $config;
33
34
    /**
35
     * Kernel constructor.
36
     *
37
     * @param Request $request
38
     * @param Config  $config
39
     */
40
    public function __construct(Request $request, Config $config)
41
    {
42
        $this->request = $request;
43
        $this->config  = $config;
44
    }
45
46
    /**
47
     * Initialize the application
48
     *
49
     * @return mixed
50
     * @codeCoverageIgnore
51
     */
52
    public function run()
53
    {
54
        Observer::instance()->trigger(new OnKernelStart($this));
55
56
        $dispatcher = new Dispatcher($this->request, $this->config);
57
58
        try {
59
60
            $this->registerErrorHandler();
61
            return $dispatcher->dispatch()->getContent();
62
63
        } catch (Exception $e) {
64
            Observer::instance()->trigger(new OnKernelException($this));
65
            return $this->showErrorPage($this->request, $e);
66
        } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
67
            Observer::instance()->trigger(new OnKernelErrorException($this));
68
            return $this->showErrorPage($this->request, $e);
69
        } catch (\Error $e) {
70
            Observer::instance()->trigger(new OnKernelError($this));
71
            return $this->showErrorPage($this->request, $e);
72
        }
73
74
    }
75
76
    /**
77
     * @param $request
78
     * @param $e
79
     * @return Http\Response
80
     * @codeCoverageIgnore
81
     */
82
    private function showErrorPage($request, $e)
83
    {
84
        $errorController = new ErrorController($request, $e);
85
        return $errorController->displayError();
86
    }
87
88
    /**
89
     * Register error handler
90
     */
91
    protected function registerErrorHandler()
92
    {
93
        set_error_handler([$this, 'errorHandler'], E_ALL);
94
    }
95
96
    /**
97
     * Custom error handler
98
     *
99
     * @param $errno
100
     * @param $errmsg
101
     * @param $errfile
102
     * @param $errline
103
     * @throws \ErrorException
104
     * @codeCoverageIgnore
105
     */
106
    public function errorHandler($errno, $errmsg, $errfile, $errline)
107
    {
108
        throw new \ErrorException($errmsg, $errno, 1, $errfile, $errline);
109
    }
110
111
}
112
113
114
115