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

FFWEB-2975: Support Fact-Finder redirect campaigns for SSR #187

Merged
merged 2 commits into from
Feb 6, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/Storefront/Controller/ResultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 16 additions & 6 deletions src/Subscriber/CategoryPageResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()));
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Utilites/Ssr/Exception/DetectRedirectCampaignException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception;

class DetectRedirectCampaignException extends \Exception
{
private string $redirectUrl;

public function __construct(string $redirectUrl)
{
parent::__construct("Detect redirection for: $redirectUrl");
$this->redirectUrl = $redirectUrl;
}

public function getRedirectUrl(): string
{
return $this->redirectUrl;
}
}
19 changes: 19 additions & 0 deletions src/Utilites/Ssr/Template/RecordList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,13 +37,20 @@ public function __construct(

/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @throws DetectRedirectCampaignException
*/
public function getContent(
string $paramString,
bool $isNavigationRequest = false
): 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);
}

Expand Down Expand Up @@ -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;
}
}
Loading