-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseController.php
More file actions
293 lines (232 loc) · 8.49 KB
/
BaseController.php
File metadata and controls
293 lines (232 loc) · 8.49 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace OWC\PrefillGravityForms\Controllers;
use DateTime;
use Exception;
use GF_Field;
use function OWC\PrefillGravityForms\Foundation\Helpers\view;
use OWC\PrefillGravityForms\Foundation\TeamsLogger;
use OWC\PrefillGravityForms\GravityForms\GravityFormsSettings;
use function Yard\DigiD\Foundation\Helpers\decrypt;
use function Yard\DigiD\Foundation\Helpers\resolve;
abstract class BaseController
{
protected GravityFormsSettings $settings;
protected TeamsLogger $teams;
protected string $supplier;
public function __construct()
{
$this->settings = GravityFormsSettings::make();
$this->teams = $this->resolveTeams();
}
public function resolveTeams(): TeamsLogger
{
try {
if (! function_exists('Yard\DigiD\Foundation\Helpers\resolve')) {
throw new Exception();
}
return TeamsLogger::make(resolve('teams'));
} catch (Exception $e) {
return TeamsLogger::make(new \Psr\Log\NullLogger());
}
}
protected function getBSN(): string
{
try {
$bsn = resolve('session')->getSegment('digid')->get('bsn');
} catch(Exception $e) {
$bsn = '';
}
return is_string($bsn) && ! empty($bsn) ? decrypt($bsn) : '';
}
/**
* BSN numbers could start with one or more zero's at the beginning.
* The zero's are not returned by DigiD so the required length of 9 characters is not met.
* Supplement the value so it meets the required length of 9.
*/
protected function supplementBSN(string $bsn): string
{
$bsnLength = strlen($bsn);
$requiredLength = 9;
$difference = $requiredLength - $bsnLength;
if (1 > $difference || $difference > $requiredLength) {
return $bsn;
}
return sprintf("%'.0" . $requiredLength . "d", $bsn);
}
protected function preFillFields(array $form, array $response): array
{
foreach ($form['fields'] as $field) {
$linkedValue = $field->linkedFieldValue ?? '';
if (empty($linkedValue)) {
continue;
}
$foundValue = $this->findLinkedValue($linkedValue, $response);
if (empty($foundValue)) {
$field->cssClass = 'owc_prefilled'; // When field has mapping but there is no value found, set to read-only.
continue;
}
if ('text' === $field->type) {
$this->handleFieldText($field, $foundValue);
continue;
}
if ('date' === $field->type) {
$this->handleFieldDate($field, $foundValue);
continue;
}
}
return $form;
}
public function findLinkedValue(string $linkedValue = '', array $response = []): string
{
if (empty($linkedValue) || empty($response)) {
return $linkedValue;
}
return $this->explodeDotNotationValue($linkedValue, $response);
}
/**
* Explode string in to array items.
* Use these array items to retrieve nested array values from the response.
*/
public function explodeDotNotationValue(string $dotNotationString, array $response): string
{
$exploded = explode('.', $dotNotationString);
$holder = [];
foreach ($exploded as $key => $item) {
if (0 === $key) {
// Place the wanted part of the response in $holder.
$holder = $response[$item] ?? '';
continue;
}
// If $holder is empty there is no need to proceed.
if (empty($holder)) {
break;
}
// If holder is a multidimensional array, flatten.
if (! empty($holder[0]) && is_array($holder[0])) {
$holder = $this->flattenMultidimensionalArray($holder);
}
// Place the nested part of the response in $holder.
$holder = $holder[$item] ?? '';
}
return is_string($holder) || is_numeric($holder) ? $holder : '';
}
protected function flattenMultidimensionalArray(array $array): array
{
$holder = [];
foreach ($array as $part) {
$holder = array_merge($holder, $part);
}
return $holder;
}
protected function handleFieldText(GF_Field $field, string $foundValue): void
{
if ($this->isPossibleDate($foundValue)) {
$field->defaultValue = (new \DateTime($foundValue))->format('d-m-Y');
} else {
$field->defaultValue = ucfirst($foundValue);
}
$field->cssClass = 'owc_prefilled';
}
public function isPossibleDate(string $value): bool
{
return (date('Y-m-d', strtotime($value)) == $value);
}
protected function handleFieldDate(GF_Field $field, string $foundValue): void
{
try {
$date = new DateTime($foundValue);
} catch(Exception $e) {
return;
}
// Field consists of 1 part.
if (empty($field->inputs) || 'datepicker' === $field->dateType) {
$field->defaultValue = $date->format('d-m-Y');
$field->displayOnly = true;
$field->cssClass = 'owc_prefilled';
return;
}
// Field consists of 3 parts which are represented by the input attribute.
if (! empty($field->inputs) && ('datefield' === $field->dateType || 'datedropdown' === $field->dateType)) {
$field->inputs[0]['defaultValue'] = $date->format('m');
$field->inputs[1]['defaultValue'] = $date->format('d');
$field->inputs[2]['defaultValue'] = $date->format('Y');
$field->cssClass = 'owc_prefilled';
}
}
public function getRequestURL(string $identifier = '', string $expand = ''): string
{
$baseURL = $this->settings->getBaseURL();
if (empty($baseURL) || empty($identifier)) {
return '';
}
$url = sprintf('%s/%s', $baseURL, $identifier);
if (! empty($expand)) {
$url = sprintf('%s?%s', $url, $this->createExpandArguments($expand));
}
return $url;
}
protected function createExpandArguments(string $expand): string
{
$exploded = explode(',', $expand);
$filtered = array_filter($exploded);
$new = array_map('trim', $filtered);
$imploded = implode(',', $new);
return urldecode(http_build_query(['expand' => $imploded], '', ','));
}
protected function getCurlHeaders(string $doelBinding = ''): array
{
$headers = [
'x-doelbinding: ' . $doelBinding,
'x-origin-oin: ' . $this->settings->getNumberOIN(),
];
if (! empty($this->settings->getAPIKey())) {
$headers[] = 'x-opentunnel-api-key: ' . $this->settings->getAPIKey();
}
return array_filter($headers);
}
protected function handleCurl(array $args): array
{
try {
$curl = curl_init();
curl_setopt_array($curl, $this->getDefaultCurlArgs() + $args);
if (! empty($this->settings->getPassphrase())) {
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $this->settings->getPassphrase());
}
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($curl);
if (curl_error($curl)) {
throw new \Exception(curl_error($curl));
}
$decoded = json_decode($output, true);
if (! $decoded || json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Something went wrong with decoding of the JSON output.');
}
return $decoded;
} catch (\Exception $e) {
return [
'status' => $e->getMessage(),
];
}
}
protected function getDefaultCurlArgs(): array
{
return [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
];
}
/**
* Prefilled fields have a custom css class
* Based on this custom class fields are disabled.
*/
protected function disableFormFields(): string
{
return view('disabledFormFields.php');
}
}