ErrorHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 2
b 0
f 0
dl 0
loc 28
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transfer() 0 3 1
A handle() 0 6 1
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\Resource\ResourceObject;
8
use BEAR\Sunday\Extension\Error\ErrorInterface;
9
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
10
use BEAR\Sunday\Extension\Transfer\TransferInterface;
11
use Exception;
12
13
/**
14
 * vnd.error for BEAR.Package
15
 *
16
 * @see https://github.com/blongden/vnd.error
17
 */
18
final class ErrorHandler implements ErrorInterface
19
{
20
    private ResourceObject|null $errorPage = null;
21
22
    public function __construct(
23
        private TransferInterface $responder,
24
        private ErrorLogger $logger,
25
        private ErrorPageFactoryInterface $factory,
26
    ) {
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function handle(Exception $e, Request $request) // phpcs:ignore SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
33
    {
34
        ($this->logger)($e, $request);
35
        $this->errorPage = $this->factory->newInstance($e, $request);
36
37
        return $this;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function transfer(): void
44
    {
45
        ($this->responder)($this->errorPage ?? new NullPage(), []);
46
    }
47
}
48