ViewAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 28
ccs 6
cts 6
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 7 2
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