| @@ 26-68 (lines=43) @@ | ||
| 23 | /** |
|
| 24 | * @author Beñat Espiña <[email protected]> |
|
| 25 | */ |
|
| 26 | class AddMenuItemHandler |
|
| 27 | { |
|
| 28 | private $repository; |
|
| 29 | ||
| 30 | public function __construct(MenuRepository $repository) |
|
| 31 | { |
|
| 32 | $this->repository = $repository; |
|
| 33 | } |
|
| 34 | ||
| 35 | public function __invoke(AddMenuItemCommand $command) |
|
| 36 | { |
|
| 37 | $menu = $this->repository->menuOfId( |
|
| 38 | MenuId::generate( |
|
| 39 | $command->menuId() |
|
| 40 | ) |
|
| 41 | ); |
|
| 42 | if (!$menu instanceof Menu) { |
|
| 43 | throw new MenuDoesNotExistException(); |
|
| 44 | } |
|
| 45 | $locale = new Locale( |
|
| 46 | $command->locale() |
|
| 47 | ); |
|
| 48 | $translation = $menu->{$command->locale()}(); |
|
| 49 | ||
| 50 | $translation->addItem( |
|
| 51 | new MenuItemLink( |
|
| 52 | $command->menuItemLabel(), |
|
| 53 | $command->menuItemUrl() |
|
| 54 | ), |
|
| 55 | null !== $command->menuItemParentId() |
|
| 56 | ? MenuItemId::generate($command->menuItemParentId()) |
|
| 57 | : null |
|
| 58 | ); |
|
| 59 | ||
| 60 | try { |
|
| 61 | $menu->removeTranslation($locale); |
|
| 62 | } catch (TranslationDoesNotExistException $exception) { |
|
| 63 | } |
|
| 64 | ||
| 65 | $menu->addTranslation($translation); |
|
| 66 | $this->repository->persist($menu); |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||
| @@ 24-67 (lines=44) @@ | ||
| 21 | /** |
|
| 22 | * @author Beñat Espiña <[email protected]> |
|
| 23 | */ |
|
| 24 | class ChangeParentMenuItemHandler |
|
| 25 | { |
|
| 26 | private $repository; |
|
| 27 | ||
| 28 | public function __construct(MenuRepository $repository) |
|
| 29 | { |
|
| 30 | $this->repository = $repository; |
|
| 31 | } |
|
| 32 | ||
| 33 | public function __invoke(ChangeParentMenuItemCommand $command) |
|
| 34 | { |
|
| 35 | $menu = $this->repository->menuOfId( |
|
| 36 | MenuId::generate( |
|
| 37 | $command->menuId() |
|
| 38 | ) |
|
| 39 | ); |
|
| 40 | if (!$menu instanceof Menu) { |
|
| 41 | throw new MenuDoesNotExistException(); |
|
| 42 | } |
|
| 43 | $translation = $menu->{$command->locale()}(); |
|
| 44 | ||
| 45 | $menuItemParent = $translation->item( |
|
| 46 | null !== $command->menuItemParentId() |
|
| 47 | ? MenuItemId::generate($command->menuItemParentId()) |
|
| 48 | : null |
|
| 49 | ); |
|
| 50 | ||
| 51 | $translation->item( |
|
| 52 | MenuItemId::generate( |
|
| 53 | $command->menuItemId() |
|
| 54 | ) |
|
| 55 | )->changeParent( |
|
| 56 | $menuItemParent->id() |
|
| 57 | ); |
|
| 58 | ||
| 59 | $menu->removeTranslation( |
|
| 60 | new Locale( |
|
| 61 | $command->locale() |
|
| 62 | ) |
|
| 63 | ); |
|
| 64 | $menu->addTranslation($translation); |
|
| 65 | $this->repository->persist($menu); |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||