/** * {@inheritdoc} */ protected function sendInternalRequests(array $internalRequests, $success, $error) { if (!empty($internalRequests)) { $this->eventDispatcher->dispatch(Events::MULTI_PRE_SEND, $multiPreSendEvent = new MultiPreSendEvent($this, $internalRequests)); $internalRequests = $multiPreSendEvent->getRequests(); } $exceptions = array(); try { $responses = $this->decorate('sendRequests', array($internalRequests)); } catch (MultiHttpAdapterException $e) { $responses = $e->getResponses(); $exceptions = $e->getExceptions(); } if (!empty($responses)) { $this->eventDispatcher->dispatch(Events::MULTI_POST_SEND, $postSendEvent = new MultiPostSendEvent($this, $responses)); $exceptions = array_merge($exceptions, $postSendEvent->getExceptions()); $responses = $postSendEvent->getResponses(); } if (!empty($exceptions)) { $this->eventDispatcher->dispatch(Events::MULTI_EXCEPTION, $exceptionEvent = new MultiExceptionEvent($this, $exceptions)); $responses = array_merge($responses, $exceptionEvent->getResponses()); $exceptions = $exceptionEvent->getExceptions(); } foreach ($responses as $response) { call_user_func($success, $response); } foreach ($exceptions as $exception) { call_user_func($error, $exception); } }
/** * Sends internal requests. * * @param array $internalRequests The internal requests. * * @throws \Ivory\HttpAdapter\MultiHttpAdapterException If an error occurred. * * @return array The responses. */ private function sendInternalRequests(array $internalRequests) { if (!empty($internalRequests) && $this->configuration->hasEventDispatcher()) { $this->configuration->getEventDispatcher()->dispatch(Events::MULTI_PRE_SEND, $multiPreSendEvent = new MultiPreSendEvent($this, $internalRequests)); $internalRequests = $multiPreSendEvent->getRequests(); } $responses = array(); $exceptions = array(); $successHandler = function (ResponseInterface $response) use(&$responses) { $responses[] = $response; }; $errorHandler = function (HttpAdapterException $exception) use(&$exceptions) { $exceptions[] = $exception; }; $this->doSendInternalRequests($internalRequests, $successHandler, $errorHandler); if (!empty($responses) && $this->configuration->hasEventDispatcher()) { $this->configuration->getEventDispatcher()->dispatch(Events::MULTI_POST_SEND, $postSendEvent = new MultiPostSendEvent($this, $responses)); $exceptions = array_merge($exceptions, $postSendEvent->getExceptions()); $responses = $postSendEvent->getResponses(); } if (!empty($exceptions)) { if ($this->configuration->hasEventDispatcher()) { $this->configuration->getEventDispatcher()->dispatch(Events::MULTI_EXCEPTION, $exceptionEvent = new MultiExceptionEvent($this, $exceptions)); $responses = array_merge($responses, $exceptionEvent->getResponses()); $exceptions = $exceptionEvent->getExceptions(); } if (!empty($exceptions)) { throw new MultiHttpAdapterException($exceptions, $responses); } } return $responses; }