public function onInit(InitEvent $event)
 {
     $command = $event->getCommand();
     $errors = [];
     $operation = $this->description->getOperation($command->getName());
     foreach ($operation->getParams() as $name => $schema) {
         $value = $command[$name];
         if (!$this->validator->validate($schema, $value)) {
             $errors = array_merge($errors, $this->validator->getErrors());
         } elseif ($value !== $command[$name]) {
             // Update the config value if it changed and no validation
             // errors were encountered
             $command[$name] = $value;
         }
     }
     if ($params = $operation->getAdditionalParameters()) {
         foreach ($command->toArray() as $name => $value) {
             // It's only additional if it isn't defined in the schema
             if (!$operation->hasParam($name)) {
                 // Always set the name so that error messages are useful
                 $params->setName($name);
                 if (!$this->validator->validate($params, $value)) {
                     $errors = array_merge($errors, $this->validator->getErrors());
                 } elseif ($value !== $command[$name]) {
                     $command[$name] = $value;
                 }
             }
         }
     }
     if ($errors) {
         throw new CommandException('Validation errors: ' . implode("\n", $errors), $event->getTransaction());
     }
 }
Example #2
0
 /**
  * @param PreparedEvent $event
  * @return array
  */
 protected function getRequestOptions(PreparedEvent $event)
 {
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     $requestOptions = $operation->getData('requestOptions');
     return is_array($requestOptions) ? $requestOptions : array();
 }
 public function onProcess(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return;
     }
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     $rawConfig = $operation->toArray();
     if (empty($rawConfig['responseParser'])) {
         return;
     }
     $parsedResponse = $this->parseResponse($event, $rawConfig['responseParser']);
     $event->setResult($parsedResponse);
 }
 /**
  * Creates a callable function used to create command objects from a
  * service description.
  *
  * @param DescriptionInterface $description Service description
  *
  * @return callable Returns a command factory
  */
 public static function defaultCommandFactory(DescriptionInterface $description)
 {
     return function ($name, array $args = [], GuzzleClientInterface $client) use($description) {
         $operation = null;
         if ($description->hasOperation($name)) {
             $operation = $description->getOperation($name);
         } else {
             $name = ucfirst($name);
             if ($description->hasOperation($name)) {
                 $operation = $description->getOperation($name);
             }
         }
         if (!$operation) {
             return null;
         }
         return new Command($operation, $args, clone $client->getEmitter());
     };
 }
 /**
  * Stops propagation of ProcessEvents when using Batching
  *
  * @param  ProcessEvent $event The Process Event
  *
  * @return bool
  */
 public function onProcess(ProcessEvent $event)
 {
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     if (!$operation->getData('batching')) {
         return false;
     }
     $event->stopPropagation();
     return true;
 }
Example #6
0
 /**
  * Create a request for the command and operation
  *
  * @param CommandTransaction $trans
  *
  * @return RequestInterface
  * @throws \RuntimeException
  */
 protected function createRequest(CommandTransaction $trans)
 {
     $operation = $this->description->getOperation($trans->command->getName());
     // If the command does not specify a template, then assume the base URL
     // of the client
     if (null === ($uri = $operation->getUri())) {
         return $trans->client->createRequest($operation->getHttpMethod(), $this->description->getBaseUrl(), $trans->command['request_options'] ?: []);
     }
     return $this->createCommandWithUri($operation, $trans->command, $trans->serviceClient);
 }
 /**
  * @param ProcessEvent $event
  * @return ResponseInterface|null
  */
 protected function createResult(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return null;
     }
     $command = $event->getCommand();
     // Do not overwrite a previous result
     if ($event->getResult()) {
         return null;
     }
     $operation = $this->description->getOperation($command->getName());
     $responseClass = $operation->getResponseModel();
     if ($responseClass === null) {
         throw new Exception\RuntimeException(sprintf('No response class configured for operation "%s"', $command->getName()));
     }
     if (!class_exists($responseClass)) {
         throw new Exception\RuntimeException(sprintf('Response class "%s" of operation "%s" does not exist', $responseClass, $command->getName()));
     }
     /** @todo We could check if the response class implements ResponseInterface */
     /** @var ResponseInterface $responseClass */
     return $responseClass::fromOperation($operation, $event);
 }
 public function onProcess(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return;
     }
     $command = $event->getCommand();
     // Do not overwrite a previous result
     if ($event->getResult()) {
         return;
     }
     $operation = $this->description->getOperation($command->getName());
     // Add a default Model as the result if no matching schema was found.
     if (!($modelName = $operation->getResponseModel())) {
         $event->setResult([]);
         return;
     }
     $model = $operation->getServiceDescription()->getModel($modelName);
     if (!$model) {
         throw new \RuntimeException("Unknown model: {$modelName}");
     }
     $event->setResult($this->visit($model, $event));
 }