-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathGraphQLExplorerService.php
More file actions
95 lines (80 loc) · 2.74 KB
/
Copy pathGraphQLExplorerService.php
File metadata and controls
95 lines (80 loc) · 2.74 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
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\DataHubBundle\Service\Studio;
use Pimcore\Bundle\DataHubBundle\Service\CheckConsumerPermissionsService;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
* @internal
*/
final readonly class GraphQLExplorerService implements GraphQLExplorerServiceInterface
{
public function __construct(
private RouterInterface $routingService,
private Environment $twig
) {
}
public function getExplorerUrl(string $clientname): string
{
return $this->routingService->generate(
'pimcore_studio_api_data_hub_graphql_explorer', ['clientname' => $clientname]
);
}
public function generateExplorerResponse(string $clientname, array $urlParams = []): Response
{
$graphQLUrl = $this->generateGraphQLUrl($clientname, $urlParams);
$content = $this->renderExplorerTemplate($graphQLUrl);
return $this->createCachedResponse($content);
}
/**
* @param array<string, mixed> $urlParams
*
* @throws RuntimeException If the GraphQL endpoint URL cannot be resolved
*/
private function generateGraphQLUrl(string $clientname, array $urlParams): string
{
$url = $this->routingService->generate('admin_pimcoredatahub_webservice', [
'clientname' => $clientname,
]);
if ($urlParams !== []) {
$url .= '?' . http_build_query($urlParams);
}
return $url;
}
/**
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function renderExplorerTemplate(string $graphQLUrl): string
{
return $this->twig->render('@PimcoreDataHub/Feature/explorer.html.twig', [
'graphQLUrl' => $graphQLUrl,
'tokenHeader' => CheckConsumerPermissionsService::TOKEN_HEADER,
]);
}
private function createCachedResponse(string $content): Response
{
$response = new Response($content, HttpResponseCodes::SUCCESS->value, [
'Content-Type' => 'text/html',
]);
$response->setPublic();
$response->setExpires(new \DateTime('tomorrow'));
return $response;
}
}