ViewAction::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Action\Post;
6
7
use App\Model\Post\PostRepository;
8
use HttpSoft\Basis\Exception\NotFoundHttpException;
9
use HttpSoft\Basis\Response\PrepareJsonDataTrait;
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 ViewAction implements RequestHandlerInterface
16
{
17
    use PrepareJsonDataTrait;
18
19
    /**
20
     * @var PostRepository
21
     */
22
    private PostRepository $posts;
23
24
    /**
25
     * @param PostRepository $posts
26
     */
27 6
    public function __construct(PostRepository $posts)
28
    {
29 6
        $this->posts = $posts;
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     * @throws NotFoundHttpException
35
     */
36 6
    public function handle(ServerRequestInterface $request): ResponseInterface
37
    {
38 6
        if (!$user = $this->posts->findById((int) $request->getAttribute('id'))) {
39 3
            throw new NotFoundHttpException();
40
        }
41
42 3
        return new JsonResponse($this->prepareJsonData($user));
43
    }
44
}
45