diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c1ba68..55530c3a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## Unreleased +### Fix +- Support Fact-Finder redirect campaigns for SSR + ## [v4.3.2] - 2024.01.26 ### Fix - Filter empty attributes for ff-communication component diff --git a/src/Storefront/Controller/ResultController.php b/src/Storefront/Controller/ResultController.php index c07f20ac..c57d33fa 100644 --- a/src/Storefront/Controller/ResultController.php +++ b/src/Storefront/Controller/ResultController.php @@ -5,12 +5,14 @@ namespace Omikron\FactFinder\Shopware6\Storefront\Controller; use Omikron\FactFinder\Shopware6\Config\Communication; +use Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception\DetectRedirectCampaignException; use Omikron\FactFinder\Shopware6\Utilites\Ssr\SearchAdapter; use Omikron\FactFinder\Shopware6\Utilites\Ssr\Template\Engine; use Omikron\FactFinder\Shopware6\Utilites\Ssr\Template\RecordList; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Shopware\Storefront\Controller\StorefrontController; use Shopware\Storefront\Page\GenericPageLoader; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -56,11 +58,16 @@ public function result( $context->getSalesChannelId(), $response->getContent(), ); - $response->setContent( - $recordList->getContent( - $this->parseQueryString((string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY)) - ) - ); + + try { + $response->setContent( + $recordList->getContent( + $this->parseQueryString((string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY)) + ) + ); + } catch (DetectRedirectCampaignException $exception) { + return new RedirectResponse($exception->getRedirectUrl()); + } return $response; } diff --git a/src/Subscriber/CategoryPageResponseSubscriber.php b/src/Subscriber/CategoryPageResponseSubscriber.php index 313080a1..5a49d8e0 100644 --- a/src/Subscriber/CategoryPageResponseSubscriber.php +++ b/src/Subscriber/CategoryPageResponseSubscriber.php @@ -5,6 +5,7 @@ namespace Omikron\FactFinder\Shopware6\Subscriber; use Omikron\FactFinder\Shopware6\Config\Communication; +use Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception\DetectRedirectCampaignException; use Omikron\FactFinder\Shopware6\Utilites\Ssr\Field\CategoryPath; use Omikron\FactFinder\Shopware6\Utilites\Ssr\SearchAdapter; use Omikron\FactFinder\Shopware6\Utilites\Ssr\Template\Engine; @@ -15,8 +16,12 @@ use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Event\BeforeSendResponseEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class CategoryPageResponseSubscriber implements EventSubscriberInterface { private bool $httpCacheEnabled; @@ -71,12 +76,17 @@ public function onPageRendered(BeforeSendResponseEvent $event): void $request->attributes->get('sw-sales-channel-id'), $response->getContent(), ); - $response->setContent( - $recordList->getContent( - $this->getParamsString($categoryPath), - true - ) - ); + + try { + $response->setContent( + $recordList->getContent( + $this->getParamsString($categoryPath), + true + ) + ); + } catch (DetectRedirectCampaignException $exception) { + $event->setResponse(new RedirectResponse($exception->getRedirectUrl())); + } } /** diff --git a/src/Utilites/Ssr/Exception/DetectRedirectCampaignException.php b/src/Utilites/Ssr/Exception/DetectRedirectCampaignException.php new file mode 100644 index 00000000..890fb34e --- /dev/null +++ b/src/Utilites/Ssr/Exception/DetectRedirectCampaignException.php @@ -0,0 +1,21 @@ +redirectUrl = $redirectUrl; + } + + public function getRedirectUrl(): string + { + return $this->redirectUrl; + } +} diff --git a/src/Utilites/Ssr/Template/RecordList.php b/src/Utilites/Ssr/Template/RecordList.php index 36071695..4bff43a6 100644 --- a/src/Utilites/Ssr/Template/RecordList.php +++ b/src/Utilites/Ssr/Template/RecordList.php @@ -4,6 +4,7 @@ namespace Omikron\FactFinder\Shopware6\Utilites\Ssr\Template; +use Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception\DetectRedirectCampaignException; use Omikron\FactFinder\Shopware6\Utilites\Ssr\SearchAdapter; use Symfony\Component\HttpFoundation\Request; @@ -36,6 +37,8 @@ public function __construct( /** * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @throws DetectRedirectCampaignException */ public function getContent( string $paramString, @@ -43,6 +46,11 @@ public function getContent( ): string { $results = $this->searchResults($paramString, $isNavigationRequest); + // Support redirect campaigns for SSR + if ($this->getRedirectCampaign($results)) { + throw new DetectRedirectCampaignException($this->getRedirectCampaign($results)); + } + return $this->renderResults($results, $paramString); } @@ -112,4 +120,15 @@ private function setTemplateString(): void $this->template = $match[0] ?? ''; } + + private function getRedirectCampaign(array $result): ?string + { + if (!empty($result['campaigns'])) { + $campaign = array_search('REDIRECT', array_column($result['campaigns'], 'flavour')); + + return $result['campaigns'][$campaign]['target']['destination'] ?? null; + } + + return null; + } }