/** * @param ServiceClientInterface $client Client that executes commands. * @param CommandInterface $command Command being executed. * @param array $context Command context array of data. */ public function __construct(ServiceClientInterface $client, CommandInterface $command, array $context = []) { $this->serviceClient = $client; $this->client = $client->getHttpClient(); $this->command = $command; $this->context = new Collection($context); $this->state = 'init'; }
/** * Sends multiple commands concurrently and returns a hash map of commands * mapped to their corresponding result or exception. * * Note: This method keeps every command and command and result in memory, * and as such is NOT recommended when sending a large number or an * indeterminable number of commands concurrently. Instead, you should use * executeAll() and utilize the event system to work with results. * * @param ServiceClientInterface $client * @param array|\Iterator $commands Commands to send. * @param array $options Passes through the options available * in {@see ServiceClientInterface::createPool()} * * @return BatchResults * @throws \InvalidArgumentException if the event format is incorrect. */ public static function batch(ServiceClientInterface $client, $commands, array $options = []) { $hash = new \SplObjectStorage(); foreach ($commands as $command) { $hash->attach($command); } $client->executeAll($commands, RequestEvents::convertEventArray($options, ['process'], ['priority' => RequestEvents::LATE, 'fn' => function (ProcessEvent $e) use($hash) { if ($e->getException()) { $hash[$e->getCommand()] = $e->getException(); } else { $hash[$e->getCommand()] = $e->getResult(); } }])); return new BatchResults($hash); }
private function prepareEvent(ProcessEvent $event, $commandName, array $errors = []) { $this->command->expects($this->once())->method('getName')->will($this->returnValue($commandName)); $description = $this->getMock('GuzzleHttp\\Command\\Guzzle\\DescriptionInterface'); $operation = new Operation(['errorResponses' => $errors], $description); $description->expects($this->once())->method('getOperation')->with($commandName)->will($this->returnValue($operation)); $this->serviceClient->expects($this->once())->method('getDescription')->will($this->returnValue($description)); }
/** * Create a request for an operation with a uri merged onto a base URI */ private function createCommandWithUri(Operation $operation, CommandInterface $command, ServiceClientInterface $client) { // Get the path values and use the client config settings $variables = []; foreach ($operation->getParams() as $name => $arg) { /* @var Parameter $arg */ if ($arg->getLocation() == 'uri') { if (isset($command[$name])) { $variables[$name] = $arg->filter($command[$name]); if (!is_array($variables[$name])) { $variables[$name] = (string) $variables[$name]; } } } } // Expand the URI template. $uri = Utils::uriTemplate($operation->getUri(), $variables); return $client->getHttpClient()->createRequest($operation->getHttpMethod(), $this->description->getBaseUrl()->combine($uri), $command['request_options'] ?: []); }