WSServer.php in Web Service Data 2.0.x
File
src/Entity/WSServer.php
View source
<?php
namespace Drupal\wsdata\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
class WSServer extends ConfigEntityBase implements WSServerInterface {
public static $WSCONFIG_DEFAULT_DEGRADED_BACKOFF = 900;
protected $id;
protected $label;
public $endpoint;
public $wsconnector;
public $settings;
public $wsconnectorInst;
public $state;
public $overrides;
protected $languagehandling;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$this->state = \Drupal::state()
->get('wsdata.wsserver.' . $this->id, []);
$this->overrides = [];
$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);
}
}
public function __destruct() {
if (!empty($this->id)) {
\Drupal::state()
->set('wsdata.wsserver.' . $this->id, $this->state);
}
}
public function getMethods() {
return $this->wsconnectorInst
->getMethods();
}
public function getDefaultMethod() {
$methods = array_keys($this
->getMethods());
return reset($methods);
}
public function setEndpoint($endpoint) {
if (isset($this->state['endpoint'])) {
$this->overrides['endpoint'] = $endpoint;
$this->endpoint = $this->state['endpoint'];
}
else {
$this->endpoint = $endpoint;
}
}
public function getEndpoint() {
return $this->endpoint;
}
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);
}
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);
}
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;
}
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;
}
public function getConnector() {
return $this->wsconnectorInst;
}
}
Classes
Name |
Description |
WSServer |
Defines the Web Service Server entity. |