Skip to content

Commit

Permalink
Add flash from event on processor
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jun 21, 2023
1 parent 59d1017 commit 2ff863b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services/dispatcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<service id="sylius.event_handler.operation" class="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventHandler">
<argument type="service" id="sylius.routing.redirect_handler" />
<argument type="service" id="sylius.helper.flash" />
</service>
<service id="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventHandlerInterface" alias="sylius.event_handler.operation" />
</services>
Expand Down
20 changes: 11 additions & 9 deletions src/Component/Symfony/EventDispatcher/OperationEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/Component/Symfony/Session/Flash/FlashHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
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;
use Symfony\Component\HttpKernel\Exception\HttpException;

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
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit 2ff863b

Please sign in to comment.