Passed
Push — master ( ce6b45...051346 )
by Alexander
03:32
created

BlogController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use App\Formatter\PaginatorFormatter;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Yiisoft\DataResponse\DataResponseFactoryInterface;
10
use OpenApi\Annotations as OA;
11
12
/**
13
 * @OA\Tag(
14
 *     name="blog",
15
 *     description="Blog"
16
 * )
17
 */
18
final class BlogController
19
{
20
    private DataResponseFactoryInterface $responseFactory;
21
    private PostFormatter $postFormatter;
22
    private BlogService $blogService;
23
24 2
    public function __construct(
25
        DataResponseFactoryInterface $responseFactory,
26
        PostFormatter $postFormatter,
27
        BlogService $blogService
28
    ) {
29 2
        $this->responseFactory = $responseFactory;
30 2
        $this->postFormatter = $postFormatter;
31 2
        $this->blogService = $blogService;
32 2
    }
33
34
    /**
35
     * @OA\Get(
36
     *     tags={"blog"},
37
     *     path="/blog/",
38
     *     summary="Returns paginated blog posts",
39
     *     description="",
40
     *     @OA\Parameter(ref="#/components/parameters/PageRequest"),
41
     *     @OA\Response(
42
     *          response="200",
43
     *          description="Success",
44
     *          @OA\JsonContent(
45
     *              allOf={
46
     *                  @OA\Schema(ref="#/components/schemas/Response"),
47
     *                  @OA\Schema(
48
     *                      @OA\Property(
49
     *                          property="data",
50
     *                          type="object",
51
     *                          @OA\Property(
52
     *                              property="posts",
53
     *                              type="array",
54
     *                              @OA\Items(ref="#/components/schemas/Post")
55
     *                          ),
56
     *                          @OA\Property(
57
     *                              property="paginator",
58
     *                              type="object",
59
     *                              ref="#/components/schemas/Paginator"
60
     *                          ),
61
     *                      ),
62
     *                  ),
63
     *              },
64
     *          )
65
     *    ),
66
     * )
67
     */
68 1
    public function index(PageRequest $request, PaginatorFormatter $paginatorFormatter): Response
69
    {
70 1
        $paginator = $this->blogService->getPosts($request->getPage());
71 1
        $posts = [];
72 1
        foreach ($paginator->read() as $post) {
73 1
            $posts[] = $this->postFormatter->format($post);
74
        }
75
76 1
        return $this->responseFactory->createResponse(
77
            [
78 1
                'paginator' => $paginatorFormatter->format($paginator),
79 1
                'posts' => $posts,
80
            ]
81
        );
82
    }
83
84
    /**
85
     * @OA\Get(
86
     *     tags={"blog"},
87
     *     path="/blog/{id}",
88
     *     summary="Returns a post with a given ID",
89
     *     description="",
90
     *     @OA\Parameter(
91
     *          @OA\Schema(type="int", example="2"),
92
     *          in="path",
93
     *          name="id",
94
     *          parameter="id"
95
     *     ),
96
     *     @OA\Response(
97
     *          response="200",
98
     *          description="Success",
99
     *          @OA\JsonContent(
100
     *              allOf={
101
     *                  @OA\Schema(ref="#/components/schemas/Response"),
102
     *                  @OA\Schema(
103
     *                      @OA\Property(
104
     *                          property="data",
105
     *                          type="object",
106
     *                          @OA\Property(
107
     *                              property="post",
108
     *                              type="object",
109
     *                              ref="#/components/schemas/Post"
110
     *                          ),
111
     *                      ),
112
     *                  ),
113
     *              },
114
     *          )
115
     *    ),
116
     *    @OA\Response(
117
     *          response="404",
118
     *          description="Not found",
119
     *          @OA\JsonContent(
120
     *              allOf={
121
     *                  @OA\Schema(ref="#/components/schemas/BadResponse"),
122
     *                  @OA\Schema(
123
     *                      @OA\Property(property="error_message", example="Entity not found"),
124
     *                      @OA\Property(property="error_code", nullable=true, example=404)
125
     *                  ),
126
     *              },
127
     *          )
128
     *    ),
129
     * )
130
     */
131 1
    public function view(ViewPostRequest $request): Response
132
    {
133 1
        return $this->responseFactory->createResponse(
134
            [
135 1
                'post' => $this->postFormatter->format(
136 1
                    $this->blogService->getPost($request->getId())
137
                ),
138
            ]
139
        );
140
    }
141
}
142