|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OWC\PrefillGravityForms\Controllers; |
| 4 | + |
| 5 | +use function OWC\PrefillGravityForms\Foundation\Helpers\view; |
| 6 | +use function Yard\DigiD\Foundation\Helpers\decrypt; |
| 7 | +use function Yard\DigiD\Foundation\Helpers\resolve; |
| 8 | +use OWC\PrefillGravityForms\Foundation\TeamsLogger; |
| 9 | +use OWC\PrefillGravityForms\GravityForms\GravityFormsSettings; |
| 10 | + |
| 11 | +abstract class BaseController |
| 12 | +{ |
| 13 | + protected GravityFormsSettings $settings; |
| 14 | + protected TeamsLogger $teams; |
| 15 | + protected string $supplier; |
| 16 | + |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + $this->settings = GravityFormsSettings::make(); |
| 20 | + $this->teams = $this->resolveTeams(); |
| 21 | + } |
| 22 | + |
| 23 | + public function resolveTeams(): TeamsLogger |
| 24 | + { |
| 25 | + try { |
| 26 | + if (! function_exists('Yard\DigiD\Foundation\Helpers\resolve')) { |
| 27 | + throw new \Exception; |
| 28 | + } |
| 29 | + |
| 30 | + return TeamsLogger::make(resolve('teams')); |
| 31 | + } catch (\Exception $e) { |
| 32 | + return TeamsLogger::make(new \Psr\Log\NullLogger()); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Validate if form has a DigiD field. |
| 38 | + * If so return the decrypted BSN number. |
| 39 | + */ |
| 40 | + protected function getBSN(array $form): string |
| 41 | + { |
| 42 | + $bsn = ''; |
| 43 | + |
| 44 | + foreach ($form['fields'] as $field) { |
| 45 | + if ($field->type !== 'digid') { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + $resolvedBSN = resolve('session')->getSegment('digid')->get('bsn'); |
| 50 | + |
| 51 | + if (empty($resolvedBSN)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $bsn = decrypt($resolvedBSN); |
| 56 | + |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + return $bsn; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * BSN numbers could start with one or more zero's at the beginning. |
| 65 | + * The zero's are not returned by DigiD so the required length of 9 characters is not met. |
| 66 | + * Supplement the value so it meets the required length of 9. |
| 67 | + */ |
| 68 | + protected function supplementBSN(string $bsn): string |
| 69 | + { |
| 70 | + $bsnLength = strlen($bsn); |
| 71 | + $requiredLength = 9; |
| 72 | + $difference = $requiredLength - $bsnLength; |
| 73 | + |
| 74 | + if ($difference < 1 || $difference > $requiredLength) { |
| 75 | + return $bsn; |
| 76 | + } |
| 77 | + |
| 78 | + return sprintf("%'.0" . $requiredLength . "d", $bsn); |
| 79 | + } |
| 80 | + |
| 81 | + protected function preFillFields(array $form, array $response): array |
| 82 | + { |
| 83 | + foreach ($form['fields'] as $field) { |
| 84 | + $linkedValue = $field->linkedFieldValue ?? ''; |
| 85 | + $foundValue = $this->findLinkedValue($linkedValue, $response); |
| 86 | + |
| 87 | + if (empty($foundValue)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if ($field->type === 'text') { |
| 92 | + $field->defaultValue = ucfirst($foundValue); |
| 93 | + $field->cssClass = 'owc_prefilled'; |
| 94 | + } |
| 95 | + |
| 96 | + if ($field->type === 'date') { |
| 97 | + $field->defaultValue = (new \DateTime($foundValue))->format('d-m-Y'); |
| 98 | + $field->displayOnly = true; |
| 99 | + $field->cssClass = 'owc_prefilled'; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return $form; |
| 104 | + } |
| 105 | + |
| 106 | + public function findLinkedValue(string $linkedValue = '', array $response = []): string |
| 107 | + { |
| 108 | + if (empty($linkedValue) || empty($response)) { |
| 109 | + return $linkedValue; |
| 110 | + } |
| 111 | + |
| 112 | + return $this->explodeDotNotationValue($linkedValue, $response); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Explode string in to array items. |
| 117 | + * Use these array items to retrieve nested array values from the response. |
| 118 | + */ |
| 119 | + public function explodeDotNotationValue(string $dotNotationString, array $response): string |
| 120 | + { |
| 121 | + $exploded = explode('.', $dotNotationString); |
| 122 | + $holder = []; |
| 123 | + |
| 124 | + foreach ($exploded as $key => $item) { |
| 125 | + if ($key === 0) { |
| 126 | + // Place the wanted part of the response in $holder. |
| 127 | + $holder = $response[$item] ?? ''; |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + // If $holder is empty there is no need to proceed. |
| 132 | + if (empty($holder)) { |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + // If holder is a multidimensional array, unflatten. |
| 137 | + if (!empty($holder[0]) && is_array($holder[0])) { |
| 138 | + $holder = $this->unflattenHolderArray($holder); |
| 139 | + } |
| 140 | + |
| 141 | + // Place the nested part of the response in $holder. |
| 142 | + $holder = $holder[$item] ?? ''; |
| 143 | + } |
| 144 | + |
| 145 | + return is_string($holder) || is_numeric($holder) ? $holder : ''; |
| 146 | + } |
| 147 | + |
| 148 | + protected function unflattenHolderArray(array $holder): array |
| 149 | + { |
| 150 | + $backupHolder = []; |
| 151 | + |
| 152 | + foreach ($holder as $part) { |
| 153 | + $backupHolder = array_merge($backupHolder, $part); |
| 154 | + } |
| 155 | + |
| 156 | + return $backupHolder; |
| 157 | + } |
| 158 | + |
| 159 | + public function getRequestURL(string $identifier = '', string $expand = ''): string |
| 160 | + { |
| 161 | + $baseURL = $this->settings->getBaseURL(); |
| 162 | + |
| 163 | + if (empty($baseURL) || empty($identifier)) { |
| 164 | + return ''; |
| 165 | + } |
| 166 | + |
| 167 | + $url = sprintf('%s/%s', $baseURL, $identifier); |
| 168 | + |
| 169 | + if (! empty($expand)) { |
| 170 | + $url = sprintf('%s?%s', $url, $this->createExpandArguments($expand)); |
| 171 | + } |
| 172 | + |
| 173 | + return $url; |
| 174 | + } |
| 175 | + |
| 176 | + protected function createExpandArguments(string $expand): string |
| 177 | + { |
| 178 | + $exploded = explode(',', $expand); |
| 179 | + $filtered = array_filter($exploded); |
| 180 | + $new = array_map('trim', $filtered); |
| 181 | + $imploded = implode(',', $new); |
| 182 | + |
| 183 | + return urldecode(http_build_query(['expand' => $imploded], '', ',')); |
| 184 | + } |
| 185 | + |
| 186 | + protected function getCurlHeaders(string $doelBinding = ''): array |
| 187 | + { |
| 188 | + $headers = [ |
| 189 | + 'x-doelbinding: ' . $doelBinding, |
| 190 | + 'x-origin-oin: ' . $this->settings->getNumberOIN() |
| 191 | + ]; |
| 192 | + |
| 193 | + return array_filter($headers); |
| 194 | + } |
| 195 | + |
| 196 | + protected function handleCurl(array $args): array |
| 197 | + { |
| 198 | + try { |
| 199 | + $curl = curl_init(); |
| 200 | + |
| 201 | + curl_setopt_array($curl, $this->getDefaultCurlArgs() + $args); |
| 202 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 203 | + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
| 204 | + |
| 205 | + $output = curl_exec($curl); |
| 206 | + |
| 207 | + if (curl_error($curl)) { |
| 208 | + throw new \Exception(curl_error($curl)); |
| 209 | + } |
| 210 | + |
| 211 | + $decoded = json_decode($output, true); |
| 212 | + |
| 213 | + if (! $decoded || json_last_error() !== JSON_ERROR_NONE) { |
| 214 | + throw new \Exception('Something went wrong with decoding of the JSON output.'); |
| 215 | + } |
| 216 | + |
| 217 | + return $decoded; |
| 218 | + } catch (\Exception $e) { |
| 219 | + return [ |
| 220 | + 'status' => $e->getMessage() |
| 221 | + ]; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + protected function getDefaultCurlArgs(): array |
| 226 | + { |
| 227 | + return [ |
| 228 | + CURLOPT_RETURNTRANSFER => true, |
| 229 | + CURLOPT_ENCODING => '', |
| 230 | + CURLOPT_MAXREDIRS => 10, |
| 231 | + CURLOPT_TIMEOUT => 0, |
| 232 | + CURLOPT_FOLLOWLOCATION => true, |
| 233 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 234 | + CURLOPT_CUSTOMREQUEST => 'GET', |
| 235 | + ]; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Prefilled fields have a custom css class |
| 240 | + * Based on this custom class fields are disabled. |
| 241 | + */ |
| 242 | + protected function disableFormFields(): string |
| 243 | + { |
| 244 | + return view('disabledFormFields.php'); |
| 245 | + } |
| 246 | +} |
0 commit comments