Skip to content

Commit 3966410

Browse files
hnccoxmvdhoek1
authored andcommitted
feat: implement transient caching for cURL responses
1 parent 2f4882a commit 3966410

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

src/PrefillGravityForms/Controllers/BaseController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,15 @@ protected function getCurlHeaders(string $doelBinding = ''): array
309309
return array_filter($headers);
310310
}
311311

312-
protected function handleCurl(array $args): array
312+
protected function handleCurl(array $args, ?string $transientKey = null): array
313313
{
314+
if (is_string($transientKey) && 0 < strlen(trim($transientKey))) {
315+
$cachedResponse = get_transient($transientKey);
316+
if (is_array($cachedResponse) && [] !== $cachedResponse) {
317+
return $cachedResponse;
318+
}
319+
}
320+
314321
$curl = curl_init();
315322

316323
try {
@@ -338,10 +345,14 @@ protected function handleCurl(array $args): array
338345

339346
$decoded = json_decode($output, true);
340347

341-
if (! $decoded || json_last_error() !== JSON_ERROR_NONE) {
348+
if (! is_array($decoded) || [] === $decoded || json_last_error() !== JSON_ERROR_NONE) {
342349
throw new Exception('Something went wrong with decoding of the JSON output.', 500);
343350
}
344351

352+
if (is_string($transientKey) && 0 < strlen(trim($transientKey))) {
353+
set_transient($transientKey, $decoded, HOUR_IN_SECONDS);
354+
}
355+
345356
return $decoded;
346357
} catch (Exception $e) {
347358
return [

src/PrefillGravityForms/Controllers/EnableUController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7373
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
7474
];
7575

76-
return $this->handleCurl($curlArgs);
76+
$hash = md5($curlArgs[CURLOPT_URL]);
77+
$transientKey = (is_string($hash) && '' !== $hash) ? $hash : null;
78+
79+
return $this->handleCurl($curlArgs, $transientKey);
7780
}
7881
}

src/PrefillGravityForms/Controllers/PinkRoccadeController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7575
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
7676
];
7777

78-
return $this->handleCurl($curlArgs);
78+
$hash = md5($curlArgs[CURLOPT_URL]);
79+
$transientKey = (is_string($hash) && '' !== $hash) ? $hash : null;
80+
81+
return $this->handleCurl($curlArgs, $transientKey);
7982
}
8083
}

src/PrefillGravityForms/Controllers/VrijBRPController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7575
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
7676
];
7777

78-
return $this->handleCurl($curlArgs);
78+
$hash = md5($curlArgs[CURLOPT_URL]);
79+
$transientKey = (is_string($hash) && '' !== $hash) ? $hash : null;
80+
81+
return $this->handleCurl($curlArgs, $transientKey);
7982
}
8083
}

src/PrefillGravityForms/Controllers/WeAreFrankController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ protected function request(array $data = []): array
132132
],
133133
];
134134

135-
return $this->handleCurl($curlArgs);
135+
$hash = md5(json_encode($data));
136+
$transientKey = (is_string($hash) && '' !== $hash) ? $hash : null;
137+
138+
return $this->handleCurl($curlArgs, $transientKey);
136139
}
137140

138141
protected function getDefaultCurlArgs(): array

0 commit comments

Comments
 (0)