Process a received response
array $context Contextual information:
BadResponseException on unsuccessful responses
protected function processResponse(array $context = array()) {
// Use the queued response if one is set
if ($this
->getParams()
->get('queued_response')) {
$this->response = $this
->getParams()
->get('queued_response');
$this->responseBody = $this->response
->getBody();
$this
->getParams()
->remove('queued_response');
}
elseif (!$this->response) {
// If no response, then processResponse shouldn't have been called
$e = new RequestException('Error completing request');
$e
->setRequest($this);
throw $e;
}
$this->state = self::STATE_COMPLETE;
// A request was sent, but we don't know if we'll send more or if the final response will be successful
$this
->dispatch('request.sent', $this
->getEventArray() + $context);
// Some response processors will remove the response or reset the state (example: ExponentialBackoffPlugin)
if ($this->state == RequestInterface::STATE_COMPLETE) {
// The request completed, so the HTTP transaction is complete
$this
->dispatch('request.complete', $this
->getEventArray());
// If the response is bad, allow listeners to modify it or throw exceptions. You can change the response by
// modifying the Event object in your listeners or calling setResponse() on the request
if ($this->response
->isError()) {
$event = new Event($this
->getEventArray());
$this
->getEventDispatcher()
->dispatch('request.error', $event);
// Allow events of request.error to quietly change the response
if ($event['response'] !== $this->response) {
$this->response = $event['response'];
}
}
// If a successful response was received, dispatch an event
if ($this->response
->isSuccessful()) {
$this
->dispatch('request.success', $this
->getEventArray());
}
}
return $this;
}