Skip to content

Commit

Permalink
Split logic in ensureThereIsNoItemOnPositionOrThrow409 function for a…
Browse files Browse the repository at this point in the history
…dd and move action
  • Loading branch information
zkabic committed Aug 2, 2023
1 parent a0b37ff commit a7115fc
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ public function batchUpdateAction(
$contentListItem = $this->findByContentOr404($list, $contentId);

if ($position !== $contentListItem->getPosition()) {
$this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky);
$this->ensureThereIsNoItemOnPositionOrThrow409(
$listId,
$position,
$isSticky,
ContentListAction::ACTION_MOVE
);
$contentListItem->setPosition($position);
$updated = true;
}
Expand All @@ -206,7 +211,12 @@ public function batchUpdateAction(

break;
case ContentListAction::ACTION_ADD:
$this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position);
$this->ensureThereIsNoItemOnPositionOrThrow409(
$listId,
$position,
$isSticky,
ContentListAction::ACTION_ADD
);

$object = $articleRepository->findOneById($contentId);
$contentListItem = $this->contentListService->addArticleToContentList($list, $object, $position, $isSticky);
Expand Down Expand Up @@ -269,15 +279,23 @@ private function findOr404($listId, $id): ContentListItemInterface {
return $listItem;
}

private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky = false): 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 (!$existingContentListItem && !$isSticky) {
return;
}

if ($isSticky || ($existingContentListItem && $existingContentListItem->isSticky())) {
throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.');
}
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.');
}
}
}

0 comments on commit a7115fc

Please sign in to comment.