/** * {@inheritdoc} */ public function move(MenuItemInterface $sourceItem, MenuItemInterface $parent, int $position = 0) { if (0 === $position) { $this->ensurePositionIsValid($sourceItem, $position); $this->menuItemRepository->persistAsFirstChildOf($sourceItem, $parent); } else { $afterItemPosition = $position; // when moving item from last to middle position if ($afterItemPosition < $sourceItem->getPosition()) { $afterItemPosition -= 1; } $this->ensurePositionIsValid($sourceItem, $afterItemPosition); // find menu item after which source item should be placed $afterItem = $this->menuItemRepository->findChildByParentAndPosition($parent, $afterItemPosition); if (null === $afterItem) { throw new HttpException(400, sprintf('You can not insert menu item at position %d. Position is not valid.', $position)); } $this->menuItemRepository->persistAsNextSiblingOf($sourceItem, $afterItem); } $sourceItem->setPosition($position); $this->objectManager->flush(); }
public function it_throws_exception_if_item_after_moved_item_should_be_placed_does_not_exist(MenuItemInterface $sourceItem, MenuItemInterface $parentItem, MenuItemInterface $afterItem, MenuItemRepositoryInterface $menuItemRepository, ObjectManager $objectManager) { $sourceItem->getPosition()->willReturn(1); $menuItemRepository->findChildByParentAndPosition($parentItem, 2)->willReturn(null); $menuItemRepository->persistAsNextSiblingOf($sourceItem, $afterItem)->shouldNotBeCalled(); $sourceItem->getPosition()->shouldBeCalled(); $sourceItem->setPosition(2)->shouldNotBeCalled(); $objectManager->flush()->shouldNotBeCalled(); $this->shouldThrow(HttpException::class)->duringMove($sourceItem, $parentItem, 2); }