<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Bundle\Shipping\EventSubscriber;
use Ibexa\Bundle\Shipping\Form\Data\Shipment\ShipmentTransitionData;
use Ibexa\Bundle\Shipping\Form\Type\Shipment\ShipmentTransitionType;
use Ibexa\Bundle\Shipping\View\ShipmentView;
use Ibexa\Contracts\Shipping\Shipment\ShipmentInterface;
use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
use Ibexa\Core\MVC\Symfony\MVCEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class ShipmentViewSubscriber implements EventSubscriberInterface
{
private FormFactoryInterface $formFactory;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
FormFactoryInterface $formFactory,
UrlGeneratorInterface $urlGenerator
) {
$this->formFactory = $formFactory;
$this->urlGenerator = $urlGenerator;
}
public static function getSubscribedEvents(): array
{
return [
MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
];
}
public function onPreContentView(PreContentViewEvent $event): void
{
$view = $event->getContentView();
if (!$view instanceof ShipmentView) {
return;
}
$shipment = $view->getShipment();
$view->addParameters([
'transition_form' => $this->createTransitionForm($shipment)->createView(),
]);
}
private function createTransitionForm(ShipmentInterface $shipment): FormInterface
{
$actionUrl = $this->urlGenerator->generate(
'ibexa.shipping.shipment.transition',
[
'identifier' => $shipment->getIdentifier(),
]
);
return $this->formFactory->create(
ShipmentTransitionType::class,
new ShipmentTransitionData(),
[
'action' => $actionUrl,
'method' => Request::METHOD_POST,
'shipment' => $shipment,
]
);
}
}