From 9aa7fcc6630aad57f6d99effbacb195a977c79f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 20 Jun 2023 14:53:20 +0200 Subject: [PATCH] Add flash from event on processor --- .../Resources/config/services/dispatcher.xml | 1 + .../EventDispatcher/OperationEventHandler.php | 20 ++++++++++-------- .../Symfony/Session/Flash/FlashHelper.php | 21 +++++++++++++++++++ .../OperationEventHandlerSpec.php | 21 ++++++++++++++++--- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Bundle/Resources/config/services/dispatcher.xml b/src/Bundle/Resources/config/services/dispatcher.xml index 32d5d0e5e..65d518cd8 100644 --- a/src/Bundle/Resources/config/services/dispatcher.xml +++ b/src/Bundle/Resources/config/services/dispatcher.xml @@ -20,6 +20,7 @@ + diff --git a/src/Component/Symfony/EventDispatcher/OperationEventHandler.php b/src/Component/Symfony/EventDispatcher/OperationEventHandler.php index 0b7349809..26ac2c3c5 100644 --- a/src/Component/Symfony/EventDispatcher/OperationEventHandler.php +++ b/src/Component/Symfony/EventDispatcher/OperationEventHandler.php @@ -17,13 +17,16 @@ use Sylius\Component\Resource\Context\Option\RequestOption; use Sylius\Component\Resource\Metadata\HttpOperation; use Sylius\Component\Resource\Symfony\Routing\RedirectHandlerInterface; +use Sylius\Component\Resource\Symfony\Session\Flash\FlashHelperInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; final class OperationEventHandler implements OperationEventHandlerInterface { - public function __construct(private RedirectHandlerInterface $redirectHandler) - { + public function __construct( + private RedirectHandlerInterface $redirectHandler, + private FlashHelperInterface $flashHelper, + ) { } public function handlePreProcessEvent( @@ -37,17 +40,16 @@ public function handlePreProcessEvent( $request = $context->get(RequestOption::class)?->request(); - if ( - 'html' === $request?->getRequestFormat() && - null !== $operationEventResponse = $event->getResponse() - ) { - return $operationEventResponse; - } - if ('html' !== $request?->getRequestFormat()) { throw new HttpException($event->getErrorCode(), $event->getMessage()); } + $this->flashHelper->addFlashFromEvent($event, $context); + + if (null !== $operationEventResponse = $event->getResponse()) { + return $operationEventResponse; + } + $operation = $event->getOperation(); if ($operation instanceof HttpOperation && null !== $request) { diff --git a/src/Component/Symfony/Session/Flash/FlashHelper.php b/src/Component/Symfony/Session/Flash/FlashHelper.php index ef9ce161f..859edccf9 100644 --- a/src/Component/Symfony/Session/Flash/FlashHelper.php +++ b/src/Component/Symfony/Session/Flash/FlashHelper.php @@ -101,6 +101,27 @@ private function addFlash(string $message, string $type, Context $context): void $flashBag->add($type, $message); } + private function buildMessage(Operation $operation, string $type): string + { + $resource = $operation->getResource(); + Assert::notNull($resource); + + $specifyKey = sprintf('%s.%s.%s', $resource->getApplicationName() ?? '', $resource->getName() ?? '', $operation->getShortName() ?? ''); + $defaultKey = sprintf('sylius.resource.%s', $operation->getShortName() ?? ''); + + $parameters = $this->getTranslationParameters($operation); + + if (!$this->translator instanceof TranslatorBagInterface) { + return $this->translator->trans($defaultKey, $parameters, 'flashes'); + } + + if ($this->translator->getCatalogue()->has($specifyKey, 'flashes')) { + return $this->translator->trans($specifyKey, $parameters, 'flashes'); + } + + return $this->translator->trans($defaultKey, $parameters, 'flashes'); + } + private function getTranslationParameters(Operation $operation): array { $resource = $operation->getResource(); diff --git a/src/Component/spec/Symfony/EventDispatcher/OperationEventHandlerSpec.php b/src/Component/spec/Symfony/EventDispatcher/OperationEventHandlerSpec.php index 6ad747f78..6b5fdeae0 100644 --- a/src/Component/spec/Symfony/EventDispatcher/OperationEventHandlerSpec.php +++ b/src/Component/spec/Symfony/EventDispatcher/OperationEventHandlerSpec.php @@ -21,6 +21,7 @@ use Sylius\Component\Resource\Symfony\EventDispatcher\OperationEvent; use Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventHandler; use Sylius\Component\Resource\Symfony\Routing\RedirectHandlerInterface; +use Sylius\Component\Resource\Symfony\Session\Flash\FlashHelperInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -28,9 +29,11 @@ final class OperationEventHandlerSpec extends ObjectBehavior { - function let(RedirectHandlerInterface $redirectHandler): void - { - $this->beConstructedWith($redirectHandler); + function let( + RedirectHandlerInterface $redirectHandler, + FlashHelperInterface $flashHelper, + ): void { + $this->beConstructedWith($redirectHandler, $flashHelper); } function it_is_initializable(): void @@ -53,6 +56,7 @@ function it_throws_an_http_exception_when_pre_process_event_is_stopped_and_reque function it_returns_response_from_pre_process_event_when_it_has_one_and_request_format_is_html( Request $request, Response $response, + FlashHelperInterface $flashHelper, ): void { $event = new OperationEvent(); $event->stop(message: 'What the hell is going on?', errorCode: 666); @@ -62,6 +66,8 @@ function it_returns_response_from_pre_process_event_when_it_has_one_and_request_ $request->getRequestFormat()->willReturn('html'); + $flashHelper->addFlashFromEvent($event, $context)->shouldBeCalled(); + $this->handlePreProcessEvent($event, $context)->shouldReturn($response); } @@ -87,6 +93,7 @@ function it_can_redirect_to_resource_when_pre_process_event_is_stopped_and_has_n \stdClass $data, RedirectHandlerInterface $redirectHandler, RedirectResponse $response, + FlashHelperInterface $flashHelper, ): void { $event = new OperationEvent($data); $event->stop(message: 'What the hell is going on?', errorCode: 666); @@ -99,6 +106,8 @@ function it_can_redirect_to_resource_when_pre_process_event_is_stopped_and_has_n $request->getRequestFormat()->willReturn('html'); + $flashHelper->addFlashFromEvent($event, $context)->shouldBeCalled(); + $redirectHandler->redirectToResource($data, $operation, $request)->willReturn($response)->shouldBeCalled(); $this->handlePreProcessEvent($event, $context)->shouldHaveType(RedirectResponse::class); @@ -109,6 +118,7 @@ function it_can_redirect_to_operation_when_pre_process_event_is_stopped_and_has_ \stdClass $data, RedirectHandlerInterface $redirectHandler, RedirectResponse $response, + FlashHelperInterface $flashHelper, ): void { $event = new OperationEvent($data); $event->stop(message: 'What the hell is going on?', errorCode: 666); @@ -121,6 +131,8 @@ function it_can_redirect_to_operation_when_pre_process_event_is_stopped_and_has_ $request->getRequestFormat()->willReturn('html'); + $flashHelper->addFlashFromEvent($event, $context)->shouldBeCalled(); + $redirectHandler->redirectToOperation($data, $operation, $request, 'index')->willReturn($response)->shouldBeCalled(); $this->handlePreProcessEvent($event, $context, 'index')->shouldHaveType(RedirectResponse::class); @@ -130,6 +142,7 @@ function it_returns_null_when_pre_process_event_is_stopped_and_has_no_response_a Request $request, \stdClass $data, Operation $operation, + FlashHelperInterface $flashHelper, ): void { $event = new OperationEvent($data); $event->stop(message: 'What the hell is going on?', errorCode: 666); @@ -139,6 +152,8 @@ function it_returns_null_when_pre_process_event_is_stopped_and_has_no_response_a $request->getRequestFormat()->willReturn('html'); + $flashHelper->addFlashFromEvent($event, $context)->shouldBeCalled(); + $this->handlePreProcessEvent($event, $context)->shouldReturn(null); }