You are here

HttpClient.php in HTTP Client Manager 8

Same filename and directory in other branches
  1. 8.2 src/HttpClient.php

File

src/HttpClient.php
View source
<?php

namespace Drupal\http_client_manager;

use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class HttpClient implements HttpClientInterface {

  /**
   * The name of the service api of this http client instance.
   *
   * @var string
   */
  protected $serviceApi;

  /**
   * The Http Service Api Handler service.
   *
   * @var HttpServiceApiHandler
   */
  protected $apiHandler;

  /**
   * An array containing the Http Service Api description.
   *
   * @var array
   */
  protected $api;

  /**
   * Guzzle Client definition.
   *
   * @var \Guzzle\Service\Client
   */
  protected $client;

  /**
   * Event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs an HttpClient object
   *
   * @param string $serviceApi
   *   The service api name for this instance.
   * @param \Drupal\http_client_manager\HttpServiceApiHandlerInterface $apiHandler
   *   The service api handler instance.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher instance.
   */
  public function __construct($serviceApi, HttpServiceApiHandlerInterface $apiHandler, EventDispatcherInterface $event_dispatcher) {
    $this->serviceApi = $serviceApi;
    $this->apiHandler = $apiHandler;
    $this->api = $this->apiHandler
      ->load($this->serviceApi);
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public function getApi() {
    return $this->api;
  }

  /**
   * Get Client.
   *
   * @return \Guzzle\Service\Client
   *   The Configured Guzzle client instance.
   */
  protected function getClient() {
    if (empty($this->client)) {
      $this
        ->setupGuzzleClient();
    }
    return $this->client;
  }

  /**
   * Setup Guzzle Client from *.http_services_api.yml files.
   */
  private function setupGuzzleClient() {
    $api = $this
      ->getApi();
    $this->client = new Client($api['base_url'], $api['config']);
    $this->client
      ->setDescription(ServiceDescription::factory($api['source']));
    $this->client
      ->setEventDispatcher($this->eventDispatcher);
  }

  /**
   * {@inheritdoc}
   */
  public function getCommands() {
    return $this
      ->getClient()
      ->getDescription()
      ->getOperations();
  }

  /**
   * {@inheritdoc}
   */
  public function getCommand($commandName) {
    return $this
      ->getClient()
      ->getCommand($commandName)
      ->getOperation();
  }

  /**
   * {@inheritdoc}
   */
  public function call($commandName, array $params = []) {
    return $this
      ->getClient()
      ->getCommand($commandName, $params)
      ->execute();
  }

  /**
   * Magic method implementation for commands execution.
   *
   * @param string $name
   *  The Guzzle command name.
   * @param array $arguments
   *  The Guzzle command parameters array.
   *
   * @return \Guzzle\Service\Resource\Model|array
   *   The Guzzle Command execution result.
   *
   * @see HttpClientInterface::call
   */
  public function __call($name, array $arguments = []) {
    $params = !empty($arguments[0]) ? $arguments[0] : [];
    return $this
      ->call($name, $params);
  }

}

Classes

Namesort descending Description
HttpClient