| @@ -45,41 +45,41 @@ 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 | - public function __construct( | |
| 60 | - ContentListItemRepositoryInterface $contentListItemRepository, | |
| 61 | - EntityManagerInterface $entityManager, | |
| 62 | - ContentListServiceInterface $contentListService, | |
| 63 | - EventDispatcherInterface $eventDispatcher, | |
| 64 | - string $invalidationCacheUrl, | |
| 65 | - string $invalidationToken | |
| 66 | -  ) { | |
| 67 | - $this->contentListItemRepository = $contentListItemRepository; | |
| 68 | - $this->entityManager = $entityManager; | |
| 69 | - $this->contentListService = $contentListService; | |
| 70 | - $this->eventDispatcher = $eventDispatcher; | |
| 71 | - $this->invalidationCacheUrl = $invalidationCacheUrl; | |
| 72 | - $this->invalidationToken = $invalidationToken; | |
| 73 | - } | |
| 74 | - | |
| 75 | - | |
| 76 | - /** | |
| 77 | -   * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) | |
| 78 | - */ | |
| 79 | -  public function listAction(Request $request, int $id): ResourcesListResponseInterface { | |
| 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 | + public function __construct( | |
| 60 | + ContentListItemRepositoryInterface $contentListItemRepository, | |
| 61 | + EntityManagerInterface $entityManager, | |
| 62 | + ContentListServiceInterface $contentListService, | |
| 63 | + EventDispatcherInterface $eventDispatcher, | |
| 64 | + string $invalidationCacheUrl, | |
| 65 | + string $invalidationToken | |
| 66 | +    ) { | |
| 67 | + $this->contentListItemRepository = $contentListItemRepository; | |
| 68 | + $this->entityManager = $entityManager; | |
| 69 | + $this->contentListService = $contentListService; | |
| 70 | + $this->eventDispatcher = $eventDispatcher; | |
| 71 | + $this->invalidationCacheUrl = $invalidationCacheUrl; | |
| 72 | + $this->invalidationToken = $invalidationToken; | |
| 73 | + } | |
| 74 | + | |
| 75 | + | |
| 76 | + /** | |
| 77 | +     * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) | |
| 78 | + */ | |
| 79 | +    public function listAction(Request $request, int $id): ResourcesListResponseInterface { | |
| 80 | 80 |      $sort = $request->query->all('sorting'); | 
| 81 | 81 |      if (empty($sort)) { | 
| 82 | - $sort = ['sticky' => 'desc']; | |
| 82 | + $sort = ['sticky' => 'desc']; | |
| 83 | 83 | } | 
| 84 | 84 | |
| 85 | 85 | $items = $this->contentListItemRepository->getPaginatedByCriteria( | 
| @@ -112,20 +112,20 @@ discard block | ||
| 112 | 112 | ); | 
| 113 | 113 | |
| 114 | 114 | return new ResourcesListResponse($items, $responseContext); | 
| 115 | - } | |
| 115 | + } | |
| 116 | 116 | |
| 117 | - /** | |
| 118 | -   * @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+"}) | |
| 119 | - */ | |
| 120 | -  public function getAction($listId, $id) { | |
| 117 | + /** | |
| 118 | +     * @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+"}) | |
| 119 | + */ | |
| 120 | +    public function getAction($listId, $id) { | |
| 121 | 121 | return new SingleResourceResponse($this->findOr404($listId, $id)); | 
| 122 | - } | |
| 122 | + } | |
| 123 | 123 | |
| 124 | - /** | |
| 125 | -   * @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+"}) | |
| 126 | - */ | |
| 127 | - public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, | |
| 128 | -                                       $id): SingleResourceResponseInterface { | |
| 124 | + /** | |
| 125 | +     * @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+"}) | |
| 126 | + */ | |
| 127 | + public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, | |
| 128 | +                                        $id): SingleResourceResponseInterface { | |
| 129 | 129 | $contentListItem = $this->findOr404($listId, $id); | 
| 130 | 130 | $form = $formFactory->createNamed( | 
| 131 | 131 | '', | 
| @@ -137,13 +137,13 @@ discard block | ||
| 137 | 137 | $form->handleRequest($request); | 
| 138 | 138 | |
| 139 | 139 |      if ($form->isSubmitted() && $form->isValid()) { | 
| 140 | - $contentListItem->getContentList()->setUpdatedAt(new DateTime()); | |
| 140 | + $contentListItem->getContentList()->setUpdatedAt(new DateTime()); | |
| 141 | 141 | |
| 142 | -      if (null !== $contentListItem->getStickyPosition()) { | |
| 142 | +        if (null !== $contentListItem->getStickyPosition()) { | |
| 143 | 143 | $contentListItem->setPosition($contentListItem->getStickyPosition()); | 
| 144 | - } | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - $this->entityManager->flush(); | |
| 146 | + $this->entityManager->flush(); | |
| 147 | 147 | ContentListController::invalidateCache( | 
| 148 | 148 | $this->invalidationCacheUrl, | 
| 149 | 149 | $this->invalidationToken, | 
| @@ -155,46 +155,46 @@ discard block | ||
| 155 | 155 | ] | 
| 156 | 156 | ); | 
| 157 | 157 | |
| 158 | - return new SingleResourceResponse($contentListItem); | |
| 158 | + return new SingleResourceResponse($contentListItem); | |
| 159 | 159 | } | 
| 160 | 160 | |
| 161 | 161 | return new SingleResourceResponse($form, new ResponseContext(400)); | 
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | -   * @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+"}) | |
| 166 | - */ | |
| 167 | - public function batchUpdateAction( | |
| 168 | - Request $request, | |
| 169 | - FormFactoryInterface $formFactory, | |
| 170 | - ContentListRepositoryInterface $contentListRepository, | |
| 171 | - ArticleRepositoryInterface $articleRepository, | |
| 172 | - EventDispatcherInterface $eventDispatcher, | |
| 173 | - int $listId | |
| 174 | -  ): SingleResourceResponseInterface { | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | +     * @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+"}) | |
| 166 | + */ | |
| 167 | + public function batchUpdateAction( | |
| 168 | + Request $request, | |
| 169 | + FormFactoryInterface $formFactory, | |
| 170 | + ContentListRepositoryInterface $contentListRepository, | |
| 171 | + ArticleRepositoryInterface $articleRepository, | |
| 172 | + EventDispatcherInterface $eventDispatcher, | |
| 173 | + int $listId | |
| 174 | +    ): SingleResourceResponseInterface { | |
| 175 | 175 | /** @var ContentListInterface $list */ | 
| 176 | 176 | $list = $contentListRepository->findOneBy(['id' => $listId]); | 
| 177 | 177 |      if (null === $list) { | 
| 178 | -      throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); | |
| 178 | +        throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); | |
| 179 | 179 | } | 
| 180 | 180 | |
| 181 | 181 |      $form = $formFactory->createNamed('', ContentListItemsType::class, [], ['method' => $request->getMethod()]); | 
| 182 | 182 | |
| 183 | 183 | $form->handleRequest($request); | 
| 184 | 184 |      if ($form->isSubmitted() && $form->isValid()) { | 
| 185 | - $data = $form->getData(); | |
| 186 | -      $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); | |
| 187 | -      $updatedAt->setTimezone(new DateTimeZone('UTC')); | |
| 188 | - $listUpdatedAt = $list->getUpdatedAt(); | |
| 189 | -      $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); | |
| 190 | -      if ($updatedAt < $listUpdatedAt) { | |
| 185 | + $data = $form->getData(); | |
| 186 | +        $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); | |
| 187 | +        $updatedAt->setTimezone(new DateTimeZone('UTC')); | |
| 188 | + $listUpdatedAt = $list->getUpdatedAt(); | |
| 189 | +        $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); | |
| 190 | +        if ($updatedAt < $listUpdatedAt) { | |
| 191 | 191 |          throw new ConflictHttpException('List was already updated'); | 
| 192 | - } | |
| 192 | + } | |
| 193 | 193 | |
| 194 | - $updatedArticles = []; | |
| 195 | - $updatedItemsInvalidateCache = []; | |
| 196 | - /** @var ContentListAction $item */ | |
| 197 | -      foreach ($data['items'] as $item) { | |
| 194 | + $updatedArticles = []; | |
| 195 | + $updatedItemsInvalidateCache = []; | |
| 196 | + /** @var ContentListAction $item */ | |
| 197 | +        foreach ($data['items'] as $item) { | |
| 198 | 198 | $position = $item->getPosition(); | 
| 199 | 199 | $isSticky = $item->isSticky(); | 
| 200 | 200 | $contentId = $item->getContentId(); | 
| @@ -208,7 +208,7 @@ discard block | ||
| 208 | 208 | |
| 209 | 209 | |
| 210 | 210 |          switch ($item->getAction()) { | 
| 211 | - case ContentListAction::ACTION_MOVE: | |
| 211 | + case ContentListAction::ACTION_MOVE: | |
| 212 | 212 | $updated = false; | 
| 213 | 213 | $contentListItem = $this->findByContentOr404($list, $contentId); | 
| 214 | 214 | |
| @@ -236,7 +236,7 @@ discard block | ||
| 236 | 236 | $updatedArticles[$contentId] = $contentListItem->getContent(); | 
| 237 | 237 | |
| 238 | 238 | break; | 
| 239 | - case ContentListAction::ACTION_ADD: | |
| 239 | + case ContentListAction::ACTION_ADD: | |
| 240 | 240 | $this->ensureThereIsNoItemOnPositionOrThrow409( | 
| 241 | 241 | $listId, | 
| 242 | 242 | $position, | 
| @@ -250,7 +250,7 @@ discard block | ||
| 250 | 250 | $updatedArticles[$contentId] = $contentListItem->getContent(); | 
| 251 | 251 | |
| 252 | 252 | break; | 
| 253 | - case ContentListAction::ACTION_DELETE: | |
| 253 | + case ContentListAction::ACTION_DELETE: | |
| 254 | 254 | $contentListItem = $this->findByContentOr404($list, $contentId); | 
| 255 | 255 | $this->entityManager->remove($contentListItem); | 
| 256 | 256 |              $list->setUpdatedAt(new DateTime('now')); | 
| @@ -259,17 +259,17 @@ discard block | ||
| 259 | 259 | |
| 260 | 260 | break; | 
| 261 | 261 | } | 
| 262 | - } | |
| 262 | + } | |
| 263 | 263 | |
| 264 | - $this->contentListService->repositionStickyItems($list); | |
| 264 | + $this->contentListService->repositionStickyItems($list); | |
| 265 | 265 | |
| 266 | -      foreach ($updatedArticles as $updatedArticle) { | |
| 266 | +        foreach ($updatedArticles as $updatedArticle) { | |
| 267 | 267 | $eventDispatcher->dispatch(new ArticleEvent( | 
| 268 | 268 | $updatedArticle, | 
| 269 | 269 | $updatedArticle->getPackage(), | 
| 270 | 270 | ArticleEvents::POST_UPDATE | 
| 271 | 271 | ), ArticleEvents::POST_UPDATE); | 
| 272 | - } | |
| 272 | + } | |
| 273 | 273 | ContentListController::invalidateCache( | 
| 274 | 274 | $this->invalidationCacheUrl, | 
| 275 | 275 | $this->invalidationToken, | 
| @@ -282,16 +282,16 @@ discard block | ||
| 282 | 282 | ] | 
| 283 | 283 | ); | 
| 284 | 284 | |
| 285 | - // Dispatch WebSocket update | |
| 286 | - $this->dispatchWebSocketUpdate($list, $updatedItemsInvalidateCache, 'BATCH-UPDATE'); | |
| 285 | + // Dispatch WebSocket update | |
| 286 | + $this->dispatchWebSocketUpdate($list, $updatedItemsInvalidateCache, 'BATCH-UPDATE'); | |
| 287 | 287 | |
| 288 | - return new SingleResourceResponse($list, new ResponseContext(201)); | |
| 288 | + return new SingleResourceResponse($list, new ResponseContext(201)); | |
| 289 | 289 | } | 
| 290 | 290 | |
| 291 | 291 | return new SingleResourceResponse($form, new ResponseContext(400)); | 
| 292 | - } | |
| 292 | + } | |
| 293 | 293 | |
| 294 | -  private function findByContentOr404($listId, $contentId): ContentListItemInterface { | |
| 294 | +    private function findByContentOr404($listId, $contentId): ContentListItemInterface { | |
| 295 | 295 | /** @var ContentListItemInterface $listItem */ | 
| 296 | 296 | $listItem = $this->contentListItemRepository->findOneBy([ | 
| 297 | 297 | 'contentList' => $listId, | 
| @@ -299,13 +299,13 @@ discard block | ||
| 299 | 299 | ]); | 
| 300 | 300 | |
| 301 | 301 |      if (null === $listItem) { | 
| 302 | -      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)); | |
| 302 | +        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)); | |
| 303 | 303 | } | 
| 304 | 304 | |
| 305 | 305 | return $listItem; | 
| 306 | - } | |
| 306 | + } | |
| 307 | 307 | |
| 308 | -  private function findOr404($listId, $id): ContentListItemInterface { | |
| 308 | +    private function findOr404($listId, $id): ContentListItemInterface { | |
| 309 | 309 | /** @var ContentListItemInterface $listItem */ | 
| 310 | 310 | $listItem = $this->contentListItemRepository->findOneBy([ | 
| 311 | 311 | 'contentList' => $listId, | 
| @@ -313,43 +313,43 @@ discard block | ||
| 313 | 313 | ]); | 
| 314 | 314 | |
| 315 | 315 |      if (null === $listItem) { | 
| 316 | -      throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); | |
| 316 | +        throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); | |
| 317 | 317 | } | 
| 318 | 318 | |
| 319 | 319 | return $listItem; | 
| 320 | - } | |
| 320 | + } | |
| 321 | 321 | |
| 322 | - private function ensureThereIsNoItemOnPositionOrThrow409( | |
| 323 | - int $listId, | |
| 324 | - int $position, | |
| 325 | - bool $isSticky, | |
| 326 | -      string $action): void { | |
| 327 | - $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); | |
| 322 | + private function ensureThereIsNoItemOnPositionOrThrow409( | |
| 323 | + int $listId, | |
| 324 | + int $position, | |
| 325 | + bool $isSticky, | |
| 326 | +        string $action): void { | |
| 327 | + $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); | |
| 328 | 328 | |
| 329 | -      if (!$existingContentListItem && !$isSticky) { | |
| 330 | - return; | |
| 331 | - } | |
| 329 | +        if (!$existingContentListItem && !$isSticky) { | |
| 330 | + return; | |
| 331 | + } | |
| 332 | 332 | |
| 333 | -      if ($existingContentListItem && $existingContentListItem->isSticky()) { | |
| 333 | +        if ($existingContentListItem && $existingContentListItem->isSticky()) { | |
| 334 | 334 |          throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); | 
| 335 | - } | |
| 335 | + } | |
| 336 | 336 | |
| 337 | -      if ($action === ContentListAction::ACTION_MOVE && $isSticky) { | |
| 338 | -          throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); | |
| 339 | - } | |
| 340 | - } | |
| 337 | +        if ($action === ContentListAction::ACTION_MOVE && $isSticky) { | |
| 338 | +            throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); | |
| 339 | + } | |
| 340 | + } | |
| 341 | 341 | |
| 342 | - private function dispatchWebSocketUpdate(ContentListInterface $list, array $updatedItems, string $action): void | |
| 343 | -  { | |
| 342 | + private function dispatchWebSocketUpdate(ContentListInterface $list, array $updatedItems, string $action): void | |
| 343 | +    { | |
| 344 | 344 | $pushData = [ | 
| 345 | - 'contentListId' => $list->getId(), | |
| 346 | - 'action' => $action, | |
| 347 | - 'items' => $updatedItems, | |
| 348 | -      'timestamp' => (new \DateTime())->format('c') | |
| 345 | + 'contentListId' => $list->getId(), | |
| 346 | + 'action' => $action, | |
| 347 | + 'items' => $updatedItems, | |
| 348 | +        'timestamp' => (new \DateTime())->format('c') | |
| 349 | 349 | ]; | 
| 350 | 350 | |
| 351 | 351 | // Get the WebSocket pusher from the container | 
| 352 | 352 |      $pusher = $this->container->get('gos_web_socket.pusher.amqp'); | 
| 353 | 353 | $pusher->push($pushData, 'content_list.update'); | 
| 354 | - } | |
| 354 | + } | |
| 355 | 355 | } |