/** * Queues up mock Result objects for a client * * @param SdkClientInterface $client * @param Result[]|array[] $results * * @return SdkClientInterface */ private function addMockResults(SdkClientInterface $client, array $results) { $client->getEmitter()->on('prepared', function (PreparedEvent $event) use(&$results) { $result = array_shift($results); if (is_array($result)) { $event->intercept(new Result($result)); } elseif ($result instanceof Result) { $event->intercept($result); } elseif ($result instanceof CommandException) { throw $result; } else { throw new \Exception('There are no more mock results left. This client executed more commands than expected.'); } }, 'last'); return $client; }
/** * Creates and attaches parsers given client based on the protocol of the * description. * * @param SdkClientInterface $client SDK client to update * * @throws \UnexpectedValueException if the protocol doesn't exist */ protected function applyParser(SdkClientInterface $client) { $api = $client->getApi(); $parser = Service::createParser($api); $errorParser = Service::createErrorParser($api->getProtocol()); $client->getEmitter()->on('process', function (ProcessEvent $e) use($errorParser) { // Ensure a response exists in order to parse. $response = $e->getResponse(); if (!$response) { throw new \RuntimeException('No response was received.'); } /* @var \SellerCenter\SDK\Common\Api\ErrorParser\XmlErrorParser $errorParser */ $result = $errorParser($response, $e); if ($result instanceof Api\Response\Error\ErrorResponse) { $e->setResult($result); $e->stopPropagation(); } }); $client->getEmitter()->on('process', function (ProcessEvent $e) use($parser, $api) { // Guard against exceptions and injected results. if ($e->getException() || $e->getResult()) { return; } // Ensure a response exists in order to parse. $response = $e->getResponse(); if (!$response) { throw new \RuntimeException('No response was received.'); } $operation = $api->getOperation($e->getCommand()->getName()); $result = $parser($response, $operation['deserialize']); $e->setResult($result); }); }