You are here

class MockHttpClientFactory in Apigee Edge 8

Class MockHttpClientFactory.

Hierarchy

Expanded class hierarchy of MockHttpClientFactory

1 string reference to 'MockHttpClientFactory'
apigee_mock_api_client.services.yml in tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
1 service uses MockHttpClientFactory
apigee_mock_api_client.mock_http_client_factory in tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
Drupal\apigee_mock_api_client\MockHttpClientFactory

File

tests/modules/apigee_mock_api_client/src/MockHttpClientFactory.php, line 31

Namespace

Drupal\apigee_mock_api_client
View source
class MockHttpClientFactory extends ClientFactory {

  /**
   * The handler stack (retained for compatibility).
   *
   * @var \GuzzleHttp\HandlerStack
   */
  protected $stack;

  /**
   * The mock handler stack (Allows us to queue responses).
   *
   * @var \GuzzleHttp\HandlerStack
   */
  protected $mock_stack;

  /**
   * Whether or not integration is currently enabled.
   *
   * @var bool
   */
  protected $integration_enabled;

  /**
   * Constructs a new ClientFactory instance.
   *
   * @param \GuzzleHttp\HandlerStack $stack
   *   The handler stack.
   * @param \Apigee\MockClient\GuzzleHttp\MockHandler $mock_stack
   *   The mock handler stack (Allows us to queue responses).
   * @param \Drupal\Core\State\StateInterface $state
   *   Drupal state service, used to determine whether tests should be run
   *   using the mock handler or against a remote edge instance.
   */
  public function __construct(HandlerStack $stack, MockHandler $mock_stack, StateInterface $state) {
    $this->stack = $stack;
    $this->mock_stack = $mock_stack;

    // Check for the integration enabled environment variable.
    if ($enabled = getenv('APIGEE_INTEGRATION_ENABLE')) {
      $this->integration_enabled = !empty($enabled);

      // Callbacks won't have access to the same environment variables so save
      // the flag to state.
      $state
        ->set('APIGEE_INTEGRATION_ENABLE', $enabled);
    }
    else {
      $this->integration_enabled = !empty($state
        ->get('APIGEE_INTEGRATION_ENABLE', FALSE));
    }
    parent::__construct($stack);
  }

  /**
   * {@inheritdoc}
   */
  public function fromOptions(array $config = []) {
    $config = [
      'handler' => $this->integration_enabled ? $this->stack : $this->mock_stack,
    ];
    return parent::fromOptions($config);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockHttpClientFactory::$integration_enabled protected property Whether or not integration is currently enabled.
MockHttpClientFactory::$mock_stack protected property The mock handler stack (Allows us to queue responses).
MockHttpClientFactory::$stack protected property The handler stack (retained for compatibility). Overrides ClientFactory::$stack
MockHttpClientFactory::fromOptions public function Constructs a new client object from some configuration. Overrides ClientFactory::fromOptions
MockHttpClientFactory::__construct public function Constructs a new ClientFactory instance. Overrides ClientFactory::__construct