|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormStateInterface; |
| 6 | + |
| 7 | +/** |
| 8 | + * Defines base plugin class for Serviceplatformen plugins. |
| 9 | + */ |
| 10 | +abstract class ServiceplatformenBase extends DataLookupBase implements DataLookupInterface { |
| 11 | + |
| 12 | + /** |
| 13 | + * Plugin status string. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $status; |
| 18 | + |
| 19 | + /** |
| 20 | + * Service object. |
| 21 | + * |
| 22 | + * @var \SoapClient |
| 23 | + */ |
| 24 | + protected $client; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function __construct(array $configuration, $plugin_id, $plugin_definition) { |
| 30 | + parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 31 | + $this->init(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + public function defaultConfiguration() { |
| 38 | + return [ |
| 39 | + 'mode_selector' => 0, |
| 40 | + 'serviceagreementuuid' => '', |
| 41 | + 'serviceuuid' => '', |
| 42 | + 'wsdl' => '', |
| 43 | + 'location' => '', |
| 44 | + 'location_test' => '', |
| 45 | + 'usersystemuuid' => '', |
| 46 | + 'useruuid' => '', |
| 47 | + 'accountinginfo' => '', |
| 48 | + 'certfile_passphrase' => '', |
| 49 | + 'certfile' => '', |
| 50 | + 'certfile_test' => '', |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
| 58 | + $form['mode_fieldset'] = [ |
| 59 | + '#type' => 'fieldset', |
| 60 | + '#title' => $this->t('Mode'), |
| 61 | + ]; |
| 62 | + |
| 63 | + $form['mode_fieldset']['mode_selector'] = [ |
| 64 | + '#type' => 'radios', |
| 65 | + '#default_value' => $this->configuration['mode_selector'], |
| 66 | + '#options' => [0 => $this->t('Live'), 1 => $this->t('Test')], |
| 67 | + ]; |
| 68 | + |
| 69 | + $form['serviceagreementuuid'] = [ |
| 70 | + '#type' => 'textfield', |
| 71 | + '#title' => 'Serviceaftale UUID', |
| 72 | + '#default_value' => $this->configuration['serviceagreementuuid'], |
| 73 | + ]; |
| 74 | + |
| 75 | + $form['serviceuuid'] = [ |
| 76 | + '#type' => 'textfield', |
| 77 | + '#title' => 'Service UUID', |
| 78 | + '#default_value' => $this->configuration['serviceuuid'], |
| 79 | + '#description' => $this->t('ex. c0daecde-e278-43b7-84fd-477bfeeea027'), |
| 80 | + ]; |
| 81 | + |
| 82 | + $form['wsdl'] = [ |
| 83 | + '#type' => 'textfield', |
| 84 | + '#maxlength' => 500, |
| 85 | + '#title' => 'Service WSDL location', |
| 86 | + '#default_value' => $this->configuration['wsdl'], |
| 87 | + '#description' => $this->t('ex. CVROnline-SF1530/wsdl/token/OnlineService.wsdl, relative path would be automatically converted to absolute path'), |
| 88 | + ]; |
| 89 | + |
| 90 | + $form['location'] = [ |
| 91 | + '#type' => 'textfield', |
| 92 | + '#title' => 'Service location (live)', |
| 93 | + '#default_value' => $this->configuration['location'], |
| 94 | + '#description' => $this->t('ex. https://prod.serviceplatformen.dk/service/CVR/Online/2'), |
| 95 | + ]; |
| 96 | + |
| 97 | + $form['location_test'] = [ |
| 98 | + '#type' => 'textfield', |
| 99 | + '#title' => 'Service location (test)', |
| 100 | + '#default_value' => $this->configuration['location_test'], |
| 101 | + '#description' => $this->t('ex. https://exttest.serviceplatformen.dk/service/CVR/Online/2'), |
| 102 | + ]; |
| 103 | + |
| 104 | + $form['usersystemuuid'] = [ |
| 105 | + '#type' => 'textfield', |
| 106 | + '#title' => 'System UUID', |
| 107 | + '#default_value' => $this->configuration['usersystemuuid'], |
| 108 | + ]; |
| 109 | + |
| 110 | + $form['useruuid'] = [ |
| 111 | + '#type' => 'textfield', |
| 112 | + '#title' => 'Kommune UUID', |
| 113 | + '#default_value' => $this->configuration['useruuid'], |
| 114 | + ]; |
| 115 | + |
| 116 | + $form['accountinginfo'] = [ |
| 117 | + '#type' => 'textfield', |
| 118 | + '#title' => 'AccountingInfo', |
| 119 | + '#default_value' => $this->configuration['accountinginfo'], |
| 120 | + ]; |
| 121 | + |
| 122 | + $form['certfile_passphrase'] = [ |
| 123 | + '#type' => 'password', |
| 124 | + '#title' => 'Certfile passphrase', |
| 125 | + '#default_value' => $this->configuration['certfile_passphrase'], |
| 126 | + ]; |
| 127 | + |
| 128 | + $form['certfile'] = [ |
| 129 | + '#type' => 'textfield', |
| 130 | + '#title' => 'Certfile (live)', |
| 131 | + '#default_value' => $this->configuration['certfile'], |
| 132 | + ]; |
| 133 | + |
| 134 | + $form['certfile_test'] = [ |
| 135 | + '#type' => 'textfield', |
| 136 | + '#title' => 'Certfile (test)', |
| 137 | + '#default_value' => $this->configuration['certfile_test'], |
| 138 | + ]; |
| 139 | + |
| 140 | + return $form; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * {@inheritdoc} |
| 145 | + */ |
| 146 | + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
| 147 | + if ($form_state->getValue('certfile_passphrase') == '') { |
| 148 | + $form_state->unsetValue('certfile_passphrase'); |
| 149 | + } |
| 150 | + |
| 151 | + $keys = array_keys($this->defaultConfiguration()); |
| 152 | + $configuration = $this->getConfiguration(); |
| 153 | + foreach ($keys as $key) { |
| 154 | + $configuration[$key] = $form_state->getValue($key); |
| 155 | + } |
| 156 | + $this->setConfiguration($configuration); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritdoc} |
| 161 | + */ |
| 162 | + public function getStatus() { |
| 163 | + return $this->status; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Plugin init method. |
| 168 | + */ |
| 169 | + private function init() { |
| 170 | + ini_set('soap.wsdl_cache_enabled', 0); |
| 171 | + ini_set('soap.wsdl_cache_ttl', 0); |
| 172 | + $this->status = $this->t('Plugin is ready to work')->__toString(); |
| 173 | + |
| 174 | + $required_configuration = [ |
| 175 | + 0 => [ |
| 176 | + 'serviceagreementuuid', |
| 177 | + 'serviceuuid', |
| 178 | + 'wsdl', |
| 179 | + 'location', |
| 180 | + 'usersystemuuid', |
| 181 | + 'useruuid', |
| 182 | + 'accountinginfo', |
| 183 | + 'certfile', |
| 184 | + ], |
| 185 | + 1 => [ |
| 186 | + 'serviceagreementuuid', |
| 187 | + 'serviceuuid', |
| 188 | + 'wsdl', |
| 189 | + 'location_test', |
| 190 | + 'usersystemuuid', |
| 191 | + 'useruuid', |
| 192 | + 'accountinginfo', |
| 193 | + 'certfile_test', |
| 194 | + ], |
| 195 | + ]; |
| 196 | + $this->isReady = TRUE; |
| 197 | + foreach ($required_configuration[$this->configuration['mode_selector']] as $key) { |
| 198 | + if (empty($this->configuration[$key])) { |
| 199 | + $this->isReady = FALSE; |
| 200 | + $this->status = $this->t('Configuration is not completed.')->__toString(); |
| 201 | + return; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + try { |
| 206 | + switch ($this->configuration['mode_selector']) { |
| 207 | + case 0: |
| 208 | + $ws_config = [ |
| 209 | + 'location' => $this->configuration['location'], |
| 210 | + 'local_cert' => $this->configuration['certfile'], |
| 211 | + 'passphrase' => $this->configuration['certfile_passphrase'], |
| 212 | + 'trace' => TRUE, |
| 213 | + ]; |
| 214 | + break; |
| 215 | + |
| 216 | + case 1: |
| 217 | + $ws_config = [ |
| 218 | + 'location' => $this->configuration['location_test'], |
| 219 | + 'local_cert' => $this->configuration['certfile_test'], |
| 220 | + 'trace' => TRUE, |
| 221 | + ]; |
| 222 | + break; |
| 223 | + } |
| 224 | + $this->client = new \SoapClient($this->getWsdlUrl(), $ws_config); |
| 225 | + } |
| 226 | + catch (\SoapFault $e) { |
| 227 | + $this->isReady = FALSE; |
| 228 | + $this->status = $e->faultstring; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Get wsdl URL method. |
| 234 | + * |
| 235 | + * @return string |
| 236 | + * WSDL URL. |
| 237 | + */ |
| 238 | + protected function getWsdlUrl() { |
| 239 | + $wsdl = $this->configuration['wsdl']; |
| 240 | + // If it is relative URL make is absolute. |
| 241 | + if (substr($wsdl, 0, 4) !== "http") { |
| 242 | + global $base_url, $base_path; |
| 243 | + $wsdl = $base_url . $base_path . drupal_get_path('module', 'os2web_datalookup') . '/' . $wsdl; |
| 244 | + } |
| 245 | + return $wsdl; |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * Webservice general request array prepare method. |
| 250 | + * |
| 251 | + * @return array |
| 252 | + * Prepared request with general info. |
| 253 | + */ |
| 254 | + protected function prepareRequest() { |
| 255 | + /** @var \Drupal\Core\Session\AccountProxyInterface $user */ |
| 256 | + $user = \Drupal::currentUser(); |
| 257 | + return [ |
| 258 | + 'InvocationContext' => [ |
| 259 | + 'ServiceAgreementUUID' => $this->configuration['serviceagreementuuid'], |
| 260 | + 'UserSystemUUID' => $this->configuration['usersystemuuid'], |
| 261 | + 'UserUUID' => $this->configuration['useruuid'], |
| 262 | + 'ServiceUUID' => $this->configuration['serviceuuid'], |
| 263 | + 'AccountingInfo' => $this->configuration['accountinginfo'], |
| 264 | + 'OnBehalfOfUser' => $user->getAccountName(), |
| 265 | + ], |
| 266 | + ]; |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Main service request query method. |
| 271 | + * |
| 272 | + * @param string $method |
| 273 | + * Method name to call. |
| 274 | + * @param array $request |
| 275 | + * Request array to call method. |
| 276 | + * |
| 277 | + * @return array |
| 278 | + * Method response or FALSE. |
| 279 | + */ |
| 280 | + protected function query($method, array $request) { |
| 281 | + if (!$this->isReady()) { |
| 282 | + return [ |
| 283 | + 'status' => FALSE, |
| 284 | + 'text' => $this->getStatus(), |
| 285 | + ]; |
| 286 | + } |
| 287 | + |
| 288 | + try { |
| 289 | + $response = (array) $this->client->$method($request); |
| 290 | + $response['status'] = TRUE; |
| 291 | + } |
| 292 | + catch (\SoapFault $e) { |
| 293 | + $response = [ |
| 294 | + 'status' => FALSE, |
| 295 | + 'error' => $e->faultstring, |
| 296 | + ]; |
| 297 | + } |
| 298 | + |
| 299 | + return $response; |
| 300 | + } |
| 301 | + |
| 302 | +} |
0 commit comments