-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSearchResultController.php
More file actions
115 lines (94 loc) · 4.17 KB
/
Copy pathSearchResultController.php
File metadata and controls
115 lines (94 loc) · 4.17 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
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Oxid\Controller;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Version;
use Omikron\FactFinder\Oxid\Event\EnrichProxyDataEvent;
use Omikron\FactFinder\Oxid\Subscriber\EnrichProxyDataEventSubscriber;
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class SearchResultController extends FrontendController
{
public function __construct()
{
parent::__construct();
$this->_sThisTemplate = '@ffwebcomponents/webcomponents/blocks/page/result.html.twig';
}
public function proxy(): void
{
$currentUrl = Registry::getUtilsUrl()->getCurrentUrl();
$endpoint = $this->getEndpoint(Registry::getUtilsUrl()->getCurrentUrl());
$httpMethod = Registry::getUtilsServer()->getServerVar()['REQUEST_METHOD'];
try {
if (!$endpoint) {
throw new \Exception('Endpoint missing');
}
$client = oxNew(ClientBuilder::class)
->withServerUrl($this->getConfigParam('ffServerUrl'))
->withApiKey($this->getConfigParam('ffApiKey'))
->withVersion(Version::NG)
->build();
switch ($httpMethod) {
case 'GET':
$query = (string) $this->removeOxidParams(parse_url($currentUrl, PHP_URL_QUERY));
$response = $client->request('GET', $endpoint . '?' . $query);
break;
case 'POST':
$rawBody = file_get_contents('php://input');
$body = json_decode($rawBody, true) ?: [];
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Invalid JSON in request body');
}
$response = $client->request('POST', $endpoint, [
'body' => json_encode($body),
'headers' => ['Content-Type' => 'application/json'],
]);
break;
default:
throw new \Exception(sprintf('HTTP Method %s is not supported', $httpMethod));
}
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new EnrichProxyDataEventSubscriber());
$event = new EnrichProxyDataEvent(json_decode($response->getBody()->getContents(), true) ?? []);
$eventDispatcher->dispatch($event, EnrichProxyDataEvent::class);
$this->showJsonAndExit(json_encode($event->getData()));
} catch (\Exception $e) {
$this->fallback();
echo json_encode(['error' => $e->getMessage()]);
}
}
protected function showJsonAndExit(string $jsonResponse): void
{
$oUtils = Registry::getUtils();
$oUtils->setHeader('Content-Type: application/json');
header('Content-Type: application/json');
$oUtils->showMessageAndExit($jsonResponse);
}
protected function fallback(): void
{
// this function could be used to implement fallback logic in case of any communication error.
$this->showJsonAndExit('Error: Unable to process the request.');
}
protected function getConfigParam(string $key): string
{
$moduleSettingService = ContainerFactory::getInstance()
->getContainer()
->get(ModuleSettingServiceInterface::class);
return (string) $moduleSettingService->getString($key, 'ffwebcomponents');
}
private function getEndpoint(string $currentUrl): string
{
preg_match('#/([A-Za-z]+\.ff|rest/v[^?]*)#', $currentUrl, $match);
return $match[1] ?? '';
}
private function removeOxidParams(?string $queryString): string
{
if ($queryString === null) {
return '';
}
return preg_replace('/(fnc|cl)=[A-Za-z0-9_]*&?/', '', $queryString);
}
}