ExceptionHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setSapi() 0 5 1
A getSapi() 0 7 2
A handle() 0 3 1
A register() 0 10 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Debug\Exceptions\Handlers\Whoops;
6
7
use Opulence\Debug\Exceptions\Handlers\IExceptionHandler;
8
use Psr\Log\LoggerInterface;
9
use Throwable;
10
use Whoops\Handler\PlainTextHandler;
11
use Whoops\Handler\PrettyPageHandler;
12
13
/**
14
 * @SuppressWarnings(PHPMD)
15
 */
16
class ExceptionHandler implements IExceptionHandler
17
{
18
    protected LoggerInterface $logger;
19
20
    protected ExceptionRenderer $whoopsRenderer;
21
22
    protected ?string $sapi = null;
23
24
    /**
25
     * @param LoggerInterface   $logger
26
     * @param ExceptionRenderer $whoopsRenderer
27
     */
28
    public function __construct(LoggerInterface $logger, ExceptionRenderer $whoopsRenderer)
29
    {
30
        $this->logger         = $logger;
31
        $this->whoopsRenderer = $whoopsRenderer;
32
    }
33
34
    /**
35
     * @return string|null
36
     */
37
    public function getSapi(): ?string
38
    {
39
        if (null === $this->sapi) {
40
            $this->sapi = PHP_SAPI;
41
        }
42
43
        return $this->sapi;
44
    }
45
46
    /**
47
     * @param string|null $sapi
48
     *
49
     * @return $this
50
     */
51
    public function setSapi(?string $sapi): ExceptionHandler
52
    {
53
        $this->sapi = $sapi;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Handles an exception
60
     *
61
     * @param Throwable $ex The exception to handle
62
     */
63
    public function handle($ex): void
64
    {
65
        $this->whoopsRenderer->render($ex);
66
    }
67
68
    /**
69
     * Registers the handler with PHP
70
     */
71
    public function register(): void
72
    {
73
        $whoops = $this->whoopsRenderer->getRun();
74
75
        $whoops->pushHandler(new PlainTextHandler($this->logger));
76
        if ($this->getSapi() !== 'cli') {
77
            $whoops->pushHandler(new PrettyPageHandler());
78
        }
79
80
        $whoops->register();
81
    }
82
}
83