/**
  * Transforms an id to an object (menu item).
  *
  * @param string $menuItemId
  *
  * @return MenuItemInterface|null
  *
  * @throws TransformationFailedException if object (menu item) is not found
  */
 public function reverseTransform($menuItemId)
 {
     if (null === $menuItemId) {
         return;
     }
     $menuItem = $this->repository->getOneMenuItemById((int) $menuItemId);
     if (null === $menuItem) {
         throw new TransformationFailedException(sprintf('Menu with id "%s" does not exist!', $menuItem));
     }
     return $menuItem;
 }
Ejemplo n.º 2
0
 /**
  * {@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);
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function has($name, array $options = [])
 {
     $menuItem = $this->repository->getOneMenuItemByName($name);
     return $menuItem instanceof MenuItemInterface;
 }