You are here

class WSConnectorSOAP in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/WSConnector/WSConnectorSOAP.php \Drupal\wsdata\Plugin\WSConnector\WSConnectorSOAP

REST Connector.

Plugin annotation


@WSConnector(
  id = "WSConnectorSOAP",
  label = @Translation("SOAP Connector", context = "WSConnector"),
)

Hierarchy

Expanded class hierarchy of WSConnectorSOAP

File

src/Plugin/WSConnector/WSConnectorSOAP.php, line 17

Namespace

Drupal\wsdata\Plugin\WSConnector
View source
class WSConnectorSOAP extends WSConnectorSimpleHTTP {

  /**
   * {@inheritdoc}
   */
  public function getMethods() {
    return [
      'create',
      'read',
      'update',
      'delete',
      'index',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions() {
    return [
      'path' => null,
      'methods' => [],
      'headers' => [],
      'user' => null,
      'key' => null,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getReplacements(array $options) {
    return $this
      ->findTokens($options['path']);
  }

  /**
   * {@inheritdoc}
   */
  public function getOptionsForm($options = []) {
    $form['path'] = [
      '#title' => $this
        ->t('Path'),
      '#description' => $this
        ->t('The final endpoint will be <em>Server Endpoint/Path</em>'),
      '#type' => 'textfield',
      '#maxlength' => 512,
    ];
    $form['user'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username'),
      '#description' => $this
        ->t('Authentication'),
      '#required' => true,
    ];
    $form['key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Password'),
      '#description' => $this
        ->t('Authentication'),
      '#required' => true,
    ];
    $header_count = 5;
    if (isset($options['form_state'])) {
      $input = $options['form_state']
        ->getUserInput();
      if (isset($input['headers_count'])) {
        $header_count = $input['headers_count'] + 1;
      }
    }
    $form['headers'] = [
      '#title' => $this
        ->t('Fixed Parameters'),
      '#type' => 'fieldset',
      '#attributes' => [
        'id' => 'wsconnector-headers',
      ],
    ];
    $form['headers']['headers_count'] = [
      '#type' => 'hidden',
      '#value' => $header_count,
    ];
    for ($i = 0; $i < $header_count; $i++) {
      $form['headers'][$i]['key_' . $i] = [
        '#type' => 'textfield',
        '#title' => t('Key'),
      ];
      $form['headers'][$i]['value_' . $i] = [
        '#type' => 'textfield',
        '#title' => t('Value'),
      ];
    }
    if (isset($options['form_state'])) {
      $form['headers']['add_another'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Add another'),
        '#ajax' => [
          'callback' => '\\Drupal\\wsdata\\Plugin\\WSConnector\\WSConnectorSimpleHTTP::wsconnectorHttpHeaderAjaxCallback',
          'wrapper' => 'wsconnector-headers',
        ],
        '#limit_validation_errors' => [],
      ];
    }
    $form['wsdl'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('WSDL'),
      '#description' => $this
        ->t('WSDL url'),
      '#required' => true,
    ];
    $form['method'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Method'),
      '#description' => $this
        ->t('Method name'),
      '#required' => true,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function call($options, $method, $replacements = [], $data = null, array $tokens = []) {
    $token_service = \Drupal::token();
    $uri = $this->endpoint . '/' . $options['path'];

    // Perform the token replace on the headers.
    if (!empty($options['headers'])) {
      for ($i = 0; $i < count($options['headers']); $i++) {
        if (!empty($options['headers'][$i]['key_' . $i])) {
          $options['headers'][$options['headers'][$i]['key_' . $i]] = $token_service
            ->replace($options['headers'][$i]['value_' . $i], $tokens);
        }
        unset($options['headers'][$i]['key_' . $i]);
        unset($options['headers'][$i]['value_' . $i]);
        unset($options['headers'][$i]);
      }
      if (count($replacements) == count($replacements, COUNT_RECURSIVE)) {
        $payload = array_merge($replacements, $options['headers']);
      }
      else {

        // array is multidimensional
        $payload = $replacements;
        foreach ($options['headers'] as $k => $v) {
          $payload[array_keys($replacements)[0]][$k] = $v;
        }
      }
    }
    if (isset($options['method']) && !empty($options['method'])) {
      $method = $options['method'];
    }
    if (isset($options['wsdl']) && !empty($options['wsdl'])) {
      $wsdl = DRUPAL_ROOT . $options['wsdl'];
    }
    else {
      $wsdl = $uri;
    }
    $result = FALSE;
    try {
      if (isset($options["user"]) && isset($options["key"])) {
        $service = new \SoapClient($wsdl, array(
          'login' => $options["user"],
          'password' => $options["key"],
        ));
      }
      else {
        $service = new \SoapClient($wsdl);
      }
      if (isset($options['wsdl']) && !empty($options['wsdl']) && isset($options['path']) && !empty($options['path'])) {
        $service
          ->__setLocation($uri);
      }
      if (!is_soap_fault($service)) {
        $result = $service
          ->__soapCall($method, !empty($options['headers']) ? array(
          $payload,
        ) : array(
          $replacements,
        ));
      }
    } catch (\Throwable $th) {
      $message = $this
        ->t('SOAP call: Could not call endpoint: :uri and method: @method', [
        ':uri' => $uri,
        '@method' => $method,
      ]);
      $this
        ->setError(1, $message);
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WSConnectorBase::$cacheDefaultOverride protected property
WSConnectorBase::$cacheDefaultTime protected property
WSConnectorBase::$endpoint protected property
WSConnectorBase::$error protected property
WSConnectorBase::$expires protected property
WSConnectorBase::$languagePlugins protected property
WSConnectorBase::$staleCache protected property
WSConnectorBase::$status protected property
WSConnectorBase::applyReplacements protected function Internal function for applying replacements.
WSConnectorBase::clearError protected function Clear current error.
WSConnectorBase::defaultCache public function Figure out the overrides for cache times.
WSConnectorBase::expires public function Get the expired time for caching.
WSConnectorBase::findTokens protected function Internal function for finding tokens.
WSConnectorBase::getCache public function Return cache cid for cases cache rules change. 2
WSConnectorBase::getEndpoint public function Getter for the endpoint.
WSConnectorBase::getError public function Return the last error the connector received.
WSConnectorBase::getStatus public function Return the status of the last call.
WSConnectorBase::getSupportedLanguagePlugins public function Return the list of supported language handling plugins.
WSConnectorBase::isDegraded public function Whether the connector is in a dead state and shouldn't be called.
WSConnectorBase::setEndpoint public function Setter for the endpoint.
WSConnectorBase::setError protected function Setter for the connector errors.
WSConnectorSimpleHTTP::create public static function Creates an instance of the plugin. Overrides WSConnectorBase::create 2
WSConnectorSimpleHTTP::saveOptions public function Saves the options form. Overrides WSConnectorBase::saveOptions
WSConnectorSimpleHTTP::setCacheExpire public function Sets the expires times based on the response.
WSConnectorSimpleHTTP::supportsCaching public function Whether returned data can be cached. Overrides WSConnectorBase::supportsCaching
WSConnectorSimpleHTTP::wsconnectorHttpHeaderAjaxCallback public static function Ajax callback function. 1
WSConnectorSimpleHTTP::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides WSConnectorBase::__construct 2
WSConnectorSOAP::call public function Make the connector call. Overrides WSConnectorSimpleHTTP::call
WSConnectorSOAP::getMethods public function Return available methods supported by the connector. Overrides WSConnectorSimpleHTTP::getMethods
WSConnectorSOAP::getOptions public function Return available options supported by the connector. Overrides WSConnectorSimpleHTTP::getOptions
WSConnectorSOAP::getOptionsForm public function Return the settings form provided by the connector. Overrides WSConnectorSimpleHTTP::getOptionsForm
WSConnectorSOAP::getReplacements public function Return an array of replacements. Overrides WSConnectorSimpleHTTP::getReplacements