vendor/ibexa/product-catalog/src/bundle/EventSubscriber/CurrencyListViewSubscriber.php line 48

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\ProductCatalog\EventSubscriber;
  8. use Ibexa\Bundle\ProductCatalog\Form\Type\Currency\CurrencyDeleteType;
  9. use Ibexa\Bundle\ProductCatalog\View\CurrencyListView;
  10. use Ibexa\Contracts\ProductCatalog\Permission\Policy\Commerce\AdministrateCurrencies;
  11. use Ibexa\Contracts\ProductCatalog\PermissionResolverInterface;
  12. use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
  13. use Ibexa\Core\MVC\Symfony\MVCEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\Form\FormInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. final class CurrencyListViewSubscriber implements EventSubscriberInterface
  20. {
  21. private FormFactoryInterface $formFactory;
  22. private UrlGeneratorInterface $urlGenerator;
  23. private PermissionResolverInterface $permissionResolver;
  24. public function __construct(
  25. FormFactoryInterface $formFactory,
  26. UrlGeneratorInterface $urlGenerator,
  27. PermissionResolverInterface $permissionResolver
  28. ) {
  29. $this->formFactory = $formFactory;
  30. $this->urlGenerator = $urlGenerator;
  31. $this->permissionResolver = $permissionResolver;
  32. }
  33. public static function getSubscribedEvents(): array
  34. {
  35. return [
  36. MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
  37. ];
  38. }
  39. public function onPreContentView(PreContentViewEvent $event): void
  40. {
  41. $view = $event->getContentView();
  42. if (!$view instanceof CurrencyListView) {
  43. return;
  44. }
  45. if ($this->canAdministrateCurrencies()) {
  46. $view->addParameters([
  47. 'bulk_delete_form' => $this->createBulkDeleteForm()->createView(),
  48. ]);
  49. }
  50. }
  51. private function createBulkDeleteForm(): FormInterface
  52. {
  53. return $this->formFactory->create(CurrencyDeleteType::class, null, [
  54. 'method' => Request::METHOD_POST,
  55. 'action' => $this->urlGenerator->generate('ibexa.product_catalog.currency.bulk_delete'),
  56. ]);
  57. }
  58. private function canAdministrateCurrencies(): bool
  59. {
  60. return $this->permissionResolver->canUser(new AdministrateCurrencies());
  61. }
  62. }