You are here

class RemoteManager in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 modules/entity_share_client/src/Service/RemoteManager.php \Drupal\entity_share_client\Service\RemoteManager
  2. 8 modules/entity_share_client/src/Service/RemoteManager.php \Drupal\entity_share_client\Service\RemoteManager

Class RemoteManager.

@package Drupal\entity_share_client\Service

Hierarchy

Expanded class hierarchy of RemoteManager

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 16

Namespace

Drupal\entity_share_client\Service
View source
class RemoteManager implements RemoteManagerInterface {

  /**
   * The HTTP client factory.
   *
   * @var \Drupal\Core\Http\ClientFactory
   */
  protected $httpClientFactory;

  /**
   * The request service.
   *
   * @var \Drupal\entity_share_client\Service\RequestServiceInterface
   */
  protected $requestService;

  /**
   * RemoteManager constructor.
   *
   * @param \Drupal\Core\Http\ClientFactory $http_client_factory
   *   The HTTP client factory.
   * @param \Drupal\entity_share_client\Service\RequestServiceInterface $request_service
   *   The request service.
   */
  public function __construct(ClientFactory $http_client_factory, RequestServiceInterface $request_service) {
    $this->httpClientFactory = $http_client_factory;
    $this->requestService = $request_service;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareClient(RemoteInterface $remote) {
    $http_client = $this->httpClientFactory
      ->fromOptions([
      'base_uri' => $remote
        ->get('url') . '/',
      'cookies' => TRUE,
      'allow_redirects' => TRUE,
    ]);
    $http_client
      ->post('user/login', [
      'form_params' => [
        'name' => $remote
          ->get('basic_auth_username'),
        'pass' => $remote
          ->get('basic_auth_password'),
        'form_id' => 'user_login_form',
      ],
    ]);
    return $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareJsonApiClient(RemoteInterface $remote) {
    return $this->httpClientFactory
      ->fromOptions([
      'base_uri' => $remote
        ->get('url') . '/',
      'auth' => [
        $remote
          ->get('basic_auth_username'),
        $remote
          ->get('basic_auth_password'),
      ],
      'headers' => [
        'Content-type' => 'application/vnd.api+json',
      ],
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getChannelsInfos(RemoteInterface $remote) {
    $http_client = $this
      ->prepareJsonApiClient($remote);
    $response = $this->requestService
      ->request($http_client, 'GET', 'entity_share');
    $json = Json::decode((string) $response
      ->getBody());
    return $json['data']['channels'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RemoteManager::$httpClientFactory protected property The HTTP client factory.
RemoteManager::$requestService protected property The request service.
RemoteManager::getChannelsInfos public function Get the channels infos of a remote website. Overrides RemoteManagerInterface::getChannelsInfos
RemoteManager::prepareClient public function Prepare an HTTP client authenticated to handle private files. Overrides RemoteManagerInterface::prepareClient
RemoteManager::prepareJsonApiClient public function Prepare an HTTP client for the JSON:API endpoints. Overrides RemoteManagerInterface::prepareJsonApiClient
RemoteManager::__construct public function RemoteManager constructor.