@@ -47,48 +47,48 @@ discard block |
||
47 | 47 | use Gos\Bundle\WebSocketBundle\Pusher\PusherRegistry; |
48 | 48 | |
49 | 49 | class ContentListItemController extends AbstractController { |
50 | - private ContentListItemRepositoryInterface $contentListItemRepository; |
|
51 | - private EntityManagerInterface $entityManager; |
|
52 | - private ContentListServiceInterface $contentListService; |
|
53 | - private EventDispatcherInterface $eventDispatcher; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var PusherInterface |
|
57 | - */ |
|
58 | - private $pusher; |
|
59 | - |
|
60 | - /** |
|
61 | - * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
62 | - * @param EntityManagerInterface $entityManager |
|
63 | - * @param ContentListServiceInterface $contentListService |
|
64 | - * @param EventDispatcherInterface $eventDispatcher |
|
65 | - */ |
|
66 | - public function __construct( |
|
67 | - ContentListItemRepositoryInterface $contentListItemRepository, |
|
68 | - EntityManagerInterface $entityManager, |
|
69 | - ContentListServiceInterface $contentListService, |
|
70 | - EventDispatcherInterface $eventDispatcher, |
|
71 | - string $invalidationCacheUrl, |
|
72 | - string $invalidationToken, |
|
73 | - PusherRegistry $pusher |
|
74 | - ) { |
|
75 | - $this->contentListItemRepository = $contentListItemRepository; |
|
76 | - $this->entityManager = $entityManager; |
|
77 | - $this->contentListService = $contentListService; |
|
78 | - $this->eventDispatcher = $eventDispatcher; |
|
79 | - $this->invalidationCacheUrl = $invalidationCacheUrl; |
|
80 | - $this->invalidationToken = $invalidationToken; |
|
81 | - $this->pusher = $pusher->getPusher('amqp'); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
87 | - */ |
|
88 | - public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
50 | + private ContentListItemRepositoryInterface $contentListItemRepository; |
|
51 | + private EntityManagerInterface $entityManager; |
|
52 | + private ContentListServiceInterface $contentListService; |
|
53 | + private EventDispatcherInterface $eventDispatcher; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var PusherInterface |
|
57 | + */ |
|
58 | + private $pusher; |
|
59 | + |
|
60 | + /** |
|
61 | + * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
62 | + * @param EntityManagerInterface $entityManager |
|
63 | + * @param ContentListServiceInterface $contentListService |
|
64 | + * @param EventDispatcherInterface $eventDispatcher |
|
65 | + */ |
|
66 | + public function __construct( |
|
67 | + ContentListItemRepositoryInterface $contentListItemRepository, |
|
68 | + EntityManagerInterface $entityManager, |
|
69 | + ContentListServiceInterface $contentListService, |
|
70 | + EventDispatcherInterface $eventDispatcher, |
|
71 | + string $invalidationCacheUrl, |
|
72 | + string $invalidationToken, |
|
73 | + PusherRegistry $pusher |
|
74 | + ) { |
|
75 | + $this->contentListItemRepository = $contentListItemRepository; |
|
76 | + $this->entityManager = $entityManager; |
|
77 | + $this->contentListService = $contentListService; |
|
78 | + $this->eventDispatcher = $eventDispatcher; |
|
79 | + $this->invalidationCacheUrl = $invalidationCacheUrl; |
|
80 | + $this->invalidationToken = $invalidationToken; |
|
81 | + $this->pusher = $pusher->getPusher('amqp'); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
87 | + */ |
|
88 | + public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
89 | 89 | $sort = $request->query->all('sorting'); |
90 | 90 | if (empty($sort)) { |
91 | - $sort = ['sticky' => 'desc']; |
|
91 | + $sort = ['sticky' => 'desc']; |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | $items = $this->contentListItemRepository->getPaginatedByCriteria( |
@@ -121,20 +121,20 @@ discard block |
||
121 | 121 | ); |
122 | 122 | |
123 | 123 | return new ResourcesListResponse($items, $responseContext); |
124 | - } |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_show_lists_item", requirements={"id"="\d+"}) |
|
128 | - */ |
|
129 | - public function getAction($listId, $id) { |
|
126 | + /** |
|
127 | + * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_show_lists_item", requirements={"id"="\d+"}) |
|
128 | + */ |
|
129 | + public function getAction($listId, $id) { |
|
130 | 130 | return new SingleResourceResponse($this->findOr404($listId, $id)); |
131 | - } |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_lists_item", requirements={"id"="\d+", "listId"="\d+"}) |
|
135 | - */ |
|
136 | - public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
137 | - $id): SingleResourceResponseInterface { |
|
133 | + /** |
|
134 | + * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_lists_item", requirements={"id"="\d+", "listId"="\d+"}) |
|
135 | + */ |
|
136 | + public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
137 | + $id): SingleResourceResponseInterface { |
|
138 | 138 | $contentListItem = $this->findOr404($listId, $id); |
139 | 139 | $form = $formFactory->createNamed( |
140 | 140 | '', |
@@ -146,13 +146,13 @@ discard block |
||
146 | 146 | $form->handleRequest($request); |
147 | 147 | |
148 | 148 | if ($form->isSubmitted() && $form->isValid()) { |
149 | - $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
149 | + $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
150 | 150 | |
151 | - if (null !== $contentListItem->getStickyPosition()) { |
|
151 | + if (null !== $contentListItem->getStickyPosition()) { |
|
152 | 152 | $contentListItem->setPosition($contentListItem->getStickyPosition()); |
153 | - } |
|
153 | + } |
|
154 | 154 | |
155 | - $this->entityManager->flush(); |
|
155 | + $this->entityManager->flush(); |
|
156 | 156 | ContentListController::invalidateCache( |
157 | 157 | $this->invalidationCacheUrl, |
158 | 158 | $this->invalidationToken, |
@@ -164,46 +164,46 @@ discard block |
||
164 | 164 | ] |
165 | 165 | ); |
166 | 166 | |
167 | - return new SingleResourceResponse($contentListItem); |
|
167 | + return new SingleResourceResponse($contentListItem); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | return new SingleResourceResponse($form, new ResponseContext(400)); |
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @Route("/api/{version}/content/lists/{listId}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_batch_update_lists_item", requirements={"listId"="\d+"}) |
|
175 | - */ |
|
176 | - public function batchUpdateAction( |
|
177 | - Request $request, |
|
178 | - FormFactoryInterface $formFactory, |
|
179 | - ContentListRepositoryInterface $contentListRepository, |
|
180 | - ArticleRepositoryInterface $articleRepository, |
|
181 | - EventDispatcherInterface $eventDispatcher, |
|
182 | - int $listId |
|
183 | - ): SingleResourceResponseInterface { |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @Route("/api/{version}/content/lists/{listId}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_batch_update_lists_item", requirements={"listId"="\d+"}) |
|
175 | + */ |
|
176 | + public function batchUpdateAction( |
|
177 | + Request $request, |
|
178 | + FormFactoryInterface $formFactory, |
|
179 | + ContentListRepositoryInterface $contentListRepository, |
|
180 | + ArticleRepositoryInterface $articleRepository, |
|
181 | + EventDispatcherInterface $eventDispatcher, |
|
182 | + int $listId |
|
183 | + ): SingleResourceResponseInterface { |
|
184 | 184 | /** @var ContentListInterface $list */ |
185 | 185 | $list = $contentListRepository->findOneBy(['id' => $listId]); |
186 | 186 | if (null === $list) { |
187 | - throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
187 | + throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | $form = $formFactory->createNamed('', ContentListItemsType::class, [], ['method' => $request->getMethod()]); |
191 | 191 | |
192 | 192 | $form->handleRequest($request); |
193 | 193 | if ($form->isSubmitted() && $form->isValid()) { |
194 | - $data = $form->getData(); |
|
195 | - $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
196 | - $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
197 | - $listUpdatedAt = $list->getUpdatedAt(); |
|
198 | - $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
199 | - if ($updatedAt < $listUpdatedAt) { |
|
194 | + $data = $form->getData(); |
|
195 | + $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
196 | + $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
197 | + $listUpdatedAt = $list->getUpdatedAt(); |
|
198 | + $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
199 | + if ($updatedAt < $listUpdatedAt) { |
|
200 | 200 | throw new ConflictHttpException('List was already updated'); |
201 | - } |
|
201 | + } |
|
202 | 202 | |
203 | - $updatedArticles = []; |
|
204 | - $updatedItemsInvalidateCache = []; |
|
205 | - /** @var ContentListAction $item */ |
|
206 | - foreach ($data['items'] as $item) { |
|
203 | + $updatedArticles = []; |
|
204 | + $updatedItemsInvalidateCache = []; |
|
205 | + /** @var ContentListAction $item */ |
|
206 | + foreach ($data['items'] as $item) { |
|
207 | 207 | $position = $item->getPosition(); |
208 | 208 | $isSticky = $item->isSticky(); |
209 | 209 | $contentId = $item->getContentId(); |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | |
218 | 218 | |
219 | 219 | switch ($item->getAction()) { |
220 | - case ContentListAction::ACTION_MOVE: |
|
220 | + case ContentListAction::ACTION_MOVE: |
|
221 | 221 | $updated = false; |
222 | 222 | $contentListItem = $this->findByContentOr404($list, $contentId); |
223 | 223 | |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
246 | 246 | |
247 | 247 | break; |
248 | - case ContentListAction::ACTION_ADD: |
|
248 | + case ContentListAction::ACTION_ADD: |
|
249 | 249 | $this->ensureThereIsNoItemOnPositionOrThrow409( |
250 | 250 | $listId, |
251 | 251 | $position, |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
260 | 260 | |
261 | 261 | break; |
262 | - case ContentListAction::ACTION_DELETE: |
|
262 | + case ContentListAction::ACTION_DELETE: |
|
263 | 263 | $contentListItem = $this->findByContentOr404($list, $contentId); |
264 | 264 | $this->entityManager->remove($contentListItem); |
265 | 265 | $list->setUpdatedAt(new DateTime('now')); |
@@ -268,17 +268,17 @@ discard block |
||
268 | 268 | |
269 | 269 | break; |
270 | 270 | } |
271 | - } |
|
271 | + } |
|
272 | 272 | |
273 | - $this->contentListService->repositionStickyItems($list); |
|
273 | + $this->contentListService->repositionStickyItems($list); |
|
274 | 274 | |
275 | - foreach ($updatedArticles as $updatedArticle) { |
|
275 | + foreach ($updatedArticles as $updatedArticle) { |
|
276 | 276 | $eventDispatcher->dispatch(new ArticleEvent( |
277 | 277 | $updatedArticle, |
278 | 278 | $updatedArticle->getPackage(), |
279 | 279 | ArticleEvents::POST_UPDATE |
280 | 280 | ), ArticleEvents::POST_UPDATE); |
281 | - } |
|
281 | + } |
|
282 | 282 | ContentListController::invalidateCache( |
283 | 283 | $this->invalidationCacheUrl, |
284 | 284 | $this->invalidationToken, |
@@ -291,16 +291,16 @@ discard block |
||
291 | 291 | ] |
292 | 292 | ); |
293 | 293 | |
294 | - // Dispatch WebSocket update |
|
295 | - $this->dispatchWebSocketUpdate($list, $updatedItemsInvalidateCache, 'BATCH-UPDATE'); |
|
294 | + // Dispatch WebSocket update |
|
295 | + $this->dispatchWebSocketUpdate($list, $updatedItemsInvalidateCache, 'BATCH-UPDATE'); |
|
296 | 296 | |
297 | - return new SingleResourceResponse($list, new ResponseContext(201)); |
|
297 | + return new SingleResourceResponse($list, new ResponseContext(201)); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | return new SingleResourceResponse($form, new ResponseContext(400)); |
301 | - } |
|
301 | + } |
|
302 | 302 | |
303 | - private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
303 | + private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
304 | 304 | /** @var ContentListItemInterface $listItem */ |
305 | 305 | $listItem = $this->contentListItemRepository->findOneBy([ |
306 | 306 | 'contentList' => $listId, |
@@ -308,13 +308,13 @@ discard block |
||
308 | 308 | ]); |
309 | 309 | |
310 | 310 | if (null === $listItem) { |
311 | - throw new NotFoundHttpException(sprintf('Content list item with content_id "%s" was not found on that list. If You want to add new item - use action type "add".', $contentId)); |
|
311 | + throw new NotFoundHttpException(sprintf('Content list item with content_id "%s" was not found on that list. If You want to add new item - use action type "add".', $contentId)); |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | return $listItem; |
315 | - } |
|
315 | + } |
|
316 | 316 | |
317 | - private function findOr404($listId, $id): ContentListItemInterface { |
|
317 | + private function findOr404($listId, $id): ContentListItemInterface { |
|
318 | 318 | /** @var ContentListItemInterface $listItem */ |
319 | 319 | $listItem = $this->contentListItemRepository->findOneBy([ |
320 | 320 | 'contentList' => $listId, |
@@ -322,41 +322,41 @@ discard block |
||
322 | 322 | ]); |
323 | 323 | |
324 | 324 | if (null === $listItem) { |
325 | - throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
325 | + throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | return $listItem; |
329 | - } |
|
329 | + } |
|
330 | 330 | |
331 | - private function ensureThereIsNoItemOnPositionOrThrow409( |
|
332 | - int $listId, |
|
333 | - int $position, |
|
334 | - bool $isSticky, |
|
335 | - string $action): void { |
|
336 | - $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
|
331 | + private function ensureThereIsNoItemOnPositionOrThrow409( |
|
332 | + int $listId, |
|
333 | + int $position, |
|
334 | + bool $isSticky, |
|
335 | + string $action): void { |
|
336 | + $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
|
337 | 337 | |
338 | - if (!$existingContentListItem && !$isSticky) { |
|
339 | - return; |
|
340 | - } |
|
338 | + if (!$existingContentListItem && !$isSticky) { |
|
339 | + return; |
|
340 | + } |
|
341 | 341 | |
342 | - if ($existingContentListItem && $existingContentListItem->isSticky()) { |
|
342 | + if ($existingContentListItem && $existingContentListItem->isSticky()) { |
|
343 | 343 | throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
344 | - } |
|
344 | + } |
|
345 | 345 | |
346 | - if ($action === ContentListAction::ACTION_MOVE && $isSticky) { |
|
347 | - throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); |
|
348 | - } |
|
349 | - } |
|
346 | + if ($action === ContentListAction::ACTION_MOVE && $isSticky) { |
|
347 | + throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); |
|
348 | + } |
|
349 | + } |
|
350 | 350 | |
351 | - private function dispatchWebSocketUpdate(ContentListInterface $list, array $updatedItems, string $action): void |
|
352 | - { |
|
351 | + private function dispatchWebSocketUpdate(ContentListInterface $list, array $updatedItems, string $action): void |
|
352 | + { |
|
353 | 353 | $pushData = [ |
354 | - 'contentListId' => $list->getId(), |
|
355 | - 'action' => $action, |
|
356 | - 'items' => $updatedItems, |
|
357 | - 'timestamp' => (new \DateTime())->format('c') |
|
354 | + 'contentListId' => $list->getId(), |
|
355 | + 'action' => $action, |
|
356 | + 'items' => $updatedItems, |
|
357 | + 'timestamp' => (new \DateTime())->format('c') |
|
358 | 358 | ]; |
359 | 359 | |
360 | 360 | $this->pusher->push($pushData, 'content_list_topic'); |
361 | - } |
|
361 | + } |
|
362 | 362 | } |