NotFoundHandler::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
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