You are here

class WSClientServiceDescription in Web service client 7

Class representing web service descriptions.

Hierarchy

Expanded class hierarchy of WSClientServiceDescription

1 string reference to 'WSClientServiceDescription'
wsclient_entity_info in ./wsclient.module
Implements hook_entity_info().

File

./wsclient.inc, line 11
Web service client - include file.

View source
class WSClientServiceDescription extends Entity {
  public $settings = array();
  public $operations = array();
  public $datatypes = array();
  public $global_parameters = array();
  public $global_header_parameters = array();
  public $name;
  public $label;
  public $url;
  public $type;
  protected $endpoint = NULL;
  function __construct($values = array(), $entity_type = 'wsclient_service') {
    parent::__construct($values, $entity_type);
  }

  /**
   * Magic method to catch service invocations.
   */
  public function __call($operation, $arguments) {
    return $this
      ->invoke($operation, $arguments);
  }

  /**
   * Invoke a service via its endpoint.
   *
   * @param string $operation
   *   The name of the operation to execute.
   * @param array $arguments
   *   Arguments to pass to the service with this operation. If the array keys
   *   do not match the parameter information, the array values are assigned in
   *   sequential order according to the order of parameters.
   */
  public function invoke($operation, array $arguments) {
    if (!isset($this->operations[$operation])) {
      throw new WSClientException('Operation %operation does not exist for web service %service.', array(
        '%operation' => $operation,
        '%service' => $this->name,
      ));
    }
    $named_arguments = array();
    if (isset($this->operations[$operation]['parameter'])) {
      $remaining_params = $this->operations[$operation]['parameter'];

      // Assign named arguments and hidden parameters.
      foreach ($this->operations[$operation]['parameter'] as $param => $info) {
        if (isset($arguments[$param])) {
          $named_arguments[$param] = $arguments[$param];
          unset($arguments[$param]);
          unset($remaining_params[$param]);
        }
        elseif ($info['type'] == 'hidden') {
          $named_arguments[$param] = $info['default value'];
          unset($remaining_params[$param]);
        }
      }

      // Assign the rest in sequential order.
      foreach ($remaining_params as $param => $info) {
        $named_arguments[$param] = array_shift($arguments);
      }
    }

    // Fill in global parameters.
    foreach ($this->global_parameters as $param => $info) {
      if (!isset($named_arguments[$param])) {
        $named_arguments[$param] = $info['default value'];
      }
    }

    // Unless explicitly permitted by 'allow null', unset optional parameters
    // with NULL or empty ('') values.
    if (isset($this->operations[$operation]['parameter'])) {
      foreach ($this->operations[$operation]['parameter'] as $param => $info) {
        if (isset($info['optional']) && $info['optional'] && (!isset($info['allow null']) || !$info['allow null']) && ($named_arguments[$param] === '' || !isset($named_arguments[$param]))) {
          unset($named_arguments[$param]);
        }
      }
    }
    drupal_alter('wsclient_invoke_arguments', $named_arguments, $operation, $this);
    $response = $this
      ->endpoint()
      ->call($operation, $named_arguments);
    drupal_alter('wsclient_invoke_response', $response, $operation, $this);
    return $response;
  }

  /**
   * Determines access to the web service.
   */
  public function access($account = NULL) {
    if (method_exists($this
      ->endpoint(), 'access')) {
      return $this
        ->endpoint()
        ->access($account);
    }
    return user_access('interact with ' . $this->name, $account);
  }

  /**
   * Returns the associated web service endpoint object.
   *
   * @return WSClientEndpointInterface
   */
  public function endpoint() {
    if (!isset($this->endpoint)) {
      $types = wsclient_get_types();
      $this->endpoint = new $types[$this->type]['class']($this);
    }
    return $this->endpoint;
  }

  /**
   * Returns info about the data types of the web service.
   */
  public function dataTypes() {
    $types = $this->datatypes;
    foreach ($types as $type => $info) {

      // Add in the service name so that others know where this type comes from.
      $types[$type]['service'] = $this->name;
    }
    return $types;
  }

  /**
   * Returns info about the actions of the web service.
   */
  public function actions() {
    $actions = array();
    foreach ($this->operations as $name => $operation) {
      $actions[$name] = $operation += array(
        'base' => 'wsclient_service_action',
        'named parameter' => TRUE,
      );
      $actions[$name]['parameter'] = array();
      if (isset($operation['parameter'])) {

        // Prefix operation parameter names to avoid name clashes.
        foreach ((array) $operation['parameter'] as $param => $info) {
          $actions[$name]['parameter']['param_' . $param] = $info;
        }
      }
      $actions[$name]['parameter']['service'] = array(
        'type' => 'hidden',
        'default value' => $this->name,
      );
      $actions[$name]['parameter']['operation'] = array(
        'type' => 'hidden',
        'default value' => $name,
      );

      // Pass through the service result as provided variable.
      if (!empty($actions[$name]['result'])) {
        $actions[$name]['provides']['result'] = $actions[$name]['result'];
        unset($actions[$name]['result']);
      }
    }
    return $actions;
  }
  public function clearCache() {
    $this
      ->endpoint()
      ->clearCache();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::save public function Permanently saves the entity. Overrides EntityInterface::save
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
WSClientServiceDescription::$datatypes public property
WSClientServiceDescription::$endpoint protected property
WSClientServiceDescription::$global_header_parameters public property
WSClientServiceDescription::$global_parameters public property
WSClientServiceDescription::$label public property
WSClientServiceDescription::$name public property
WSClientServiceDescription::$operations public property
WSClientServiceDescription::$settings public property
WSClientServiceDescription::$type public property
WSClientServiceDescription::$url public property
WSClientServiceDescription::access public function Determines access to the web service.
WSClientServiceDescription::actions public function Returns info about the actions of the web service.
WSClientServiceDescription::clearCache public function
WSClientServiceDescription::dataTypes public function Returns info about the data types of the web service.
WSClientServiceDescription::endpoint public function Returns the associated web service endpoint object.
WSClientServiceDescription::invoke public function Invoke a service via its endpoint.
WSClientServiceDescription::__call public function Magic method to catch service invocations.
WSClientServiceDescription::__construct function Overrides Entity::__construct