Development::handleError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 4
cp 0
crap 2
rs 10
c 2
b 0
f 1
1
<?php
2
/**
3
 * @link http://philecms.github.io/
4
 * @license http://opensource.org/licenses/MIT
5
 * @package Phile\Plugin\Phile\ErrorHandler
6
 */
7
8
namespace Phile\Plugin\Phile\ErrorHandler;
9
10
use Phile\ServiceLocator\ErrorHandlerInterface;
11
use Whoops\Handler\Handler;
12
use Whoops\Handler\PlainTextHandler;
13
use Whoops\Handler\PrettyPageHandler;
14
use Whoops\Run;
15
16
/**
17
 * Developement error handler: use whoops
18
 */
19
class Development implements ErrorHandlerInterface
20
{
21
    protected $settings;
22
23
    protected $whoops;
24
25 34
    public function __construct(array $settings = [])
26
    {
27 34
        $this->settings = $settings;
28 34
        $this->whoops = new Run();
29 34
        $this->whoops->pushHandler($this->createHandler());
30
    }
31
32
    public function handleError(
33
        int $errno,
34
        string $errstr,
35
        ?string $errfile,
36
        ?int $errline
37
    ): bool {
38
        $level = $this->settings['level'];
39
        $this->whoops->{Run::ERROR_HANDLER}($level, $errstr, $errfile, $errline);
40
41
        return true;
42
    }
43
44 1
    public function handleException(\Throwable $exception)
45
    {
46 1
        $this->whoops->{Run::EXCEPTION_HANDLER}($exception);
47
    }
48
49
    public function handleShutdown()
50
    {
51
        $this->whoops->{Run::SHUTDOWN_HANDLER}();
52
    }
53
54 34
    protected function createHandler(): Handler
55
    {
56 34
        if (PHILE_CLI_MODE) {
57 34
            $handler = new PlainTextHandler;
58 34
            $this->whoops->allowQuit(false);
59 34
            return $handler;
60
        }
61
62
        $handler = new PrettyPageHandler;
63
        if (!empty($this->settings['editor'])) {
64
            $handler->setEditor($this->settings['editor']);
65
        }
66
        return $handler;
67
    }
68
}
69