public function WSCall::call in Web Service Data 2.0.x
Same name and namespace in other branches
- 8 src/Entity/WSCall.php \Drupal\wsdata\Entity\WSCall::call()
Make the web service call.
Overrides WSCallInterface::call
File
- src/
Entity/ WSCall.php, line 123
Class
- WSCall
- Defines the Web Service Call entity.
Namespace
Drupal\wsdata\EntityCode
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;
}
// Build out the Cache ID based on the parameters passed.
$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;
// Try to retrieve the data from cache.
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);
}
// Try to make the call.
$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,
];
// Encode the payload data.
$this->wsencoderInst
->encode($data, $replacements, $options['path'], $context);
// Call the connector.
$result = $conn
->call($options, $method, $replacements, $data, $tokens);
$this->status['call-status'] = $conn
->getStatus();
// Handle error case.
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;
// Fetch the cache tags for this call and the server instance call.
$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;
}