You are here

class RemoteManager in Entity Share 8.3

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Service/RemoteManager.php \Drupal\entity_share_client\Service\RemoteManager
  2. 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

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\Service
View 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

Namesort descending Modifiers Type Description Overrides
RemoteManager::$httpClients protected property HTTP clients prepared per remote.
RemoteManager::$jsonApiHttpClients protected property HTTP clients prepared for JSON:API endpoints per remotes.
RemoteManager::$logger protected property Logger.
RemoteManager::$remoteInfos protected property Data provided by entity_share entry point per remote.
RemoteManager::doRequest protected function Performs a HTTP request. 1
RemoteManager::getChannelsInfos public function Get the channels infos of a remote website. Overrides RemoteManagerInterface::getChannelsInfos
RemoteManager::getfieldMappings public function Get the field mappings of a remote website. Overrides RemoteManagerInterface::getfieldMappings
RemoteManager::getHttpClient protected function Prepares a client object from the auth plugin.
RemoteManager::getJsonApiHttpClient protected function Prepares a client object from the auth plugin.
RemoteManager::jsonApiRequest public function Performs a HTTP request on a JSON:API endpoint. Wraps the HTTP client. Overrides RemoteManagerInterface::jsonApiRequest
RemoteManager::JSON_API_CLIENT constant A constant to document the call for a JSON:API client.
RemoteManager::request public function Performs a HTTP request. Wraps the HTTP client. Overrides RemoteManagerInterface::request
RemoteManager::STANDARD_CLIENT constant A constant to document the call for a standard client.
RemoteManager::__construct public function RemoteManager constructor.