vendor/ibexa/shipping/src/bundle/EventSubscriber/ShippingMethodListViewSubscriber.php line 50

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\Bundle\Shipping\Form\Type\ShippingMethod\ShippingMethodBulkDeleteType;
  9. use Ibexa\Bundle\Shipping\Form\Type\ShippingMethod\ShippingMethodDefinitionPreCreateType;
  10. use Ibexa\Bundle\Shipping\View\ShippingMethodListView;
  11. use Ibexa\Contracts\ProductCatalog\PermissionResolverInterface;
  12. use Ibexa\Contracts\Shipping\ShippingMethod\Permission\Create;
  13. use Ibexa\Contracts\Shipping\ShippingMethod\Permission\Edit;
  14. use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
  15. use Ibexa\Core\MVC\Symfony\MVCEvents;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Form\FormFactoryInterface;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. final class ShippingMethodListViewSubscriber implements EventSubscriberInterface
  22. {
  23. private PermissionResolverInterface $permissionResolver;
  24. private FormFactoryInterface $formFactory;
  25. private UrlGeneratorInterface $urlGenerator;
  26. public function __construct(
  27. PermissionResolverInterface $permissionResolver,
  28. FormFactoryInterface $formFactory,
  29. UrlGeneratorInterface $urlGenerator
  30. ) {
  31. $this->permissionResolver = $permissionResolver;
  32. $this->formFactory = $formFactory;
  33. $this->urlGenerator = $urlGenerator;
  34. }
  35. public static function getSubscribedEvents(): array
  36. {
  37. return [
  38. MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
  39. ];
  40. }
  41. public function onPreContentView(PreContentViewEvent $event): void
  42. {
  43. $view = $event->getContentView();
  44. if (!$view instanceof ShippingMethodListView) {
  45. return;
  46. }
  47. $view->addParameters([
  48. 'can_create' => $this->canCreate(),
  49. 'can_edit' => $this->canEdit(),
  50. 'bulk_delete_form' => $this->createBulkDeleteForm()->createView(),
  51. 'pre_create_form' => $this->createPreCreateForm()->createView(),
  52. ]);
  53. }
  54. private function canCreate(): bool
  55. {
  56. return $this->permissionResolver->canUser(new Create());
  57. }
  58. private function canEdit(): bool
  59. {
  60. return $this->permissionResolver->canUser(new Edit());
  61. }
  62. private function createBulkDeleteForm(): FormInterface
  63. {
  64. return $this->formFactory->create(ShippingMethodBulkDeleteType::class, null, [
  65. 'method' => Request::METHOD_POST,
  66. 'action' => $this->urlGenerator->generate('ibexa.shipping.shipping_method.bulk_delete'),
  67. ]);
  68. }
  69. private function createPreCreateForm(): FormInterface
  70. {
  71. return $this->formFactory->create(ShippingMethodDefinitionPreCreateType::class, null, [
  72. 'action' => $this->urlGenerator->generate('ibexa.shipping.shipping_method.pre_create'),
  73. 'method' => Request::METHOD_POST,
  74. ]);
  75. }
  76. }