Skip to content

Commit 680ae15

Browse files
Mike van den Hoekmvdhoek1
authored andcommitted
feat: supplement embedded data
1 parent 9fae22c commit 680ae15

7 files changed

Lines changed: 178 additions & 12 deletions

File tree

TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TODO
2+
3+
- Remove deceased related entity by form setting.
4+
- Refactor to PHP8 branch.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OWC\PrefillGravityForms\Abstracts;
6+
7+
use OWC\PrefillGravityForms\Controllers\BaseController;
8+
9+
abstract class GetController extends BaseController
10+
{
11+
/**
12+
* Supplements the '_embedded' array with additional data retrieved via the '_links' array,
13+
* using the configured expand arguments to fetch and merge related resources.
14+
*/
15+
protected function supplementEmbeddedByLinks(array $apiResponse, string $embedType = '', string $doelBinding = ''): array
16+
{
17+
if (! isset($apiResponse['_embedded'][$embedType])) {
18+
return $apiResponse;
19+
}
20+
21+
foreach ($apiResponse['_embedded'][$embedType] as $key => $embeddedItem) {
22+
if (! isset($embeddedItem['_links']['ingeschrevenPersoon']['href'])) {
23+
continue;
24+
}
25+
26+
$response = $this->requestEmbedded(str_replace('https://api.acc-vrijbrp-hoeksche-waard.commonground.nu/haal-centraal-brp-bevragen/api/v1.3/ingeschrevenpersonen', 'https://api.acc-vrijbrp-hoeksche-waard.commonground.nu/api/haalcentraal-brp-bevragen/api/v1.3/ingeschrevenpersonen', $embeddedItem['_links']['ingeschrevenPersoon']['href']), $doelBinding);
27+
28+
// @todo: do this conditionally based on setting.
29+
if (true === ($response['overlijden']['indicatieOverleden'] ?? false)) {
30+
unset($apiResponse['_embedded'][$embedType][$key]);
31+
}
32+
}
33+
34+
return $apiResponse;
35+
}
36+
37+
abstract protected function requestEmbedded(string $url, string $doelBinding): array;
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OWC\PrefillGravityForms\Abstracts;
6+
7+
use OWC\PrefillGravityForms\Controllers\BaseController;
8+
9+
abstract class PostController extends BaseController
10+
{
11+
/**
12+
* Supplements the '_embedded' array with additional data retrieved via the '_links' array,
13+
* using the configured expand arguments to fetch and merge related resources.
14+
*/
15+
protected function supplementEmbeddedByLinks(array $apiResponse, string $embedType = ''): array
16+
{
17+
if (! isset($apiResponse[$embedType])) {
18+
return $apiResponse;
19+
}
20+
21+
foreach ($apiResponse[$embedType] as $key => $embeddedItem) {
22+
if (! isset($embeddedItem['burgerservicenummer']) || ! is_numeric($embeddedItem['burgerservicenummer'])) {
23+
continue;
24+
}
25+
26+
$response = $this->requestEmbedded((string) $embeddedItem['burgerservicenummer']);
27+
$personData = $response['personen'] ?? [];
28+
$firstPerson = reset($personData); // Response is in a multidimensional array which differs from other suppliers.
29+
30+
if (! is_array($firstPerson) || 0 === count($firstPerson)) {
31+
continue;
32+
}
33+
34+
// @todo: do this conditionally based on setting.
35+
if ('overlijden' === ($firstPerson['opschortingBijhouding']['reden']['omschrijving'] ?? '')) {
36+
unset($apiResponse[$embedType][$key]);
37+
}
38+
}
39+
40+
return $apiResponse;
41+
}
42+
43+
abstract protected function requestEmbedded(string $bsn): array;
44+
}

src/PrefillGravityForms/Controllers/EnableUController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace OWC\PrefillGravityForms\Controllers;
66

7+
use OWC\PrefillGravityForms\Abstracts\GetController;
78
use OWC\PrefillGravityForms\Services\CacheService;
89

9-
class EnableUController extends BaseController
10+
class EnableUController extends GetController
1011
{
1112
public function handle(array $form): array
1213
{
@@ -65,6 +66,10 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6566
return [];
6667
}
6768

69+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
70+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
71+
}
72+
6873
return $apiResponse;
6974
}
7075

@@ -77,4 +82,17 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7782

7883
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
7984
}
85+
86+
protected function requestEmbedded(string $url, string $doelBinding): array
87+
{
88+
$curlArgs = [
89+
CURLOPT_URL => $url,
90+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
91+
];
92+
93+
$urlParts = explode('/', $url);
94+
$bsn = is_array($urlParts) && 0 < count($urlParts) ? end($urlParts) : '';
95+
96+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
97+
}
8098
}

src/PrefillGravityForms/Controllers/PinkRoccadeController.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace OWC\PrefillGravityForms\Controllers;
66

7+
use OWC\PrefillGravityForms\Abstracts\GetController;
78
use OWC\PrefillGravityForms\Services\CacheService;
89

9-
class PinkRoccadeController extends BaseController
10+
class PinkRoccadeController extends GetController
1011
{
1112
public function handle(array $form): array
1213
{
@@ -65,6 +66,10 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6566
return [];
6667
}
6768

69+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
70+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
71+
}
72+
6873
return $apiResponse;
6974
}
7075

@@ -79,4 +84,19 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7984

8085
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
8186
}
87+
88+
protected function requestEmbedded(string $url, string $doelBinding): array
89+
{
90+
$curlArgs = [
91+
CURLOPT_URL => $url,
92+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
93+
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
94+
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
95+
];
96+
97+
$urlParts = explode('/', $url);
98+
$bsn = is_array($urlParts) && 0 < count($urlParts) ? end($urlParts) : '';
99+
100+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
101+
}
82102
}

src/PrefillGravityForms/Controllers/VrijBRPController.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace OWC\PrefillGravityForms\Controllers;
66

7+
use OWC\PrefillGravityForms\Abstracts\GetController;
78
use OWC\PrefillGravityForms\Services\CacheService;
89

9-
class VrijBRPController extends BaseController
10+
class VrijBRPController extends GetController
1011
{
1112
public function handle(array $form): array
1213
{
@@ -15,6 +16,7 @@ public function handle(array $form): array
1516
}
1617

1718
$bsn = $this->getBSN();
19+
$bsn = '900231038';
1820

1921
if ('' === $bsn) {
2022
return $form;
@@ -65,6 +67,10 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6567
return [];
6668
}
6769

70+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
71+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
72+
}
73+
6874
return $apiResponse;
6975
}
7076

@@ -79,4 +85,19 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7985

8086
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
8187
}
88+
89+
protected function requestEmbedded(string $url, string $doelBinding): array
90+
{
91+
$curlArgs = [
92+
CURLOPT_URL => $url,
93+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
94+
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
95+
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
96+
];
97+
98+
$urlParts = explode('/', $url);
99+
$bsn = is_array($urlParts) && 0 < count($urlParts) ? end($urlParts) : '';
100+
101+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
102+
}
82103
}

src/PrefillGravityForms/Controllers/WeAreFrankController.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace OWC\PrefillGravityForms\Controllers;
66

77
use Exception;
8+
use OWC\PrefillGravityForms\Abstracts\PostController;
89
use OWC\PrefillGravityForms\Services\CacheService;
910

10-
class WeAreFrankController extends BaseController
11+
class WeAreFrankController extends PostController
1112
{
1213
public function handle(array $form): array
1314
{
@@ -22,9 +23,7 @@ public function handle(array $form): array
2223
}
2324

2425
$expand = rgar($form, 'owc-iconnect-expand', '');
25-
$preparedData = $this->prepareData($bsn, $expand);
26-
27-
$firstPerson = $this->fetchPersonData($preparedData, $bsn);
26+
$firstPerson = $this->fetchPersonData($bsn, $expand);
2827

2928
if (empty($firstPerson)) {
3029
return $form;
@@ -45,7 +44,7 @@ protected function makeRequest(): array
4544

4645
$preparedData = $this->prepareData($bsn);
4746

48-
return $this->fetchPersonData($preparedData, $bsn);
47+
return $this->fetchPersonData($bsn);
4948
}
5049

5150
/**
@@ -120,9 +119,9 @@ protected function extractBSN(array $response): string
120119
return (string) $bsn;
121120
}
122121

123-
protected function fetchPersonData(array $preparedData, string $bsn): array
122+
protected function fetchPersonData(string $bsn, string $expand = ''): array
124123
{
125-
$apiResponse = $this->request($preparedData, $bsn);
124+
$apiResponse = $this->request($bsn, $expand);
126125
$personData = $apiResponse['personen'] ?? [];
127126
$firstPerson = reset($personData); // Response is in a multidimensional array which differs from other suppliers.
128127

@@ -138,14 +137,36 @@ protected function fetchPersonData(array $preparedData, string $bsn): array
138137
return [];
139138
}
140139

140+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
141+
$firstPerson = $this->supplementEmbeddedByLinks($firstPerson, trim($expandItem));
142+
}
143+
141144
return $firstPerson;
142145
}
143146

144-
protected function request(array $data = [], string $bsn = ''): array
147+
protected function request(string $bsn = '', string $expand = ''): array
148+
{
149+
$curlArgs = [
150+
CURLOPT_URL => $this->settings->getBaseURL(),
151+
CURLOPT_POSTFIELDS => json_encode($this->prepareData($bsn, $expand)),
152+
CURLOPT_HTTPHEADER => [
153+
'Content-Type: application/json',
154+
'Accept: application/json',
155+
sprintf('%s: %s', $this->settings->getAPITokenUsername(), $this->settings->getAPITokenPassword()),
156+
],
157+
];
158+
159+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
160+
}
161+
162+
/**
163+
* This one breaks the contract, fix later.
164+
*/
165+
protected function requestEmbedded(string $bsn = ''): array
145166
{
146167
$curlArgs = [
147168
CURLOPT_URL => $this->settings->getBaseURL(),
148-
CURLOPT_POSTFIELDS => json_encode($data),
169+
CURLOPT_POSTFIELDS => json_encode($this->prepareData($bsn)),
149170
CURLOPT_HTTPHEADER => [
150171
'Content-Type: application/json',
151172
'Accept: application/json',

0 commit comments

Comments
 (0)