You are here

public function MockHandler::__invoke in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/guzzle/src/Handler/MockHandler.php \GuzzleHttp\Handler\MockHandler::__invoke()

File

vendor/guzzlehttp/guzzle/src/Handler/MockHandler.php, line 62

Class

MockHandler
Handler that returns responses or throw exceptions from a queue.

Namespace

GuzzleHttp\Handler

Code

public function __invoke(RequestInterface $request, array $options) {
  if (!$this->queue) {
    throw new \OutOfBoundsException('Mock queue is empty');
  }
  if (isset($options['delay'])) {
    usleep($options['delay'] * 1000);
  }
  $this->lastRequest = $request;
  $this->lastOptions = $options;
  $response = array_shift($this->queue);
  if (is_callable($response)) {
    $response = $response($request, $options);
  }
  $response = $response instanceof \Exception ? new RejectedPromise($response) : \GuzzleHttp\Promise\promise_for($response);
  return $response
    ->then(function ($value) use ($request, $options) {
    $this
      ->invokeStats($request, $options, $value);
    if ($this->onFulfilled) {
      call_user_func($this->onFulfilled, $value);
    }
    return $value;
  }, function ($reason) use ($request, $options) {
    $this
      ->invokeStats($request, $options, null, $reason);
    if ($this->onRejected) {
      call_user_func($this->onRejected, $reason);
    }
    return new RejectedPromise($reason);
  });
}