NotFoundHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 53
ccs 12
cts 12
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A handle() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Basis\Handler;
6
7
use HttpSoft\Basis\TemplateRendererInterface;
8
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
14
final class NotFoundHandler implements RequestHandlerInterface
15
{
16
    /**
17
     * @var ResponseFactoryInterface
18
     */
19
    private ResponseFactoryInterface $responseFactory;
20
21
    /**
22
     * @var TemplateRendererInterface
23
     */
24
    private TemplateRendererInterface $template;
25
26
    /**
27
     * @var string
28
     */
29
    private string $view;
30
31
    /**
32
     * @var bool
33
     */
34
    private bool $debug;
35
36
    /**
37
     * @param ResponseFactoryInterface $responseFactory
38
     * @param TemplateRendererInterface $template
39
     * @param string $view
40
     * @param bool $debug
41
     */
42 18
    public function __construct(
43
        ResponseFactoryInterface $responseFactory,
44
        TemplateRendererInterface $template,
45
        string $view,
46
        bool $debug = false
47
    ) {
48 18
        $this->responseFactory = $responseFactory;
49 18
        $this->template = $template;
50 18
        $this->view = $view;
51 18
        $this->debug = $debug;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 6
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59 6
        $response = $this->responseFactory->createResponse(ErrorResponseGeneratorInterface::STATUS_NOT_FOUND);
60
61 6
        $response->getBody()->write($this->template->render($this->view, [
62 6
            'debug' => $this->debug,
63 6
            'request' => $request,
64 6
        ]));
65
66 6
        return $response;
67
    }
68
}
69