-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCategoryPageResponseSubscriber.php
More file actions
151 lines (128 loc) · 4.84 KB
/
Copy pathCategoryPageResponseSubscriber.php
File metadata and controls
151 lines (128 loc) · 4.84 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Shopware6\Subscriber;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Exception\DetectRedirectException;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Field\CategoryPath;
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\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CategoryPageResponseSubscriber implements EventSubscriberInterface
{
private bool $httpCacheEnabled;
private EntityRepository $categoryRepository;
private Communication $config;
private SearchAdapter $searchAdapter;
private Engine $handlebars;
private CategoryPath $categoryPath;
private EventDispatcherInterface $eventDispatcher;
public function __construct(
bool $httpCacheEnabled,
EntityRepository $categoryRepository,
Communication $config,
SearchAdapter $searchAdapter,
Engine $handlebars,
CategoryPath $categoryPath,
EventDispatcherInterface $eventDispatcher,
) {
$this->httpCacheEnabled = $httpCacheEnabled;
$this->categoryRepository = $categoryRepository;
$this->config = $config;
$this->searchAdapter = $searchAdapter;
$this->handlebars = $handlebars;
$this->categoryPath = $categoryPath;
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onPageRendered',
];
}
public function onPageRendered(ResponseEvent $event): void
{
$request = $event->getRequest();
$response = $event->getResponse();
$categoryPath = $this->getCategoryPath($request);
if ($response->getContent() === false) {
return;
}
if ($this->config->isSsrActive() === false
|| $request->isXmlHttpRequest()
|| $categoryPath === ''
) {
$response->setContent(str_replace('{FF_SEARCH_RESULT}', '{}', $response->getContent()));
return;
}
$recordList = new RecordList(
$request,
$this->handlebars,
$this->searchAdapter,
$this->config,
$this->eventDispatcher,
$request->attributes->get('sw-sales-channel-id'),
$response->getContent(),
);
try {
$response->setContent(
$recordList->getContent(
$this->getParamsString($categoryPath),
true
)
);
} catch (DetectRedirectException $exception) {
$event->setResponse(new RedirectResponse($exception->getRedirectUrl()));
}
}
/**
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function getParamsString(string $categoryPath): string
{
$params = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY);
if ($params === '') {
return $categoryPath;
}
return sprintf('%s&%s', $params, $categoryPath);
}
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
private function getCategoryPath(Request $request): string
{
$categoryId = $this->getCategoryId($request);
if ($categoryId === '') {
return '';
}
if ($this->httpCacheEnabled === false) {
return $request->attributes->get('categoryPath', '');
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $categoryId));
$category = $this->categoryRepository->search($criteria, Context::createDefaultContext())->first();
return $category !== null ? $this->categoryPath->getSsrValue($category) : '';
}
private function getCategoryId(Request $request): string
{
$uri = $request->attributes->get('resolved-uri');
$start = '/navigation/';
$pos = strpos($uri ?? '', $start);
if ($pos !== 0) {
return '';
}
return trim(substr($uri, strlen($start), strlen($uri)), '/');
}
}