-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathResultController.php
More file actions
96 lines (83 loc) · 3.26 KB
/
Copy pathResultController.php
File metadata and controls
96 lines (83 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Shopware6\Storefront\Controller;
use Omikron\FactFinder\Communication\Client\ClientException;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception\DetectRedirectException;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\SearchAdapter;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Template\Engine;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Template\RecordList;
use Psr\Log\LoggerInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
#[Route(defaults: ['_routeScope' => ['storefront']])]
class ResultController extends StorefrontController
{
public function __construct(
private readonly Communication $config,
private readonly GenericPageLoader $pageLoader,
private readonly LoggerInterface $factfinderLogger,
) {
}
#[Route('/factfinder/result', name: 'frontend.factfinder.result', methods: ['GET'])]
public function result(
Request $request,
SalesChannelContext $context,
SearchAdapter $searchAdapter,
Engine $handlebars,
EventDispatcherInterface $eventDispatcher,
): Response {
$page = $this->pageLoader->load($request, $context);
$response = $this->renderStorefront('@Parent/storefront/page/factfinder/result.html.twig', ['page' => $page]);
if ($this->config->isSsrActive() === false) {
return $response;
}
$recordList = new RecordList(
$request,
$handlebars,
$searchAdapter,
$this->config,
$eventDispatcher,
$context->getSalesChannelId(),
$response->getContent(),
);
try {
$response->setContent(
$recordList->getContent(
$this->parseQueryString($request->getQueryString() ?? '', $request)
)
);
} catch (DetectRedirectException $e) {
return new RedirectResponse($e->getRedirectUrl());
} catch (ClientException $e) {
$this->factfinderLogger->error("{$e->getMessage()}. Check logs for more information.");
}
return $response;
}
private function parseQueryString(string $queryString, Request $request): string
{
if ($queryString === '') {
return '';
}
$queryParams = explode('&', $queryString);
$queryParams[] = sprintf('sid=%s', $request->cookies->get('ffwebc_sid', ''));
$result = array_reduce(
$queryParams,
function (string $carry, string $queryParam) {
$result = explode('=', $queryParam);
return sprintf('%s&%s=%s', $carry, $result[0], htmlspecialchars($result[1]));
},
''
);
return substr($result, 1);
}
}