From 987f3e9fca5d74f04e7fa9f5781a1ea0c68c2228 Mon Sep 17 00:00:00 2001 From: IvanJelicSF Date: Wed, 20 Mar 2024 17:21:19 +0100 Subject: [PATCH] Content list webhook --- .../Controller/ContentListItemController.php | 74 ++++++++++++------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php b/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php index 4fcdf8182..b9a850497 100644 --- a/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php +++ b/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php @@ -21,9 +21,9 @@ use SWP\Bundle\ContentBundle\Event\ArticleEvent; use SWP\Bundle\ContentListBundle\Form\Type\ContentListItemsType; use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface; +use SWP\Bundle\CoreBundle\Controller\ContentListController; use SWP\Bundle\CoreBundle\Form\Type\ContentListItemType; use SWP\Bundle\CoreBundle\Model\ContentListInterface; -use SWP\Bundle\CoreBundle\Controller\ContentListController; use SWP\Bundle\CoreBundle\Model\ContentListItemInterface; use SWP\Bundle\CoreBundle\Repository\ArticleRepositoryInterface; use SWP\Bundle\CoreBundle\Repository\ContentListItemRepositoryInterface; @@ -56,21 +56,13 @@ class ContentListItemController extends AbstractController { * @param ContentListServiceInterface $contentListService * @param EventDispatcherInterface $eventDispatcher */ - - public function __construct( - EntityManagerInterface $entityManager, + public function __construct( ContentListItemRepositoryInterface $contentListItemRepository, - ContentListServiceInterface $contentListService, EntityManagerInterface $entityManager, - EventDispatcherInterface $eventDispatcher) { ContentListServiceInterface $contentListService, - $this->contentListItemRepository = $contentListItemRepository; EventDispatcherInterface $eventDispatcher, - $this->entityManager = $entityManager; string $invalidationCacheUrl, - $this->contentListService = $contentListService; string $invalidationToken, - $this->eventDispatcher = $eventDispatcher; ) { $this->contentListItemRepository = $contentListItemRepository; $this->entityManager = $entityManager; @@ -79,7 +71,7 @@ public function __construct( $this->invalidationCacheUrl = $invalidationCacheUrl; $this->invalidationToken = $invalidationToken; } - } + /** * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) @@ -152,7 +144,7 @@ public function updateAction(Request $request, FormFactoryInterface $formFactory } $this->entityManager->flush(); - ContentListController::invalidateCache( + ContentListController::invalidateCache( $this->invalidationCacheUrl, $this->invalidationToken, [ @@ -208,20 +200,40 @@ public function batchUpdateAction( switch ($item->getAction()) { case ContentListAction::ACTION_MOVE: + $updated = false; $contentListItem = $this->findByContentOr404($list, $contentId); - $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky); - - $contentListItem->setPosition($position); - $this->contentListService->toggleStickOnItemPosition($contentListItem, $isSticky, $position); + if ($position !== $contentListItem->getPosition()) { + $this->ensureThereIsNoItemOnPositionOrThrow409( + $listId, + $position, + $isSticky, + ContentListAction::ACTION_MOVE + ); + $contentListItem->setPosition($position); + $updated = true; + } + + if ($isSticky !== $contentListItem->getStickyPosition()) { + $this->contentListService->toggleStickOnItemPosition($contentListItem, $isSticky, $position); + $updated = true; + } + + if ($updated) { + $list->setUpdatedAt(new DateTime('now')); + $this->entityManager->flush(); + } - $list->setUpdatedAt(new DateTime('now')); - $this->entityManager->flush(); $updatedArticles[$contentId] = $contentListItem->getContent(); break; case ContentListAction::ACTION_ADD: - $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky); + $this->ensureThereIsNoItemOnPositionOrThrow409( + $listId, + $position, + $isSticky, + ContentListAction::ACTION_ADD + ); $object = $articleRepository->findOneById($contentId); $contentListItem = $this->contentListService->addArticleToContentList($list, $object, $position, $isSticky); @@ -249,7 +261,7 @@ public function batchUpdateAction( ArticleEvents::POST_UPDATE ), ArticleEvents::POST_UPDATE); } - ContentListController::invalidateCache( + ContentListController::invalidateCache( $this->invalidationCacheUrl, $this->invalidationToken, [ @@ -294,11 +306,23 @@ private function findOr404($listId, $id): ContentListItemInterface { return $listItem; } - private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky): void { - $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); + private function ensureThereIsNoItemOnPositionOrThrow409( + int $listId, + int $position, + bool $isSticky, + string $action): void { + $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); - if (null !== $existingContentListItem && $isSticky && $existingContentListItem->isSticky()) { - throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); - } + if (!$existingContentListItem && !$isSticky) { + return; + } + + if ($existingContentListItem && $existingContentListItem->isSticky()) { + throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); + } + + if ($action === ContentListAction::ACTION_MOVE && $isSticky) { + throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); + } } }