Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flash from event on processor #680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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