77use DateTime ;
88use Exception ;
99use GF_Field ;
10+ use function OWC \PrefillGravityForms \Foundation \Helpers \resolve_teams ;
1011use function OWC \PrefillGravityForms \Foundation \Helpers \view ;
1112use OWC \PrefillGravityForms \Foundation \TeamsLogger ;
1213use OWC \PrefillGravityForms \GravityForms \GravityFormsSettings ;
14+ use OWC \PrefillGravityForms \Services \CacheService ;
1315use OWC \PrefillGravityForms \Traits \SessionTrait ;
1416use TypeError ;
1517use WP_Screen ;
16- use function Yard \DigiD \Foundation \Helpers \resolve ;
1718
1819abstract class BaseController
1920{
@@ -31,7 +32,7 @@ abstract class BaseController
3132 public function __construct ()
3233 {
3334 $ this ->settings = GravityFormsSettings::make ();
34- $ this ->teams = $ this -> resolveTeams ();
35+ $ this ->teams = resolve_teams ();
3536 }
3637
3738 abstract public function handle (array $ form ): array ;
@@ -54,22 +55,9 @@ public function get(): array
5455
5556 abstract protected function makeRequest (): array ;
5657
57- public function resolveTeams (): TeamsLogger
58- {
59- try {
60- if (! function_exists ('Yard\DigiD\Foundation\Helpers\resolve ' )) {
61- throw new Exception ();
62- }
63-
64- return TeamsLogger::make (resolve ('teams ' ));
65- } catch (Exception $ e ) {
66- return TeamsLogger::make (new \Psr \Log \NullLogger ());
67- }
68- }
69-
7058 protected function logError (string $ message , $ status ): void
7159 {
72- $ this ->teams ->addRecord ('error ' , 'Prefill data ' , [
60+ $ this ->teams ->addRecord ('error ' , 'BRP Prefill GravityForms ' , [
7361 'message ' => $ message ,
7462 'status ' => $ status ,
7563 ]);
@@ -309,13 +297,15 @@ protected function getCurlHeaders(string $doelBinding = ''): array
309297 return array_filter ($ headers );
310298 }
311299
312- protected function handleCurl (array $ args , ? string $ transientKey = null ): array
300+ protected function handleCurl (array $ args , string $ transientKey ): array
313301 {
314- if (is_string ($ transientKey ) && 0 < strlen (trim ($ transientKey ))) {
315- $ cachedResponse = get_transient ($ transientKey );
316- if (is_array ($ cachedResponse ) && [] !== $ cachedResponse ) {
317- return $ cachedResponse ;
318- }
302+ /**
303+ * IMPORTANT NOTE: when adjusting this piece of code, please make sure
304+ * that the transient key is unique per request. Otherwise, different requests
305+ * might return the same cached response.
306+ */
307+ if ($ cachedResponse = CacheService::getArrayFromTransient ($ transientKey )) {
308+ return $ cachedResponse ;
319309 }
320310
321311 $ curl = curl_init ();
@@ -343,17 +333,15 @@ protected function handleCurl(array $args, ?string $transientKey = null): array
343333 throw new Exception ('Request failed ' , is_int ($ httpStatus ) ? $ httpStatus : 500 );
344334 }
345335
346- $ decoded = json_decode ($ output , true );
336+ $ response = json_decode ($ output , true );
347337
348- if (! is_array ($ decoded ) || [] === $ decoded || json_last_error () !== JSON_ERROR_NONE ) {
338+ if (! is_array ($ response ) || [] === $ response || json_last_error () !== JSON_ERROR_NONE ) {
349339 throw new Exception ('Something went wrong with decoding of the JSON output. ' , 500 );
350340 }
351341
352- if (is_string ($ transientKey ) && 0 < strlen (trim ($ transientKey ))) {
353- set_transient ($ transientKey , $ decoded , HOUR_IN_SECONDS );
354- }
342+ $ this ->handleTransient ($ response , $ transientKey );
355343
356- return $ decoded ;
344+ return $ response ;
357345 } catch (Exception $ e ) {
358346 return [
359347 'message ' => $ e ->getMessage (),
@@ -364,6 +352,31 @@ protected function handleCurl(array $args, ?string $transientKey = null): array
364352 }
365353 }
366354
355+ /**
356+ * Validates whether the necessary conditions are met before setting the transient.
357+ *
358+ * Ensures that:
359+ * - A valid BSN (burgerservicenummer) is present in the response.
360+ * - The transient key derived from that BSN matches the one generated from the current session.
361+ */
362+ protected function handleTransient (array $ response , string $ transientKey ): void
363+ {
364+ $ responseBSN = (string ) ($ response ['burgerservicenummer ' ] ?? '' );
365+
366+ if ('' === $ responseBSN ) {
367+ throw new Exception ('No burgerservicenummer found in the response. ' , 404 );
368+ }
369+
370+ $ transientKeyByResponse = CacheService::formatTransientKey ($ responseBSN );
371+
372+ // Ensure the transient keys generated from the BSN out of the response and current session match.
373+ if ($ transientKeyByResponse !== $ transientKey ) {
374+ throw new Exception ('Transient key mismatch. ' , 500 );
375+ }
376+
377+ CacheService::setTransient ($ transientKey , $ response );
378+ }
379+
367380 protected function timeoutOptionCURL (): int
368381 {
369382 $ timeout = apply_filters ('owc_prefill_gravity_forms_curl_timeout ' , 10 );
0 commit comments