-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProxyController.php
More file actions
94 lines (80 loc) · 3.3 KB
/
Copy pathProxyController.php
File metadata and controls
94 lines (80 loc) · 3.3 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
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Shopware6\Storefront\Controller;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Omikron\FactFinder\Shopware6\Events\BeforeProxyErrorResponseEvent;
use Omikron\FactFinder\Shopware6\Events\EnrichProxyDataEvent;
use Psr\Http\Client\ClientExceptionInterface;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
#[Route(defaults: ['_routeScope' => ['storefront']])]
class ProxyController extends StorefrontController
{
public function __construct(private readonly Communication $config)
{
}
/**
* @SuppressWarnings(PHPMD.Superglobals)
*/
#[Route(
'/fact-finder/proxy{endpoint}',
name: 'frontend.factfinder.proxy.execute',
requirements: ['endpoint' => '.*'],
defaults: ['csrf_protected' => false],
methods: ['GET', 'POST']
)]
public function execute(
string $endpoint,
Request $request,
ClientBuilder $clientBuilder,
EventDispatcherInterface $eventDispatcher,
): Response {
if (!$this->config->isProxyEnabled()) {
throw new NotFoundHttpException('Proxy is disabled.');
}
$apiKey = $request->headers->get('x-ff-api-key');
if (!$apiKey) {
return new JsonResponse(
['message' => 'UNAUTHORIZED'],
Response::HTTP_UNAUTHORIZED
);
}
$client = $clientBuilder
->withServerUrl($this->config->getServerUrl() . '/')
->withApiKey($apiKey)
->withVersion($this->config->getVersion())
->build();
$query = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY);
$method = $request->getMethod();
$endpoint = trim($endpoint, '/');
try {
switch ($method) {
case Request::METHOD_GET:
$response = $client->request('GET', sprintf('%s?%s', $endpoint, $query));
break;
case Request::METHOD_POST:
$response = $client->request('POST', $endpoint, [
'body' => $request->getContent(),
'headers' => ['Content-Type' => 'application/json'],
]);
break;
default:
throw new \Exception(sprintf('HTTP Method %s is not supported', $method));
}
$event = new EnrichProxyDataEvent(json_decode((string) $response->getBody(), true) ?? []);
$eventDispatcher->dispatch($event);
return new JsonResponse($event->getData());
} catch (ClientExceptionInterface $e) {
$response = new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
$event = new BeforeProxyErrorResponseEvent($response);
$eventDispatcher->dispatch($event);
return $event->getResponse();
}
}
}