vendor/ibexa/shipping/src/bundle/EventSubscriber/ShipmentViewSubscriber.php line 44

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\Data\Shipment\ShipmentTransitionData;
  9. use Ibexa\Bundle\Shipping\Form\Type\Shipment\ShipmentTransitionType;
  10. use Ibexa\Bundle\Shipping\View\ShipmentView;
  11. use Ibexa\Contracts\Shipping\Shipment\ShipmentInterface;
  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 ShipmentViewSubscriber implements EventSubscriberInterface
  20. {
  21. private FormFactoryInterface $formFactory;
  22. private UrlGeneratorInterface $urlGenerator;
  23. public function __construct(
  24. FormFactoryInterface $formFactory,
  25. UrlGeneratorInterface $urlGenerator
  26. ) {
  27. $this->formFactory = $formFactory;
  28. $this->urlGenerator = $urlGenerator;
  29. }
  30. public static function getSubscribedEvents(): array
  31. {
  32. return [
  33. MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
  34. ];
  35. }
  36. public function onPreContentView(PreContentViewEvent $event): void
  37. {
  38. $view = $event->getContentView();
  39. if (!$view instanceof ShipmentView) {
  40. return;
  41. }
  42. $shipment = $view->getShipment();
  43. $view->addParameters([
  44. 'transition_form' => $this->createTransitionForm($shipment)->createView(),
  45. ]);
  46. }
  47. private function createTransitionForm(ShipmentInterface $shipment): FormInterface
  48. {
  49. $actionUrl = $this->urlGenerator->generate(
  50. 'ibexa.shipping.shipment.transition',
  51. [
  52. 'identifier' => $shipment->getIdentifier(),
  53. ]
  54. );
  55. return $this->formFactory->create(
  56. ShipmentTransitionType::class,
  57. new ShipmentTransitionData(),
  58. [
  59. 'action' => $actionUrl,
  60. 'method' => Request::METHOD_POST,
  61. 'shipment' => $shipment,
  62. ]
  63. );
  64. }
  65. }