@@ -44,35 +44,35 @@ discard block |
||
| 44 | 44 | use FOS\RestBundle\Controller\Annotations\Route; |
| 45 | 45 | |
| 46 | 46 | class ContentListItemController extends AbstractController { |
| 47 | - private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 48 | - private EntityManagerInterface $entityManager; |
|
| 49 | - private ContentListServiceInterface $contentListService; |
|
| 50 | - private EventDispatcherInterface $eventDispatcher; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 54 | - * @param EntityManagerInterface $entityManager |
|
| 55 | - * @param ContentListServiceInterface $contentListService |
|
| 56 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 57 | - */ |
|
| 58 | - public function __construct(ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 59 | - EntityManagerInterface $entityManager, |
|
| 60 | - ContentListServiceInterface $contentListService, |
|
| 61 | - EventDispatcherInterface $eventDispatcher) { |
|
| 47 | + private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 48 | + private EntityManagerInterface $entityManager; |
|
| 49 | + private ContentListServiceInterface $contentListService; |
|
| 50 | + private EventDispatcherInterface $eventDispatcher; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 54 | + * @param EntityManagerInterface $entityManager |
|
| 55 | + * @param ContentListServiceInterface $contentListService |
|
| 56 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 57 | + */ |
|
| 58 | + public function __construct(ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 59 | + EntityManagerInterface $entityManager, |
|
| 60 | + ContentListServiceInterface $contentListService, |
|
| 61 | + EventDispatcherInterface $eventDispatcher) { |
|
| 62 | 62 | $this->contentListItemRepository = $contentListItemRepository; |
| 63 | 63 | $this->entityManager = $entityManager; |
| 64 | 64 | $this->contentListService = $contentListService; |
| 65 | 65 | $this->eventDispatcher = $eventDispatcher; |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 71 | - */ |
|
| 72 | - public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 69 | + /** |
|
| 70 | + * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 71 | + */ |
|
| 72 | + public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 73 | 73 | $sort = $request->query->all('sorting'); |
| 74 | 74 | if (empty($sort)) { |
| 75 | - $sort = ['sticky' => 'desc']; |
|
| 75 | + $sort = ['sticky' => 'desc']; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $items = $this->contentListItemRepository->getPaginatedByCriteria( |
@@ -105,20 +105,20 @@ discard block |
||
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | 107 | return new ResourcesListResponse($items, $responseContext); |
| 108 | - } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * @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+"}) |
|
| 112 | - */ |
|
| 113 | - public function getAction($listId, $id) { |
|
| 110 | + /** |
|
| 111 | + * @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+"}) |
|
| 112 | + */ |
|
| 113 | + public function getAction($listId, $id) { |
|
| 114 | 114 | return new SingleResourceResponse($this->findOr404($listId, $id)); |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * @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+"}) |
|
| 119 | - */ |
|
| 120 | - public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 121 | - $id): SingleResourceResponseInterface { |
|
| 117 | + /** |
|
| 118 | + * @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+"}) |
|
| 119 | + */ |
|
| 120 | + public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 121 | + $id): SingleResourceResponseInterface { |
|
| 122 | 122 | $contentListItem = $this->findOr404($listId, $id); |
| 123 | 123 | $form = $formFactory->createNamed( |
| 124 | 124 | '', |
@@ -130,59 +130,59 @@ discard block |
||
| 130 | 130 | $form->handleRequest($request); |
| 131 | 131 | |
| 132 | 132 | if ($form->isSubmitted() && $form->isValid()) { |
| 133 | - $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 133 | + $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 134 | 134 | |
| 135 | - if (null !== $contentListItem->getStickyPosition()) { |
|
| 135 | + if (null !== $contentListItem->getStickyPosition()) { |
|
| 136 | 136 | $contentListItem->setPosition($contentListItem->getStickyPosition()); |
| 137 | - } |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - $this->entityManager->flush(); |
|
| 139 | + $this->entityManager->flush(); |
|
| 140 | 140 | |
| 141 | - return new SingleResourceResponse($contentListItem); |
|
| 141 | + return new SingleResourceResponse($contentListItem); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @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+"}) |
|
| 149 | - */ |
|
| 150 | - public function batchUpdateAction( |
|
| 151 | - Request $request, |
|
| 152 | - FormFactoryInterface $formFactory, |
|
| 153 | - ContentListRepositoryInterface $contentListRepository, |
|
| 154 | - ArticleRepositoryInterface $articleRepository, |
|
| 155 | - EventDispatcherInterface $eventDispatcher, |
|
| 156 | - int $listId |
|
| 157 | - ): SingleResourceResponseInterface { |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @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+"}) |
|
| 149 | + */ |
|
| 150 | + public function batchUpdateAction( |
|
| 151 | + Request $request, |
|
| 152 | + FormFactoryInterface $formFactory, |
|
| 153 | + ContentListRepositoryInterface $contentListRepository, |
|
| 154 | + ArticleRepositoryInterface $articleRepository, |
|
| 155 | + EventDispatcherInterface $eventDispatcher, |
|
| 156 | + int $listId |
|
| 157 | + ): SingleResourceResponseInterface { |
|
| 158 | 158 | /** @var ContentListInterface $list */ |
| 159 | 159 | $list = $contentListRepository->findOneBy(['id' => $listId]); |
| 160 | 160 | if (null === $list) { |
| 161 | - throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 161 | + throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | $form = $formFactory->createNamed('', ContentListItemsType::class, [], ['method' => $request->getMethod()]); |
| 165 | 165 | |
| 166 | 166 | $form->handleRequest($request); |
| 167 | 167 | if ($form->isSubmitted() && $form->isValid()) { |
| 168 | - $data = $form->getData(); |
|
| 169 | - $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 170 | - $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 171 | - $listUpdatedAt = $list->getUpdatedAt(); |
|
| 172 | - $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 173 | - if ($updatedAt < $listUpdatedAt) { |
|
| 168 | + $data = $form->getData(); |
|
| 169 | + $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 170 | + $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 171 | + $listUpdatedAt = $list->getUpdatedAt(); |
|
| 172 | + $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 173 | + if ($updatedAt < $listUpdatedAt) { |
|
| 174 | 174 | throw new ConflictHttpException('List was already updated'); |
| 175 | - } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - $updatedArticles = []; |
|
| 178 | - /** @var ContentListAction $item */ |
|
| 179 | - foreach ($data['items'] as $item) { |
|
| 177 | + $updatedArticles = []; |
|
| 178 | + /** @var ContentListAction $item */ |
|
| 179 | + foreach ($data['items'] as $item) { |
|
| 180 | 180 | $position = $item->getPosition(); |
| 181 | 181 | $isSticky = $item->isSticky(); |
| 182 | 182 | $contentId = $item->getContentId(); |
| 183 | 183 | |
| 184 | 184 | switch ($item->getAction()) { |
| 185 | - case ContentListAction::ACTION_MOVE: |
|
| 185 | + case ContentListAction::ACTION_MOVE: |
|
| 186 | 186 | $updated = false; |
| 187 | 187 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 188 | 188 | |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 206 | 206 | |
| 207 | 207 | break; |
| 208 | - case ContentListAction::ACTION_ADD: |
|
| 208 | + case ContentListAction::ACTION_ADD: |
|
| 209 | 209 | $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position); |
| 210 | 210 | |
| 211 | 211 | $object = $articleRepository->findOneById($contentId); |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 215 | 215 | |
| 216 | 216 | break; |
| 217 | - case ContentListAction::ACTION_DELETE: |
|
| 217 | + case ContentListAction::ACTION_DELETE: |
|
| 218 | 218 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 219 | 219 | $this->entityManager->remove($contentListItem); |
| 220 | 220 | $list->setUpdatedAt(new DateTime('now')); |
@@ -223,25 +223,25 @@ discard block |
||
| 223 | 223 | |
| 224 | 224 | break; |
| 225 | 225 | } |
| 226 | - } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - $this->contentListService->repositionStickyItems($list); |
|
| 228 | + $this->contentListService->repositionStickyItems($list); |
|
| 229 | 229 | |
| 230 | - foreach ($updatedArticles as $updatedArticle) { |
|
| 230 | + foreach ($updatedArticles as $updatedArticle) { |
|
| 231 | 231 | $eventDispatcher->dispatch(new ArticleEvent( |
| 232 | 232 | $updatedArticle, |
| 233 | 233 | $updatedArticle->getPackage(), |
| 234 | 234 | ArticleEvents::POST_UPDATE |
| 235 | 235 | ), ArticleEvents::POST_UPDATE); |
| 236 | - } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 238 | + return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 242 | - } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 244 | + private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 245 | 245 | /** @var ContentListItemInterface $listItem */ |
| 246 | 246 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 247 | 247 | 'contentList' => $listId, |
@@ -249,13 +249,13 @@ discard block |
||
| 249 | 249 | ]); |
| 250 | 250 | |
| 251 | 251 | if (null === $listItem) { |
| 252 | - 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)); |
|
| 252 | + 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)); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | return $listItem; |
| 256 | - } |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - private function findOr404($listId, $id): ContentListItemInterface { |
|
| 258 | + private function findOr404($listId, $id): ContentListItemInterface { |
|
| 259 | 259 | /** @var ContentListItemInterface $listItem */ |
| 260 | 260 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 261 | 261 | 'contentList' => $listId, |
@@ -263,21 +263,21 @@ discard block |
||
| 263 | 263 | ]); |
| 264 | 264 | |
| 265 | 265 | if (null === $listItem) { |
| 266 | - throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 266 | + throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | return $listItem; |
| 270 | - } |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky = false): void { |
|
| 272 | + private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky = false): void { |
|
| 273 | 273 | $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
| 274 | 274 | |
| 275 | - if (!$existingContentListItem && !$isSticky) { |
|
| 276 | - return; |
|
| 277 | - } |
|
| 275 | + if (!$existingContentListItem && !$isSticky) { |
|
| 276 | + return; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | 279 | if ($isSticky || ($existingContentListItem && $existingContentListItem->isSticky())) { |
| 280 | - throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
|
| 280 | + throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
|
| 281 | + } |
|
| 281 | 282 | } |
| 282 | - } |
|
| 283 | 283 | } |