Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractCommand
  • ClosureCommand
  • DefaultRequestSerializer
  • DefaultResponseParser
  • OperationCommand
  • OperationResponseParser

Interfaces

  • CommandInterface
  • RequestSerializerInterface
  • ResponseClassInterface
  • ResponseParserInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Service\Command;
  4: 
  5: use Guzzle\Http\Message\Response;
  6: use Guzzle\Service\Command\LocationVisitor\VisitorFlyweight;
  7: use Guzzle\Service\Command\LocationVisitor\Response\ResponseVisitorInterface;
  8: use Guzzle\Service\Description\Parameter;
  9: use Guzzle\Service\Description\OperationInterface;
 10: use Guzzle\Service\Description\Operation;
 11: use Guzzle\Service\Exception\ResponseClassException;
 12: use Guzzle\Service\Resource\Model;
 13: 
 14: /**
 15:  * Response parser that attempts to marshal responses into an associative array based on models in a service description
 16:  */
 17: class OperationResponseParser extends DefaultResponseParser
 18: {
 19:     /**
 20:      * @var VisitorFlyweight $factory Visitor factory
 21:      */
 22:     protected $factory;
 23: 
 24:     /**
 25:      * @var self
 26:      */
 27:     protected static $instance;
 28: 
 29:     /**
 30:      * Get a cached default instance of the Operation response parser that uses default visitors
 31:      *
 32:      * @return self
 33:      * @codeCoverageIgnore
 34:      */
 35:     public static function getInstance()
 36:     {
 37:         if (!static::$instance) {
 38:             static::$instance = new static(VisitorFlyweight::getInstance());
 39:         }
 40: 
 41:         return static::$instance;
 42:     }
 43: 
 44:     /**
 45:      * @param VisitorFlyweight $factory Factory to use when creating visitors
 46:      */
 47:     public function __construct(VisitorFlyweight $factory)
 48:     {
 49:         $this->factory = $factory;
 50:     }
 51: 
 52:     /**
 53:      * Add a location visitor to the command
 54:      *
 55:      * @param string                   $location Location to associate with the visitor
 56:      * @param ResponseVisitorInterface $visitor  Visitor to attach
 57:      *
 58:      * @return self
 59:      */
 60:     public function addVisitor($location, ResponseVisitorInterface $visitor)
 61:     {
 62:         $this->factory->addResponseVisitor($location, $visitor);
 63: 
 64:         return $this;
 65:     }
 66: 
 67:     /**
 68:      * {@inheritdoc}
 69:      */
 70:     protected function handleParsing(AbstractCommand $command, Response $response, $contentType)
 71:     {
 72:         $operation = $command->getOperation();
 73:         $type = $operation->getResponseType();
 74:         $model = null;
 75: 
 76:         if ($type == OperationInterface::TYPE_MODEL) {
 77:             $model = $operation->getServiceDescription()->getModel($operation->getResponseClass());
 78:         } elseif ($type == OperationInterface::TYPE_CLASS) {
 79:             $responseClassInterface = __NAMESPACE__ . '\ResponseClassInterface';
 80:             $className = $operation->getResponseClass();
 81:             if (!class_exists($className)) {
 82:                 throw new ResponseClassException("{$className} does not exist");
 83:             } elseif (!method_exists($className, 'fromCommand')) {
 84:                 throw new ResponseClassException("{$className} must implement {$responseClassInterface}");
 85:             }
 86:             return $className::fromCommand($command);
 87:         }
 88: 
 89:         if (!$model) {
 90:             // Return basic processing if the responseType is not model or the model cannot be found
 91:             return parent::handleParsing($command, $response, $contentType);
 92:         } elseif ($command->get(AbstractCommand::RESPONSE_PROCESSING) != AbstractCommand::TYPE_MODEL) {
 93:             // Returns a model with no visiting if the command response processing is not model
 94:             return new Model(parent::handleParsing($command, $response, $contentType), $model);
 95:         } else {
 96:             return new Model($this->visitResult($model, $command, $response), $model);
 97:         }
 98:     }
 99: 
100:     /**
101:      * Perform transformations on the result array
102:      *
103:      * @param Parameter        $model    Model that defines the structure
104:      * @param CommandInterface $command  Command that performed the operation
105:      * @param Response         $response Response received
106:      *
107:      * @return array Returns the array of result data
108:      */
109:     protected function visitResult(
110:         Parameter $model,
111:         CommandInterface $command,
112:         Response $response
113:     ) {
114:         $foundVisitors = $result = array();
115:         $props = $model->getProperties();
116: 
117:         foreach ($props as $schema) {
118:             if ($location = $schema->getLocation()) {
119:                 // Trigger the before method on the first found visitor of this type
120:                 if (!isset($foundVisitors[$location])) {
121:                     $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
122:                     $foundVisitors[$location]->before($command, $result);
123:                 }
124:             }
125:         }
126: 
127:         // Visit additional properties when it is an actual schema
128:         if ($additional = $model->getAdditionalProperties()) {
129:             if ($additional instanceof Parameter) {
130:                 // Only visit when a location is specified
131:                 if ($location = $additional->getLocation()) {
132:                     if (!isset($foundVisitors[$location])) {
133:                         $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
134:                         $foundVisitors[$location]->before($command, $result);
135:                     }
136:                     // Only traverse if an array was parsed from the before() visitors
137:                     if (is_array($result)) {
138:                         // Find each additional property
139:                         foreach (array_keys($result) as $key) {
140:                             // Check if the model actually knows this property. If so, then it is not additional
141:                             if (!$model->getProperty($key)) {
142:                                 // Set the name to the key so that we can parse it with each visitor
143:                                 $additional->setName($key);
144:                                 $foundVisitors[$location]->visit($command, $response, $additional, $result);
145:                             }
146:                         }
147:                         // Reset the additionalProperties name to null
148:                         $additional->setName(null);
149:                     }
150:                 }
151:             }
152:         }
153: 
154:         // Apply the parameter value with the location visitor
155:         foreach ($props as $schema) {
156:             if ($location = $schema->getLocation()) {
157:                 $foundVisitors[$location]->visit($command, $response, $schema, $result);
158:             }
159:         }
160: 
161:         // Call the after() method of each found visitor
162:         foreach ($foundVisitors as $visitor) {
163:             $visitor->after($command);
164:         }
165: 
166:         return $result;
167:     }
168: }
169: 
php-coveralls API documentation generated by ApiGen 2.8.0