NotFoundHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Application\Handler;
4
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\StreamFactoryInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
/**
12
 * A simple handler that sends a 404 response.
13
 * @author Riikka Kalliomäki <[email protected]>
14
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
15
 * @license http://opensource.org/licenses/mit-license.php MIT License
16
 */
17
class NotFoundHandler implements RequestHandlerInterface
18
{
19
    /** @var ResponseFactoryInterface The factory used to generate the response */
20
    private $responseFactory;
21
22
    /** @var StreamFactoryInterface The factory used to generate the body for the response */
23
    private $streamFactory;
24
25
    /**
26
     * NotFoundHandler constructor.
27
     * @param ResponseFactoryInterface $responseFactory The factory used to generate the response
28
     * @param StreamFactoryInterface $streamFactory The factory used to generate the body for the response
29
     */
30 12
    public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
31
    {
32 12
        $this->responseFactory = $responseFactory;
33 12
        $this->streamFactory = $streamFactory;
34 12
    }
35
36
    /** {@inheritdoc} */
37 1
    public function handle(ServerRequestInterface $request): ResponseInterface
38
    {
39 1
        return $this->responseFactory->createResponse(404, 'Not Found')
40 1
            ->withHeader('Content-Type', 'text/plain; charset=utf-8')
41 1
            ->withBody($this->streamFactory->createStream('Not Found'));
42
    }
43
}
44