Skip to content

Commit

Permalink
Remove usage of FlashBagAwareSessionInterface since it isn't in SF5
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Sep 2, 2024
1 parent 8612498 commit 2cce7c1
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Controller/Action/Admin/ProcessFeedAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

/**
* @psalm-suppress UndefinedClass
Expand All @@ -38,22 +37,30 @@ public function __construct(
) {
$this->commandBus = $commandBus;
$this->urlGenerator = $urlGenerator;
if ($requestStackOrFlashBag instanceof FlashBagInterface) {
$this->flashBag = $requestStackOrFlashBag;
} else {
$session = $requestStackOrFlashBag->getSession();
Assert::isInstanceOf($session, FlashBagAwareSessionInterface::class);
$this->flashBag = $session->getFlashBag();
}
$this->flashBag = $requestStackOrFlashBag;
$this->translator = $translator;
}

public function __invoke(int $id): RedirectResponse
{
$this->commandBus->dispatch(new ProcessFeed($id));

$this->flashBag->add('success', $this->translator->trans('setono_sylius_feed.feed_generation_triggered'));
$this->getFlashBag()->add('success', $this->translator->trans('setono_sylius_feed.feed_generation_triggered'));

return new RedirectResponse($this->urlGenerator->generate('setono_sylius_feed_admin_feed_show', ['id' => $id]));
}

private function getFlashBag(): FlashBagInterface
{
if ($this->flashBag instanceof FlashBagInterface) {
return $this->flashBag;
}

$session = $this->flashBag->getSession();
if ($session instanceof Session) {
return $session->getFlashBag();
}

throw new \RuntimeException('Could not get flash bag');
}
}

0 comments on commit 2cce7c1

Please sign in to comment.