NotFoundJsonHandler::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Basis\Handler;
6
7
use HttpSoft\Basis\Response\ExtractErrorDataTrait;
8
use HttpSoft\Basis\Response\PrepareJsonDataTrait;
9
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
10
use HttpSoft\Response\JsonResponse;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
15
final class NotFoundJsonHandler implements RequestHandlerInterface
16
{
17
    use ExtractErrorDataTrait;
18
    use PrepareJsonDataTrait;
19
20
    /**
21
     * @var bool
22
     */
23
    private bool $debug;
24
25
    /**
26
     * @param bool $debug
27
     */
28 79
    public function __construct(bool $debug = false)
29
    {
30 79
        $this->debug = $debug;
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36 6
    public function handle(ServerRequestInterface $request): ResponseInterface
37
    {
38 6
        $code = ErrorResponseGeneratorInterface::STATUS_NOT_FOUND;
39 6
        $message = ErrorResponseGeneratorInterface::ERROR_PHRASES[$code];
40 6
        $data = ['name' => 'Error', 'code' => $code, 'message' => $message];
41
42 6
        if ($this->debug) {
43 3
            $data['request'] = $this->extractRequestData($request);
44
        }
45
46 6
        return new JsonResponse($this->prepareJsonData($data), $code);
47
    }
48
}
49