class RemoteManager in Entity Share 8.3
Same name and namespace in other branches
- 8 modules/entity_share_client/src/Service/RemoteManager.php \Drupal\entity_share_client\Service\RemoteManager
- 8.2 modules/entity_share_client/src/Service/RemoteManager.php \Drupal\entity_share_client\Service\RemoteManager
Service to wrap requests logic.
@package Drupal\entity_share_client\Service
Hierarchy
- class \Drupal\entity_share_client\Service\RemoteManager implements RemoteManagerInterface
Expanded class hierarchy of RemoteManager
1 file declares its use of RemoteManager
- TestRemoteManager.php in modules/
entity_share_client/ tests/ modules/ entity_share_client_remote_manager_test/ src/ Service/ TestRemoteManager.php
1 string reference to 'RemoteManager'
- entity_share_client.services.yml in modules/
entity_share_client/ entity_share_client.services.yml - modules/entity_share_client/entity_share_client.services.yml
1 service uses RemoteManager
- entity_share_client.remote_manager in modules/
entity_share_client/ entity_share_client.services.yml - Drupal\entity_share_client\Service\RemoteManager
File
- modules/
entity_share_client/ src/ Service/ RemoteManager.php, line 20
Namespace
Drupal\entity_share_client\ServiceView source
class RemoteManager implements RemoteManagerInterface {
/**
* A constant to document the call for a standard client.
*
* @var bool
*/
const STANDARD_CLIENT = FALSE;
/**
* A constant to document the call for a JSON:API client.
*
* @var bool
*/
const JSON_API_CLIENT = TRUE;
/**
* Logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* HTTP clients prepared per remote.
*
* @var \GuzzleHttp\ClientInterface[]
*/
protected $httpClients = [];
/**
* HTTP clients prepared for JSON:API endpoints per remotes.
*
* @var \GuzzleHttp\ClientInterface[]
*/
protected $jsonApiHttpClients = [];
/**
* Data provided by entity_share entry point per remote.
*
* @var array
*/
protected $remoteInfos = [];
/**
* RemoteManager constructor.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
*/
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function request(RemoteInterface $remote, $method, $url) {
$client = $this
->getHttpClient($remote);
return $this
->doRequest($client, $method, $url);
}
/**
* {@inheritdoc}
*/
public function jsonApiRequest(RemoteInterface $remote, $method, $url) {
$client = $this
->getJsonApiHttpClient($remote);
return $this
->doRequest($client, $method, $url);
}
/**
* {@inheritdoc}
*/
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'];
}
/**
* {@inheritdoc}
*/
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'];
}
/**
* Prepares a client object from the auth plugin.
*
* @param \Drupal\entity_share_client\Entity\RemoteInterface $remote
* The remote website on which to perform the request.
*
* @return \GuzzleHttp\Client
* The configured client.
*/
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];
}
/**
* Prepares a client object from the auth plugin.
*
* @param \Drupal\entity_share_client\Entity\RemoteInterface $remote
* The remote website on which to perform the request.
*
* @return \GuzzleHttp\Client
* The configured client.
*/
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];
}
/**
* Performs a HTTP request.
*
* @param \GuzzleHttp\ClientInterface $client
* The client which will do the request.
* @param string $method
* HTTP method.
* @param string $url
* URL to request.
*
* @return \Psr\Http\Message\ResponseInterface||null
* The response or NULL if a problem occurred.
*
* @see \GuzzleHttp\ClientInterface::request()
*/
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;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RemoteManager:: |
protected | property | HTTP clients prepared per remote. | |
RemoteManager:: |
protected | property | HTTP clients prepared for JSON:API endpoints per remotes. | |
RemoteManager:: |
protected | property | Logger. | |
RemoteManager:: |
protected | property | Data provided by entity_share entry point per remote. | |
RemoteManager:: |
protected | function | Performs a HTTP request. | 1 |
RemoteManager:: |
public | function |
Get the channels infos of a remote website. Overrides RemoteManagerInterface:: |
|
RemoteManager:: |
public | function |
Get the field mappings of a remote website. Overrides RemoteManagerInterface:: |
|
RemoteManager:: |
protected | function | Prepares a client object from the auth plugin. | |
RemoteManager:: |
protected | function | Prepares a client object from the auth plugin. | |
RemoteManager:: |
public | function |
Performs a HTTP request on a JSON:API endpoint. Wraps the HTTP client. Overrides RemoteManagerInterface:: |
|
RemoteManager:: |
constant | A constant to document the call for a JSON:API client. | ||
RemoteManager:: |
public | function |
Performs a HTTP request. Wraps the HTTP client. Overrides RemoteManagerInterface:: |
|
RemoteManager:: |
constant | A constant to document the call for a standard client. | ||
RemoteManager:: |
public | function | RemoteManager constructor. |