Passed
Pull Request — develop (#349)
by Schlaefer
15:02 queued 10s
created

Development::handleException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 31
    public function __construct(array $settings = [])
26
    {
27 31
        $this->settings = $settings;
28 31
        $this->whoops = new Run();
29 31
        $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 31
    protected function createHandler(): Handler
55
    {
56 31
        if (PHILE_CLI_MODE) {
57 31
            $handler = new PlainTextHandler;
58 31
            $this->whoops->allowQuit(false);
59 31
            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