ErrorHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 26
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handleError() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Framework;
6
7
use Aidphp\Error\ErrorHandlerInterface;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Interop\Renderer\RendererInterface;
10
use Throwable;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
class ErrorHandler implements ErrorHandlerInterface
15
{
16
    public const TEMPLATE = 'server-error';
17
18
    protected $factory;
19
    protected $renderer;
20
    protected $debug;
21
    protected $template;
22
23 1
    public function __construct(ResponseFactoryInterface $factory, RendererInterface $renderer, bool $debug = false, string $template = self::TEMPLATE)
24
    {
25 1
        $this->factory  = $factory;
26 1
        $this->renderer = $renderer;
27 1
        $this->debug    = $debug;
28 1
        $this->template = $template;
29 1
    }
30
31 1
    public function handleError(Throwable $e, ServerRequestInterface $req = null): ResponseInterface
32
    {
33 1
        $res = $this->factory->createResponse(500);
34
35 1
        $res->getBody()->write($this->renderer->render($this->template, ['error' => new ErrorInfo($e, $this->debug, $req)]));
36
37 1
        return $res;
38
    }
39
}