Open Journal Systems  3.3.0
OperationResponseParser.php
1 <?php
2 
3 namespace Guzzle\Service\Command;
4 
13 
18 {
20  protected $factory;
21 
23  protected static $instance;
24 
26  private $schemaInModels;
27 
32  public static function getInstance()
33  {
34  if (!static::$instance) {
35  static::$instance = new static(VisitorFlyweight::getInstance());
36  }
37 
38  return static::$instance;
39  }
40 
45  public function __construct(VisitorFlyweight $factory, $schemaInModels = false)
46  {
47  $this->factory = $factory;
48  $this->schemaInModels = $schemaInModels;
49  }
50 
59  public function addVisitor($location, ResponseVisitorInterface $visitor)
60  {
61  $this->factory->addResponseVisitor($location, $visitor);
62 
63  return $this;
64  }
65 
66  protected function handleParsing(CommandInterface $command, Response $response, $contentType)
67  {
68  $operation = $command->getOperation();
69  $type = $operation->getResponseType();
70  $model = null;
71 
73  $model = $operation->getServiceDescription()->getModel($operation->getResponseClass());
74  } elseif ($type == OperationInterface::TYPE_CLASS) {
75  return $this->parseClass($command);
76  }
77 
78  if (!$model) {
79  // Return basic processing if the responseType is not model or the model cannot be found
80  return parent::handleParsing($command, $response, $contentType);
82  // Returns a model with no visiting if the command response processing is not model
83  return new Model(parent::handleParsing($command, $response, $contentType));
84  } else {
85  // Only inject the schema into the model if "schemaInModel" is true
86  return new Model($this->visitResult($model, $command, $response), $this->schemaInModels ? $model : null);
87  }
88  }
89 
98  protected function parseClass(CommandInterface $command)
99  {
100  // Emit the operation.parse_class event. If a listener injects a 'result' property, then that will be the result
101  $event = new CreateResponseClassEvent(array('command' => $command));
102  $command->getClient()->getEventDispatcher()->dispatch('command.parse_response', $event);
103  if ($result = $event->getResult()) {
104  return $result;
105  }
106 
107  $className = $command->getOperation()->getResponseClass();
108  if (!method_exists($className, 'fromCommand')) {
109  throw new ResponseClassException("{$className} must exist and implement a static fromCommand() method");
110  }
111 
112  return $className::fromCommand($command);
113  }
114 
124  protected function visitResult(Parameter $model, CommandInterface $command, Response $response)
125  {
126  $foundVisitors = $result = $knownProps = array();
127  $props = $model->getProperties();
128 
129  foreach ($props as $schema) {
130  if ($location = $schema->getLocation()) {
131  // Trigger the before method on the first found visitor of this type
132  if (!isset($foundVisitors[$location])) {
133  $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
134  $foundVisitors[$location]->before($command, $result);
135  }
136  }
137  }
138 
139  // Visit additional properties when it is an actual schema
140  if (($additional = $model->getAdditionalProperties()) instanceof Parameter) {
141  $this->visitAdditionalProperties($model, $command, $response, $additional, $result, $foundVisitors);
142  }
143 
144  // Apply the parameter value with the location visitor
145  foreach ($props as $schema) {
146  $knownProps[$schema->getName()] = 1;
147  if ($location = $schema->getLocation()) {
148  $foundVisitors[$location]->visit($command, $response, $schema, $result);
149  }
150  }
151 
152  // Remove any unknown and potentially unsafe top-level properties
153  if ($additional === false) {
154  $result = array_intersect_key($result, $knownProps);
155  }
156 
157  // Call the after() method of each found visitor
158  foreach ($foundVisitors as $visitor) {
159  $visitor->after($command);
160  }
161 
162  return $result;
163  }
164 
165  protected function visitAdditionalProperties(
166  Parameter $model,
167  CommandInterface $command,
168  Response $response,
169  Parameter $additional,
170  &$result,
171  array &$foundVisitors
172  ) {
173  // Only visit when a location is specified
174  if ($location = $additional->getLocation()) {
175  if (!isset($foundVisitors[$location])) {
176  $foundVisitors[$location] = $this->factory->getResponseVisitor($location);
177  $foundVisitors[$location]->before($command, $result);
178  }
179  // Only traverse if an array was parsed from the before() visitors
180  if (is_array($result)) {
181  // Find each additional property
182  foreach (array_keys($result) as $key) {
183  // Check if the model actually knows this property. If so, then it is not additional
184  if (!$model->getProperty($key)) {
185  // Set the name to the key so that we can parse it with each visitor
186  $additional->setName($key);
187  $foundVisitors[$location]->visit($command, $response, $additional, $result);
188  }
189  }
190  // Reset the additionalProperties name to null
191  $additional->setName(null);
192  }
193  }
194  }
195 }
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight
Definition: VisitorFlyweight.php:12
Guzzle\Service\Command\OperationResponseParser\__construct
__construct(VisitorFlyweight $factory, $schemaInModels=false)
Definition: OperationResponseParser.php:51
Guzzle\Service\Command\CreateResponseClassEvent
Definition: CreateResponseClassEvent.php:10
Guzzle\Service\Command
Definition: AbstractCommand.php:3
Guzzle\Service\Exception\ResponseClassException
Definition: ResponseClassException.php:7
Guzzle\Service\Description\Operation
Definition: Operation.php:10
Guzzle\Service\Description\Parameter\getProperty
getProperty($name)
Definition: Parameter.php:724
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Service\Description\OperationInterface\TYPE_MODEL
const TYPE_MODEL
Definition: OperationInterface.php:15
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Service\Resource\Model
Definition: Model.php:11
Guzzle\Service\Description\OperationInterface
Definition: OperationInterface.php:10
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getInstance
static getInstance()
Definition: VisitorFlyweight.php:52
Guzzle\Service\Command\DefaultResponseParser
Definition: DefaultResponseParser.php:10
Guzzle\Service\Command\OperationResponseParser\handleParsing
handleParsing(CommandInterface $command, Response $response, $contentType)
Definition: OperationResponseParser.php:72
Guzzle\Service\Command\LocationVisitor\Response\ResponseVisitorInterface
Definition: ResponseVisitorInterface.php:12
Guzzle\Service\Command\OperationResponseParser\getInstance
static getInstance()
Definition: OperationResponseParser.php:38
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\Description\Parameter\setName
setName($name)
Definition: Parameter.php:262
Guzzle\Service\Command\AbstractCommand\RESPONSE_PROCESSING
const RESPONSE_PROCESSING
Definition: AbstractCommand.php:37
Guzzle\Service\Command\OperationResponseParser\addVisitor
addVisitor($location, ResponseVisitorInterface $visitor)
Definition: OperationResponseParser.php:65
Guzzle\Service\Description\Parameter\getProperties
getProperties()
Definition: Parameter.php:705
Guzzle\Service\Command\OperationResponseParser\$instance
static $instance
Definition: OperationResponseParser.php:27
Guzzle\Service\Command\OperationResponseParser\visitAdditionalProperties
visitAdditionalProperties(Parameter $model, CommandInterface $command, Response $response, Parameter $additional, &$result, array &$foundVisitors)
Definition: OperationResponseParser.php:171
Guzzle\Service\Command\AbstractCommand\TYPE_MODEL
const TYPE_MODEL
Definition: AbstractCommand.php:40
Guzzle\Service\Command\OperationResponseParser\visitResult
visitResult(Parameter $model, CommandInterface $command, Response $response)
Definition: OperationResponseParser.php:130
Guzzle\Service\Command\CommandInterface\getClient
getClient()
Guzzle\Service\Command\OperationResponseParser
Definition: OperationResponseParser.php:17
Guzzle\Service\Description\Parameter\getAdditionalProperties
getAdditionalProperties()
Definition: Parameter.php:775
Guzzle\Service\Description\OperationInterface\TYPE_CLASS
const TYPE_CLASS
Definition: OperationInterface.php:13
Guzzle\Service\Command\OperationResponseParser\$factory
$factory
Definition: OperationResponseParser.php:23
Guzzle\Service\Command\OperationResponseParser\parseClass
parseClass(CommandInterface $command)
Definition: OperationResponseParser.php:104
Guzzle\Service\Command\CommandInterface\getOperation
getOperation()
Guzzle\Service\Description\Parameter\getLocation
getLocation()
Definition: Parameter.php:514