View source
<?php
namespace Drupal\wsdata\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class WSCall extends ConfigEntityBase implements WSCallInterface {
use StringTranslationTrait;
protected $id;
protected $label;
public $options;
public $wsserver;
public $wsdecoder;
public $wsencoder;
protected $wsserverInst;
protected $wsdecoderInst;
protected $wsencoderInst;
protected $status;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
if ($this->wsserver) {
$this->wsserverInst = \Drupal::service('entity_type.manager')
->getStorage('wsserver')
->load($this->wsserver);
}
if ($this->wsdecoder) {
$wsdecoderManager = \Drupal::service('plugin.manager.wsdecoder');
$this->wsdecoderInst = $wsdecoderManager
->createInstance($this->wsdecoder);
}
if ($this->wsencoder) {
$wsencoderManager = \Drupal::service('plugin.manager.wsencoder');
$this->wsencoderInst = $wsencoderManager
->createInstance($this->wsencoder);
}
$this->status = [];
}
public function setEndpoint($endpoint) {
if ($this->wsserverInst) {
$this->wsserverInst
->setEndpoint($endpoint);
}
}
public function getEndpoint() {
return $this->wsserverInst ? $this->wsserverInst
->getEndpoint() : FALSE;
}
public function call($method = NULL, $replacements = [], $data = NULL, $options = [], $key = NULL, $tokens = [], $cache_tag = [], &$context = []) {
$this->status = [
'method' => $method,
'status' => 'called',
];
if (!$this->wsserverInst) {
$this->status['status'] = 'error';
$this->status['error_message'] = $this
->t('No WSServer Instance to found.');
$this->status['error'] = TRUE;
return FALSE;
}
$conn = $this
->getConnector();
$cid_array = array_merge($this
->getOptions(), $options, $replacements, $tokens, [
'data' => $data,
'key' => $key,
'conn' => $conn
->getCache(),
]);
$cid = md5(serialize($cid_array));
$this->status['cache']['cid'] = $cid;
$this->status['cache']['cached'] = FALSE;
$this->status['called'] = FALSE;
if ($cache = \Drupal::cache('wsdata')
->get($cid)) {
$this->status['status'] = 'success';
$this->status['cache']['cached'] = FALSE;
$cache_data = $cache->data;
if ($this->wsdecoderInst
->isCacheable()) {
$this->status['cache']['debug'] = $this
->t('Returning parsed data from cache');
return $cache_data;
}
$this->status['cache']['debug'] = $this
->t('Loaded WSCall result from cache and re-parsed the data');
$this
->addData($cache_data);
return $this
->getData($key);
}
$options = array_merge($this
->getOptions(), $options);
if ($method and !in_array($method, $conn
->getMethods())) {
throw new WSDataInvalidMethodException(sprintf('Invalid method %s on connector type %s', $method, $this->wsserverInst->wsconnector));
}
elseif (isset($options['method']) and in_array($options['method'], $conn
->getMethods())) {
$method = $options['method'];
}
else {
$methods = $conn
->getMethods();
$method = reset($methods);
}
$context = [
'replacements' => $replacements,
'data' => $data,
'options' => &$options,
'key' => $key,
'tokens' => $tokens,
];
$this->wsencoderInst
->encode($data, $replacements, $options['path'], $context);
$result = $conn
->call($options, $method, $replacements, $data, $tokens);
$this->status['call-status'] = $conn
->getStatus();
if (!empty($conn
->getError())) {
$this->status['error'] = TRUE;
$message = $this
->t('wsdata %wsdata_name failed with error %code %message', [
'%wsdata_name' => $this->id,
'%code' => $conn
->getError()['code'],
'%message' => $conn
->getError()['message'],
]);
$this->status['error_message'] = $message;
\Drupal::logger('wsdata')
->error($message);
return FALSE;
}
$this
->addData($result, $context);
$data = $this
->getData($key);
$result_expires = (int) $conn
->expires();
$this->status['called'] = TRUE;
$this->status['cache']['wsencoder'] = $this->wsencoderInst
->isCacheable();
$this->status['cache']['wsdecoder'] = $this->wsdecoderInst
->isCacheable();
$this->status['cache']['wsconnect'] = $conn
->supportsCaching($method);
$this->status['cache']['expires'] = $result_expires;
$expires = time() + $result_expires;
$cache_tags = array_merge($this->wsserverInst
->getCacheTags(), $this
->getCacheTags(), $cache_tag);
$this->status['cache']['tags'] = $cache_tags;
if ($conn
->supportsCaching($method) && $this->wsencoderInst
->isCacheable()) {
if ($this->wsdecoderInst
->isCacheable()) {
$this->status['cache']['debug'] = $this
->t('Caching the parsed results of the WSCall for %ex seconds', [
'%ex' => $result_expires,
]);
\Drupal::cache('wsdata')
->set($cid, $data, $expires, $cache_tags);
}
else {
$this->status['cache']['debug'] = $this
->t('Caching the verbatim result of the WSCall for %ex seconds.', [
'%ex' => $result_expires,
]);
\Drupal::cache('wsdata')
->set($cid, $result, $expires, $cache_tags);
}
}
else {
$this->status['cache']['debug'] = $this
->t('Result is not cachable');
}
return $data;
}
public function setOptions($values = []) {
if (!isset($this->wsserverInst)) {
$this->wsserverInst = \Drupal::service('entity_type.manager')
->getStorage('wsserver')
->load($values['wsserver']);
}
$this->options[$this->wsserver] = $this->wsserverInst->wsconnectorInst
->saveOptions($values);
$this->needSave = TRUE;
}
public function lastCallStatus() {
return $this->status;
}
public function getReplacements() {
return $this->wsserverInst ? $this->wsserverInst->wsconnectorInst
->getReplacements($this
->getOptions()) : FALSE;
}
public function getOptionsForm($wsserver = NULL, $options = []) {
if (isset($wsserver)) {
$wsserverInst = \Drupal::service('entity_type.manager')
->getStorage('wsserver')
->load($wsserver);
return $wsserverInst->wsconnectorInst
->getOptionsForm($options);
}
if (isset($this->wsserverInst->wsconnectorInst)) {
return $this->wsserverInst->wsconnectorInst
->getOptionsForm($options);
}
}
public function getOptions() {
return isset($this->options[$this->wsserver]) ? $this->options[$this->wsserver] : [];
}
public function getMethods() {
return $this->wsserverInst ? $this->wsserverInst
->getMethods() : [];
}
public function addData($data, $context = []) {
if (!isset($this->wsdecoderInst)) {
$wsdecoderManager = \Drupal::service('plugin.manager.wsdecoder');
$this->wsdecoderInst = $wsdecoderManager
->createInstance($this->wsdecoder);
}
return $this->wsdecoderInst
->addData($data, NULL, $context);
}
public function getData($key = NULL) {
if (isset($this->wsdecoderInst)) {
return $this->wsdecoderInst
->getData($key);
}
return NULL;
}
public function getConnector() {
return $this->wsserverInst ? $this->wsserverInst
->getConnector() : FALSE;
}
}