Skip to content

Commit

Permalink
Do not throw if channel does not exist on current request
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Aug 7, 2023
1 parent 7e192b0 commit d4a498d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Sylius\\Bundle\\TaxonomyBundle\\Form\\Type\\TaxonTranslationType",
"Sylius\\Bundle\\UiBundle\\Menu\\Event\\MenuBuilderEvent",
"Sylius\\Component\\Channel\\Context\\ChannelContextInterface",
"Sylius\\Component\\Channel\\Context\\ChannelNotFoundException",
"Sylius\\Component\\Channel\\Model\\ChannelAwareInterface",
"Sylius\\Component\\Channel\\Model\\ChannelInterface",
"Sylius\\Component\\Channel\\Model\\ChannelsAwareInterface",
Expand Down
9 changes: 8 additions & 1 deletion src/EventListener/ControllerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Persistence\ObjectManager;
use Setono\SyliusRedirectPlugin\Resolver\RedirectionPathResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -45,9 +46,15 @@ public static function getSubscribedEvents(): array
public function onKernelController(ControllerEvent $event): void
{
$request = $event->getRequest();
$channel = null;

try {
$channel = $this->channelContext->getChannel();
} catch (ChannelNotFoundException $e) {
}
$redirectionPath = $this->redirectionPathResolver->resolveFromRequest(
$request,
$this->channelContext->getChannel()
$channel
);

if ($redirectionPath->isEmpty()) {
Expand Down
9 changes: 8 additions & 1 deletion src/EventListener/NotFoundSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Setono\MainRequestTrait\MainRequestTrait;
use Setono\SyliusRedirectPlugin\Resolver\RedirectionPathResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
Expand Down Expand Up @@ -57,11 +58,17 @@ public function onKernelException(ExceptionEvent $event): void
if (!$throwable instanceof HttpException || $throwable->getStatusCode() !== Response::HTTP_NOT_FOUND) {
return;
}
$channel = null;

try {
$channel = $this->channelContext->getChannel();
} catch (ChannelNotFoundException $e) {
}

$request = $event->getRequest();
$redirectionPath = $this->redirectionPathResolver->resolveFromRequest(
$request,
$this->channelContext->getChannel(),
$channel,
true
);

Expand Down

0 comments on commit d4a498d

Please sign in to comment.