@@ -45,49 +45,49 @@ discard block |
||
| 45 | 45 | use FOS\RestBundle\Controller\Annotations\Route; |
| 46 | 46 | |
| 47 | 47 | class ContentListItemController extends AbstractController { |
| 48 | - private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 49 | - private EntityManagerInterface $entityManager; |
|
| 50 | - private ContentListServiceInterface $contentListService; |
|
| 51 | - private EventDispatcherInterface $eventDispatcher; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 55 | - * @param EntityManagerInterface $entityManager |
|
| 56 | - * @param ContentListServiceInterface $contentListService |
|
| 57 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 58 | - */ |
|
| 59 | - |
|
| 60 | - public function __construct( |
|
| 61 | - EntityManagerInterface $entityManager, |
|
| 62 | - ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 63 | - ContentListServiceInterface $contentListService, |
|
| 64 | - EntityManagerInterface $entityManager, |
|
| 65 | - EventDispatcherInterface $eventDispatcher) { |
|
| 66 | - ContentListServiceInterface $contentListService, |
|
| 48 | + private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 49 | + private EntityManagerInterface $entityManager; |
|
| 50 | + private ContentListServiceInterface $contentListService; |
|
| 51 | + private EventDispatcherInterface $eventDispatcher; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 55 | + * @param EntityManagerInterface $entityManager |
|
| 56 | + * @param ContentListServiceInterface $contentListService |
|
| 57 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 58 | + */ |
|
| 59 | + |
|
| 60 | + public function __construct( |
|
| 61 | + EntityManagerInterface $entityManager, |
|
| 62 | + ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 63 | + ContentListServiceInterface $contentListService, |
|
| 64 | + EntityManagerInterface $entityManager, |
|
| 65 | + EventDispatcherInterface $eventDispatcher) { |
|
| 66 | + ContentListServiceInterface $contentListService, |
|
| 67 | 67 | $this->contentListItemRepository = $contentListItemRepository; |
| 68 | - EventDispatcherInterface $eventDispatcher, |
|
| 68 | + EventDispatcherInterface $eventDispatcher, |
|
| 69 | 69 | $this->entityManager = $entityManager; |
| 70 | - string $invalidationCacheUrl, |
|
| 70 | + string $invalidationCacheUrl, |
|
| 71 | 71 | $this->contentListService = $contentListService; |
| 72 | - string $invalidationToken, |
|
| 72 | + string $invalidationToken, |
|
| 73 | 73 | $this->eventDispatcher = $eventDispatcher; |
| 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 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 86 | - */ |
|
| 87 | - public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 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 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 86 | + */ |
|
| 87 | + public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 88 | 88 | $sort = $request->query->all('sorting'); |
| 89 | 89 | if (empty($sort)) { |
| 90 | - $sort = ['sticky' => 'desc']; |
|
| 90 | + $sort = ['sticky' => 'desc']; |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | $items = $this->contentListItemRepository->getPaginatedByCriteria( |
@@ -120,20 +120,20 @@ discard block |
||
| 120 | 120 | ); |
| 121 | 121 | |
| 122 | 122 | return new ResourcesListResponse($items, $responseContext); |
| 123 | - } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @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+"}) |
|
| 127 | - */ |
|
| 128 | - public function getAction($listId, $id) { |
|
| 125 | + /** |
|
| 126 | + * @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+"}) |
|
| 127 | + */ |
|
| 128 | + public function getAction($listId, $id) { |
|
| 129 | 129 | return new SingleResourceResponse($this->findOr404($listId, $id)); |
| 130 | - } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * @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+"}) |
|
| 134 | - */ |
|
| 135 | - public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 136 | - $id): SingleResourceResponseInterface { |
|
| 132 | + /** |
|
| 133 | + * @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+"}) |
|
| 134 | + */ |
|
| 135 | + public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 136 | + $id): SingleResourceResponseInterface { |
|
| 137 | 137 | $contentListItem = $this->findOr404($listId, $id); |
| 138 | 138 | $form = $formFactory->createNamed( |
| 139 | 139 | '', |
@@ -145,14 +145,14 @@ discard block |
||
| 145 | 145 | $form->handleRequest($request); |
| 146 | 146 | |
| 147 | 147 | if ($form->isSubmitted() && $form->isValid()) { |
| 148 | - $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 148 | + $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 149 | 149 | |
| 150 | - if (null !== $contentListItem->getStickyPosition()) { |
|
| 150 | + if (null !== $contentListItem->getStickyPosition()) { |
|
| 151 | 151 | $contentListItem->setPosition($contentListItem->getStickyPosition()); |
| 152 | - } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $this->entityManager->flush(); |
|
| 155 | - ContentListController::invalidateCache( |
|
| 154 | + $this->entityManager->flush(); |
|
| 155 | + ContentListController::invalidateCache( |
|
| 156 | 156 | $this->invalidationCacheUrl, |
| 157 | 157 | $this->invalidationToken, |
| 158 | 158 | [ |
@@ -163,51 +163,51 @@ discard block |
||
| 163 | 163 | ] |
| 164 | 164 | ); |
| 165 | 165 | |
| 166 | - return new SingleResourceResponse($contentListItem); |
|
| 166 | + return new SingleResourceResponse($contentListItem); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @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+"}) |
|
| 174 | - */ |
|
| 175 | - public function batchUpdateAction( |
|
| 176 | - Request $request, |
|
| 177 | - FormFactoryInterface $formFactory, |
|
| 178 | - ContentListRepositoryInterface $contentListRepository, |
|
| 179 | - ArticleRepositoryInterface $articleRepository, |
|
| 180 | - EventDispatcherInterface $eventDispatcher, |
|
| 181 | - int $listId |
|
| 182 | - ): SingleResourceResponseInterface { |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @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+"}) |
|
| 174 | + */ |
|
| 175 | + public function batchUpdateAction( |
|
| 176 | + Request $request, |
|
| 177 | + FormFactoryInterface $formFactory, |
|
| 178 | + ContentListRepositoryInterface $contentListRepository, |
|
| 179 | + ArticleRepositoryInterface $articleRepository, |
|
| 180 | + EventDispatcherInterface $eventDispatcher, |
|
| 181 | + int $listId |
|
| 182 | + ): SingleResourceResponseInterface { |
|
| 183 | 183 | /** @var ContentListInterface $list */ |
| 184 | 184 | $list = $contentListRepository->findOneBy(['id' => $listId]); |
| 185 | 185 | if (null === $list) { |
| 186 | - throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 186 | + throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | $form = $formFactory->createNamed('', ContentListItemsType::class, [], ['method' => $request->getMethod()]); |
| 190 | 190 | |
| 191 | 191 | $form->handleRequest($request); |
| 192 | 192 | if ($form->isSubmitted() && $form->isValid()) { |
| 193 | - $data = $form->getData(); |
|
| 194 | - $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 195 | - $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 196 | - $listUpdatedAt = $list->getUpdatedAt(); |
|
| 197 | - $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 198 | - if ($updatedAt < $listUpdatedAt) { |
|
| 193 | + $data = $form->getData(); |
|
| 194 | + $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 195 | + $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 196 | + $listUpdatedAt = $list->getUpdatedAt(); |
|
| 197 | + $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 198 | + if ($updatedAt < $listUpdatedAt) { |
|
| 199 | 199 | throw new ConflictHttpException('List was already updated'); |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - $updatedArticles = []; |
|
| 203 | - /** @var ContentListAction $item */ |
|
| 204 | - foreach ($data['items'] as $item) { |
|
| 202 | + $updatedArticles = []; |
|
| 203 | + /** @var ContentListAction $item */ |
|
| 204 | + foreach ($data['items'] as $item) { |
|
| 205 | 205 | $position = $item->getPosition(); |
| 206 | 206 | $isSticky = $item->isSticky(); |
| 207 | 207 | $contentId = $item->getContentId(); |
| 208 | 208 | |
| 209 | 209 | switch ($item->getAction()) { |
| 210 | - case ContentListAction::ACTION_MOVE: |
|
| 210 | + case ContentListAction::ACTION_MOVE: |
|
| 211 | 211 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 212 | 212 | |
| 213 | 213 | $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky); |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 221 | 221 | |
| 222 | 222 | break; |
| 223 | - case ContentListAction::ACTION_ADD: |
|
| 223 | + case ContentListAction::ACTION_ADD: |
|
| 224 | 224 | $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky); |
| 225 | 225 | |
| 226 | 226 | $object = $articleRepository->findOneById($contentId); |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 230 | 230 | |
| 231 | 231 | break; |
| 232 | - case ContentListAction::ACTION_DELETE: |
|
| 232 | + case ContentListAction::ACTION_DELETE: |
|
| 233 | 233 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 234 | 234 | $this->entityManager->remove($contentListItem); |
| 235 | 235 | $list->setUpdatedAt(new DateTime('now')); |
@@ -238,18 +238,18 @@ discard block |
||
| 238 | 238 | |
| 239 | 239 | break; |
| 240 | 240 | } |
| 241 | - } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - $this->contentListService->repositionStickyItems($list); |
|
| 243 | + $this->contentListService->repositionStickyItems($list); |
|
| 244 | 244 | |
| 245 | - foreach ($updatedArticles as $updatedArticle) { |
|
| 245 | + foreach ($updatedArticles as $updatedArticle) { |
|
| 246 | 246 | $eventDispatcher->dispatch(new ArticleEvent( |
| 247 | 247 | $updatedArticle, |
| 248 | 248 | $updatedArticle->getPackage(), |
| 249 | 249 | ArticleEvents::POST_UPDATE |
| 250 | 250 | ), ArticleEvents::POST_UPDATE); |
| 251 | - } |
|
| 252 | - ContentListController::invalidateCache( |
|
| 251 | + } |
|
| 252 | + ContentListController::invalidateCache( |
|
| 253 | 253 | $this->invalidationCacheUrl, |
| 254 | 254 | $this->invalidationToken, |
| 255 | 255 | [ |
@@ -260,13 +260,13 @@ discard block |
||
| 260 | 260 | ] |
| 261 | 261 | ); |
| 262 | 262 | |
| 263 | - return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 263 | + return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 267 | - } |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 269 | + private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 270 | 270 | /** @var ContentListItemInterface $listItem */ |
| 271 | 271 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 272 | 272 | 'contentList' => $listId, |
@@ -274,13 +274,13 @@ discard block |
||
| 274 | 274 | ]); |
| 275 | 275 | |
| 276 | 276 | if (null === $listItem) { |
| 277 | - 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)); |
|
| 277 | + 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)); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | return $listItem; |
| 281 | - } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - private function findOr404($listId, $id): ContentListItemInterface { |
|
| 283 | + private function findOr404($listId, $id): ContentListItemInterface { |
|
| 284 | 284 | /** @var ContentListItemInterface $listItem */ |
| 285 | 285 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 286 | 286 | 'contentList' => $listId, |
@@ -288,17 +288,17 @@ discard block |
||
| 288 | 288 | ]); |
| 289 | 289 | |
| 290 | 290 | if (null === $listItem) { |
| 291 | - throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 291 | + throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | return $listItem; |
| 295 | - } |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky): void { |
|
| 297 | + private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky): void { |
|
| 298 | 298 | $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
| 299 | 299 | |
| 300 | 300 | if (null !== $existingContentListItem && $isSticky && $existingContentListItem->isSticky()) { |
| 301 | - throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
|
| 301 | + throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
|
| 302 | + } |
|
| 302 | 303 | } |
| 303 | - } |
|
| 304 | 304 | } |