You are here

WSServer.php in Web Service Data 2.0.x

Same filename and directory in other branches
  1. 8 src/Entity/WSServer.php

File

src/Entity/WSServer.php
View source
<?php

namespace Drupal\wsdata\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;

/**
 * Defines the Web Service Server entity.
 *
 * @ConfigEntityType(
 *   id = "wsserver",
 *   label = @Translation("Web Service Server"),
 *   handlers = {
 *     "list_builder" = "Drupal\wsdata\WSServerListBuilder",
 *     "form" = {
 *       "add" = "Drupal\wsdata\Form\WSServerForm",
 *       "edit" = "Drupal\wsdata\Form\WSServerForm",
 *       "delete" = "Drupal\wsdata\Form\WSServerDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\wsdata\WSServerHtmlRouteProvider",
 *     },
 *   },
 *   config_prefix = "wsserver",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/wsserver/{wsserver}",
 *     "add-form" = "/admin/structure/wsserver/add",
 *     "edit-form" = "/admin/structure/wsserver/{wsserver}/edit",
 *     "delete-form" = "/admin/structure/wsserver/{wsserver}/delete",
 *     "collection" = "/admin/structure/wsserver"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "endpoint",
 *     "wsconnector",
 *     "languagehandling",
 *     "settings"
 *   }
 * )
 */
class WSServer extends ConfigEntityBase implements WSServerInterface {
  public static $WSCONFIG_DEFAULT_DEGRADED_BACKOFF = 900;

  /**
   * The Web Service Server ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The Web Service Server label.
   *
   * @var string
   */
  protected $label;
  public $endpoint;
  public $wsconnector;
  public $settings;
  public $wsconnectorInst;
  public $state;
  public $overrides;
  protected $languagehandling;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);
    $this->state = \Drupal::state()
      ->get('wsdata.wsserver.' . $this->id, []);
    $this->overrides = [];

    // Allow the state to override the configured endpoint.
    $this
      ->setEndpoint($this->endpoint);
    $wsconnectorman = \Drupal::service('plugin.manager.wsconnector');
    $wscdefs = $wsconnectorman
      ->getDefinitions();
    if (isset($wscdefs[$this->wsconnector])) {
      $this->wsconnectorInst = $wsconnectorman
        ->createInstance($this->wsconnector);
      $this->wsconnectorInst
        ->setEndpoint($this->endpoint);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __destruct() {
    if (!empty($this->id)) {
      \Drupal::state()
        ->set('wsdata.wsserver.' . $this->id, $this->state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMethods() {
    return $this->wsconnectorInst
      ->getMethods();
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultMethod() {
    $methods = array_keys($this
      ->getMethods());
    return reset($methods);
  }

  /**
   * {@inheritdoc}
   */
  public function setEndpoint($endpoint) {
    if (isset($this->state['endpoint'])) {
      $this->overrides['endpoint'] = $endpoint;
      $this->endpoint = $this->state['endpoint'];
    }
    else {
      $this->endpoint = $endpoint;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getEndpoint() {
    return $this->endpoint;
  }

  /**
   * {@inheritdoc}
   */
  public function disable($degraded = FALSE) {
    $reason = '';
    if ($degraded) {
      if (!isset($this->state['degraded_backoff'])) {
        $this->state['degraded_backoff'] = wsserver::$WSCONFIG_DEFAULT_DEGRADED_BACKOFF;
      }
      if ($this->state['degraded_backoff'] == 0) {
        return;
      }
      $reason = '  ' . t('Automatically disabled due to degrated service.');
      $this->state['degraded'] = time();
    }
    $this->state['disabled'] = TRUE;
    \Drupal::logger('wsdata')
      ->warning(t('WSServer %label (%type) was disabled.', [
      '%label' => $this
        ->label(),
      '%type' => $this->wsconnector,
    ]) . $reason);
  }

  /**
   * {@inheritdoc}
   */
  public function enable($degraded = FALSE) {
    unset($this->state['degraded']);
    unset($this->state['disabled']);
    $reason = '';
    if ($degraded) {
      $reason = '  ' . t('Automatically re-enabling previously degrated service.');
    }
    \Drupal::logger('wsdata')
      ->notice(t('WSConfig Type %label (%type) was enabled.', [
      '%label' => $this
        ->label(),
      '%type' => $this->wsconnector,
    ]) . $reason);
  }

  /**
   * {@inheritdoc}
   */
  public function isDisabled() {
    if (!isset($this->state['degraded_backoff'])) {
      $this->state['degraded_backoff'] = wsserver::$WSCONFIG_DEFAULT_DEGRADED_BACKOFF;
    }
    if (isset($this->state['degraded']) and $this->state['degraded'] < time() - $this->state['degraded_backoff']) {
      $this
        ->enable(TRUE);
      return FALSE;
    }
    return isset($this->state['disabled']) ? $this->state['disabled'] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getDegraded() {
    if (!isset($this->state['degraded_backoff'])) {
      $this->state['degraded_backoff'] = wsserver::$WSCONFIG_DEFAULT_DEGRADED_BACKOFF;
    }
    if (isset($this->state['degraded'])) {
      return $this->state['degraded'] - time() + $this->state['degraded_backoff'];
    }
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function getConnector() {
    return $this->wsconnectorInst;
  }

}

Classes

Namesort descending Description
WSServer Defines the Web Service Server entity.