vendor/ibexa/shipping/src/bundle/EventSubscriber/MainMenuSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\Shipping\EventSubscriber;
  8. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  9. use Ibexa\Contracts\ProductCatalog\PermissionResolverInterface;
  10. use Ibexa\Contracts\Shipping\Shipment\Permission\View as ShipmentView;
  11. use Ibexa\Contracts\Shipping\ShippingMethod\Permission\View;
  12. use JMS\TranslationBundle\Model\Message;
  13. use JMS\TranslationBundle\Translation\TranslationContainerInterface;
  14. use Knp\Menu\ItemInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class MainMenuSubscriber implements EventSubscriberInterface, TranslationContainerInterface
  17. {
  18. public const ITEM_COMMERCE = 'main__commerce';
  19. public const ITEM_SHIPPING_METHODS = 'main__commerce__shipping_methods';
  20. public const ITEM_SHIPMENTS = 'main__commerce__shipments';
  21. public const ITEM_COMMERCE_GROUP_SETTINGS = 'main__commerce__group_settings';
  22. private PermissionResolverInterface $permissionResolver;
  23. public function __construct(
  24. PermissionResolverInterface $permissionResolver
  25. ) {
  26. $this->permissionResolver = $permissionResolver;
  27. }
  28. public static function getSubscribedEvents(): array
  29. {
  30. return [
  31. ConfigureMenuEvent::MAIN_MENU => ['onConfigureMainMenu', 5],
  32. ];
  33. }
  34. public function onConfigureMainMenu(ConfigureMenuEvent $event): void
  35. {
  36. $menu = $event->getMenu();
  37. $root = $menu->getChild(self::ITEM_COMMERCE);
  38. if ($root === null) {
  39. $root = $this->addCommerceMenu($menu);
  40. }
  41. if ($this->canViewShipments()) {
  42. $root->addChild(
  43. self::ITEM_SHIPMENTS,
  44. [
  45. 'route' => 'ibexa.shipping.shipment.list',
  46. 'extras' => [
  47. 'orderNumber' => 30,
  48. 'routes' => [
  49. [
  50. 'pattern' => '~^ibexa\.shipping\.shipment\.~',
  51. ],
  52. ],
  53. ],
  54. ]
  55. );
  56. }
  57. if ($this->canViewShippingMethods()) {
  58. $groupSettingsRoot = $this->getGroupSettingsRoot($root);
  59. $groupSettingsRoot->addChild(
  60. self::ITEM_SHIPPING_METHODS,
  61. [
  62. 'route' => 'ibexa.shipping.shipping_method.list',
  63. 'extras' => [
  64. 'orderNumber' => 130,
  65. 'routes' => [
  66. [
  67. 'pattern' => '~^ibexa\.shipping\.shipping_method\.~',
  68. ],
  69. ],
  70. ],
  71. ]
  72. );
  73. }
  74. }
  75. public static function getTranslationMessages(): array
  76. {
  77. return [
  78. (new Message(self::ITEM_COMMERCE, 'ibexa_menu'))->setDesc('Commerce'),
  79. (new Message(self::ITEM_SHIPPING_METHODS, 'ibexa_menu'))->setDesc('Shipping methods'),
  80. (new Message(self::ITEM_SHIPMENTS, 'ibexa_menu'))->setDesc('Shipments'),
  81. (new Message(self::ITEM_COMMERCE_GROUP_SETTINGS, 'ibexa_menu'))->setDesc('Settings'),
  82. ];
  83. }
  84. private function addCommerceMenu(ItemInterface $menu): ItemInterface
  85. {
  86. return $menu->addChild(
  87. self::ITEM_COMMERCE,
  88. [
  89. 'attributes' => [
  90. 'data-tooltip-placement' => 'right',
  91. 'data-tooltip-extra-class' => 'ibexa-tooltip--navigation',
  92. ],
  93. 'extras' => [
  94. 'icon' => 'cart-full',
  95. 'orderNumber' => 100,
  96. ],
  97. ]
  98. );
  99. }
  100. private function canViewShippingMethods(): bool
  101. {
  102. return $this->permissionResolver->canUser(new View());
  103. }
  104. private function canViewShipments(): bool
  105. {
  106. return $this->permissionResolver->canUser(new ShipmentView());
  107. }
  108. private function getGroupSettingsRoot(ItemInterface $menu): ItemInterface
  109. {
  110. $groupSettingsRoot = $menu->getChild(self::ITEM_COMMERCE_GROUP_SETTINGS);
  111. if ($groupSettingsRoot === null) {
  112. $groupSettingsRoot = $menu->addChild(
  113. self::ITEM_COMMERCE_GROUP_SETTINGS,
  114. [
  115. 'extras' => [
  116. 'orderNumber' => 100,
  117. ],
  118. ],
  119. );
  120. }
  121. return $groupSettingsRoot;
  122. }
  123. }