RemoteManager.php in Entity Share 8.3
File
modules/entity_share_client/src/Service/RemoteManager.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Service;
use Drupal\Component\Serialization\Json;
use Drupal\entity_share_client\Entity\RemoteInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use Psr\Log\LoggerInterface;
class RemoteManager implements RemoteManagerInterface {
const STANDARD_CLIENT = FALSE;
const JSON_API_CLIENT = TRUE;
protected $logger;
protected $httpClients = [];
protected $jsonApiHttpClients = [];
protected $remoteInfos = [];
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function request(RemoteInterface $remote, $method, $url) {
$client = $this
->getHttpClient($remote);
return $this
->doRequest($client, $method, $url);
}
public function jsonApiRequest(RemoteInterface $remote, $method, $url) {
$client = $this
->getJsonApiHttpClient($remote);
return $this
->doRequest($client, $method, $url);
}
public function getChannelsInfos(RemoteInterface $remote) {
$remote_id = $remote
->id();
if (!isset($this->remoteInfos[$remote_id])) {
$response = $this
->jsonApiRequest($remote, 'GET', 'entity_share');
$json = [
'data' => [
'channels' => [],
'field_mappings' => [],
],
];
if (!is_null($response)) {
$json = Json::decode((string) $response
->getBody());
}
$this->remoteInfos[$remote_id] = $json['data'];
}
return $this->remoteInfos[$remote_id]['channels'];
}
public function getfieldMappings(RemoteInterface $remote) {
$remote_id = $remote
->id();
if (!isset($this->remoteInfos[$remote_id])) {
$response = $this
->jsonApiRequest($remote, 'GET', 'entity_share');
$json = Json::decode((string) $response
->getBody());
$this->remoteInfos[$remote_id] = $json['data'];
}
return $this->remoteInfos[$remote_id]['field_mappings'];
}
protected function getHttpClient(RemoteInterface $remote) {
$remote_id = $remote
->id();
if (!isset($this->httpClients[$remote_id])) {
$this->httpClients[$remote_id] = $remote
->getHttpClient(self::STANDARD_CLIENT);
}
return $this->httpClients[$remote_id];
}
protected function getJsonApiHttpClient(RemoteInterface $remote) {
$remote_id = $remote
->id();
if (!isset($this->jsonApiHttpClients[$remote_id])) {
$this->jsonApiHttpClients[$remote_id] = $remote
->getHttpClient(self::JSON_API_CLIENT);
}
return $this->jsonApiHttpClients[$remote_id];
}
protected function doRequest(ClientInterface $client, $method, $url) {
$log_variables = [
'@url' => $url,
'@method' => $method,
];
try {
return $client
->request($method, $url);
} catch (ClientException $exception) {
$log_variables['@exception_message'] = $exception
->getMessage();
$this->logger
->error('Client exception when requesting the URL: @url with method @method: @exception_message', $log_variables);
} catch (ServerException $exception) {
$log_variables['@exception_message'] = $exception
->getMessage();
$this->logger
->error('Server exception when requesting the URL: @url with method @method: @exception_message', $log_variables);
} catch (GuzzleException $exception) {
$log_variables['@exception_message'] = $exception
->getMessage();
$this->logger
->error('Guzzle exception when requesting the URL: @url with method @method: @exception_message', $log_variables);
} catch (\Exception $exception) {
$log_variables['@exception_message'] = $exception
->getMessage();
$this->logger
->error('Error when requesting the URL: @url with method @method: @exception_message', $log_variables);
}
return NULL;
}
}