1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Blog; |
6
|
|
|
|
7
|
|
|
use App\Formatter\PaginatorFormatter; |
8
|
|
|
use App\User\UserRequest; |
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
10
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
11
|
|
|
use OpenApi\Annotations as OA; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @OA\Tag( |
15
|
|
|
* name="blog", |
16
|
|
|
* description="Blog" |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
final class BlogController |
20
|
|
|
{ |
21
|
|
|
private DataResponseFactoryInterface $responseFactory; |
22
|
|
|
private PostRepository $postRepository; |
23
|
|
|
private PostFormatter $postFormatter; |
24
|
|
|
private PostBuilder $postBuilder; |
25
|
|
|
private BlogService $blogService; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
PostRepository $postRepository, |
29
|
|
|
DataResponseFactoryInterface $responseFactory, |
30
|
|
|
PostFormatter $postFormatter, |
31
|
|
|
PostBuilder $postBuilder, |
32
|
|
|
BlogService $blogService |
33
|
|
|
) { |
34
|
|
|
$this->postRepository = $postRepository; |
35
|
|
|
$this->responseFactory = $responseFactory; |
36
|
|
|
$this->postFormatter = $postFormatter; |
37
|
|
|
$this->postBuilder = $postBuilder; |
38
|
|
|
$this->blogService = $blogService; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @OA\Get( |
43
|
|
|
* tags={"blog"}, |
44
|
|
|
* path="/blog/", |
45
|
|
|
* summary="Returns paginated blog posts", |
46
|
|
|
* description="", |
47
|
|
|
* @OA\Parameter(ref="#/components/parameters/PageRequest"), |
48
|
|
|
* @OA\Response( |
49
|
|
|
* response="200", |
50
|
|
|
* description="Success", |
51
|
|
|
* @OA\JsonContent( |
52
|
|
|
* allOf={ |
53
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
54
|
|
|
* @OA\Schema( |
55
|
|
|
* @OA\Property( |
56
|
|
|
* property="data", |
57
|
|
|
* type="object", |
58
|
|
|
* @OA\Property( |
59
|
|
|
* property="posts", |
60
|
|
|
* type="array", |
61
|
|
|
* @OA\Items(ref="#/components/schemas/Post") |
62
|
|
|
* ), |
63
|
|
|
* @OA\Property( |
64
|
|
|
* property="paginator", |
65
|
|
|
* type="object", |
66
|
|
|
* ref="#/components/schemas/Paginator" |
67
|
|
|
* ), |
68
|
|
|
* ), |
69
|
|
|
* ), |
70
|
|
|
* }, |
71
|
|
|
* ) |
72
|
|
|
* ), |
73
|
|
|
* ) |
74
|
|
|
*/ |
75
|
|
|
public function index(PageRequest $request, PaginatorFormatter $paginatorFormatter): Response |
76
|
|
|
{ |
77
|
|
|
$paginator = $this->blogService->getPosts($request->getPage()); |
78
|
|
|
$posts = []; |
79
|
|
|
foreach ($paginator->read() as $post) { |
80
|
|
|
$posts[] = $this->postFormatter->format($post); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->responseFactory->createResponse( |
84
|
|
|
[ |
85
|
|
|
'paginator' => $paginatorFormatter->format($paginator), |
86
|
|
|
'posts' => $posts, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @OA\Get( |
93
|
|
|
* tags={"blog"}, |
94
|
|
|
* path="/blog/{id}", |
95
|
|
|
* summary="Returns a post with a given ID", |
96
|
|
|
* description="", |
97
|
|
|
* @OA\Parameter( |
98
|
|
|
* @OA\Schema(type="int", example="2"), |
99
|
|
|
* in="path", |
100
|
|
|
* name="id", |
101
|
|
|
* parameter="id" |
102
|
|
|
* ), |
103
|
|
|
* @OA\Response( |
104
|
|
|
* response="200", |
105
|
|
|
* description="Success", |
106
|
|
|
* @OA\JsonContent( |
107
|
|
|
* allOf={ |
108
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
109
|
|
|
* @OA\Schema( |
110
|
|
|
* @OA\Property( |
111
|
|
|
* property="data", |
112
|
|
|
* type="object", |
113
|
|
|
* @OA\Property( |
114
|
|
|
* property="post", |
115
|
|
|
* type="object", |
116
|
|
|
* ref="#/components/schemas/Post" |
117
|
|
|
* ), |
118
|
|
|
* ), |
119
|
|
|
* ), |
120
|
|
|
* }, |
121
|
|
|
* ) |
122
|
|
|
* ), |
123
|
|
|
* @OA\Response( |
124
|
|
|
* response="404", |
125
|
|
|
* description="Not found", |
126
|
|
|
* @OA\JsonContent( |
127
|
|
|
* allOf={ |
128
|
|
|
* @OA\Schema(ref="#/components/schemas/BadResponse"), |
129
|
|
|
* @OA\Schema( |
130
|
|
|
* @OA\Property(property="error_message", example="Entity not found"), |
131
|
|
|
* @OA\Property(property="error_code", nullable=true, example=404) |
132
|
|
|
* ), |
133
|
|
|
* }, |
134
|
|
|
* ) |
135
|
|
|
* ), |
136
|
|
|
* ) |
137
|
|
|
*/ |
138
|
|
|
public function view(ViewPostRequest $request): Response |
139
|
|
|
{ |
140
|
|
|
return $this->responseFactory->createResponse( |
141
|
|
|
[ |
142
|
|
|
'post' => $this->postFormatter->format( |
143
|
|
|
$this->blogService->getPost($request->getId()) |
144
|
|
|
), |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @OA\Post( |
151
|
|
|
* tags={"blog"}, |
152
|
|
|
* path="/blog", |
153
|
|
|
* summary="Creates a blog post", |
154
|
|
|
* description="", |
155
|
|
|
* security={{"ApiKey": {}}}, |
156
|
|
|
* @OA\Response( |
157
|
|
|
* response="200", |
158
|
|
|
* description="Success", |
159
|
|
|
* @OA\JsonContent( |
160
|
|
|
* ref="#/components/schemas/Response" |
161
|
|
|
* ) |
162
|
|
|
* ), |
163
|
|
|
* @OA\RequestBody( |
164
|
|
|
* required=true, |
165
|
|
|
* @OA\MediaType( |
166
|
|
|
* mediaType="application/json", |
167
|
|
|
* @OA\Schema(ref="#/components/schemas/EditPostRequest"), |
168
|
|
|
* ), |
169
|
|
|
* ), |
170
|
|
|
* ) |
171
|
|
|
*/ |
172
|
|
|
public function create(EditPostRequest $postRequest, UserRequest $userRequest): Response |
173
|
|
|
{ |
174
|
|
|
$post = $this->postBuilder->build(new Post(), $postRequest); |
175
|
|
|
$post->setUser($userRequest->getUser()); |
176
|
|
|
|
177
|
|
|
$this->postRepository->save($post); |
178
|
|
|
|
179
|
|
|
return $this->responseFactory->createResponse(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @OA\Put( |
184
|
|
|
* tags={"blog"}, |
185
|
|
|
* path="/blog/{id}", |
186
|
|
|
* summary="Updates a blog post with a given ID", |
187
|
|
|
* description="", |
188
|
|
|
* security={{"ApiKey": {}}}, |
189
|
|
|
* @OA\Parameter( |
190
|
|
|
* @OA\Schema(type="int", example="2"), |
191
|
|
|
* in="path", |
192
|
|
|
* name="id", |
193
|
|
|
* parameter="id" |
194
|
|
|
* ), |
195
|
|
|
* @OA\Response( |
196
|
|
|
* response="200", |
197
|
|
|
* description="Success", |
198
|
|
|
* @OA\JsonContent( |
199
|
|
|
* ref="#/components/schemas/Response" |
200
|
|
|
* ) |
201
|
|
|
* ), |
202
|
|
|
* @OA\RequestBody( |
203
|
|
|
* required=true, |
204
|
|
|
* @OA\MediaType( |
205
|
|
|
* mediaType="application/json", |
206
|
|
|
* @OA\Schema(ref="#/components/schemas/EditPostRequest"), |
207
|
|
|
* ), |
208
|
|
|
* ) |
209
|
|
|
* ) |
210
|
|
|
*/ |
211
|
|
|
public function update(EditPostRequest $postRequest): Response |
212
|
|
|
{ |
213
|
|
|
$post = $this->postBuilder->build( |
214
|
|
|
$this->blogService->getPost($postRequest->getId()), |
215
|
|
|
$postRequest |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$this->postRepository->save($post); |
219
|
|
|
|
220
|
|
|
return $this->responseFactory->createResponse(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|