Skip to content

Commit 645ad6e

Browse files
authored
Merge pull request #621 from CommonGateway/fix/aws
Add Call function to twig, small fixes on endpoint and call services
2 parents b08c8d4 + 63309db commit 645ad6e

4 files changed

Lines changed: 68 additions & 6 deletions

File tree

src/Service/CallService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public function call(
348348
'Request failed with error '.$exception,
349349
[
350350
'sourceCall' => $this->sourceCallLogData(['method' => $method, 'url' => $url, 'response' => $response ?? null], $config),
351-
]
351+
],
352352
);
353353

354354
if (empty($response) === false) {
@@ -502,6 +502,8 @@ private function handleEndpointConfigOut(array $config, array $endpointConfigOut
502502
$body = $this->mappingService->mapping($mapping, $body);
503503
$config[$configKey] = \Safe\json_encode($body);
504504

505+
unset($config['headers']['Content-Type'], $config['headers']['Accept'], $config['headers']['accept'], $config['headers']['content-type']);
506+
505507
$config['headers']['content-type'] = 'application/json';
506508
} catch (Exception | LoaderError | SyntaxError $exception) {
507509
$this->callLogger->error("Could not map with mapping {$endpointConfigOut[$configKey]['mapping']} while handling $configKey EndpointConfigOut for a Source. ".$exception->getMessage());

src/Service/EndpointService.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,22 @@ private function getNormalPath(array $parameters): array
551551
$this->logger->debug('EndpointService->getNormalPath(): '.$exception->getMessage());
552552

553553
// Todo: When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length.
554-
array_pop($path);
555-
$combinedArray = array_combine($path, explode('/', $pathRaw));
554+
if (count(explode('/', $pathRaw)) > count($path)) {
555+
$combinedArray = array_combine($path, explode('/', $pathRaw, count($path)));
556+
} else {
557+
array_pop($path);
558+
$combinedArray = array_combine($path, explode('/', $pathRaw));
559+
}
556560
}
557561

558562
if ($combinedArray === false) {
559-
// Todo: When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length.
560-
array_pop($path);
561-
$combinedArray = array_combine($path, explode('/', $pathRaw));
563+
// When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length.
564+
if (count(explode('/', $pathRaw)) > count($path)) {
565+
$combinedArray = array_combine($path, explode('/', $pathRaw, count($path)));
566+
} else {
567+
array_pop($path);
568+
$combinedArray = array_combine($path, explode('/', $pathRaw));
569+
}
562570
}
563571

564572
if ($combinedArray === false) {

src/Twig/CallExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CommonGateway\CoreBundle\Twig;
4+
5+
use Twig\Extension\AbstractExtension;
6+
use Twig\TwigFunction;
7+
8+
class CallExtension extends AbstractExtension
9+
{
10+
public function getFunctions()
11+
{
12+
return [
13+
new TwigFunction('call', [CallRuntime::class, 'call']),
14+
];
15+
16+
}//end getFunctions()
17+
}//end class

src/Twig/CallRuntime.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace CommonGateway\CoreBundle\Twig;
4+
5+
use Adbar\Dot;
6+
use CommonGateway\CoreBundle\Service\CallService;
7+
use CommonGateway\CoreBundle\Service\GatewayResourceService;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Twig\Extension\RuntimeExtensionInterface;
10+
11+
class CallRuntime implements RuntimeExtensionInterface
12+
{
13+
public function __construct(
14+
private readonly CallService $callService,
15+
private readonly GatewayResourceService $resourceService
16+
) {
17+
18+
}//end __construct()
19+
20+
/**
21+
* Call source of given id or reference
22+
*
23+
* @param array $array The array to turn into a dot array.
24+
*
25+
* @return array The dot aray.
26+
*/
27+
public function call(string $sourceId, string $endpoint, string $method = 'GET', array $configuration = []): array
28+
{
29+
$source = $this->resourceService->getSource($sourceId, 'common-gateway/zgw-to-zds-bundle');
30+
31+
$response = $this->callService->call($source, $endpoint, $method, $configuration);
32+
return $this->callService->decodeResponse($source, $response);
33+
34+
}//end call()
35+
}//end class

0 commit comments

Comments
 (0)