Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Handler.php
Go to the documentation of this file.
1 <?php
8 
16 use Magento\Framework\Webapi\Request as SoapRequest;
17 use Magento\Framework\Webapi\Exception as WebapiException;
18 use Magento\Webapi\Model\Soap\Config as SoapConfig;
21 
29 class Handler
30 {
31  const RESULT_NODE_NAME = 'result';
32 
36  protected $_request;
37 
41  protected $_objectManager;
42 
46  protected $_apiConfig;
47 
51  protected $authorization;
52 
57 
62 
67 
72 
85  public function __construct(
86  SoapRequest $request,
87  \Magento\Framework\ObjectManagerInterface $objectManager,
88  SoapConfig $apiConfig,
90  SimpleDataObjectConverter $dataObjectConverter,
92  DataObjectProcessor $dataObjectProcessor,
94  ) {
95  $this->_request = $request;
96  $this->_objectManager = $objectManager;
97  $this->_apiConfig = $apiConfig;
98  $this->authorization = $authorization;
99  $this->_dataObjectConverter = $dataObjectConverter;
100  $this->serviceInputProcessor = $serviceInputProcessor;
101  $this->_dataObjectProcessor = $dataObjectProcessor;
102  $this->methodsMapProcessor = $methodsMapProcessor;
103  }
104 
115  public function __call($operation, $arguments)
116  {
117  $requestedServices = $this->_request->getRequestedServices();
118  $serviceMethodInfo = $this->_apiConfig->getServiceMethodInfo($operation, $requestedServices);
119  $serviceClass = $serviceMethodInfo[ServiceMetadata::KEY_CLASS];
120  $serviceMethod = $serviceMethodInfo[ServiceMetadata::KEY_METHOD];
121 
122  // check if the operation is a secure operation & whether the request was made in HTTPS
123  if ($serviceMethodInfo[ServiceMetadata::KEY_IS_SECURE] && !$this->_request->isSecure()) {
124  throw new WebapiException(__("Operation allowed only in HTTPS"));
125  }
126 
127  if (!$this->authorization->isAllowed($serviceMethodInfo[ServiceMetadata::KEY_ACL_RESOURCES])) {
128  throw new AuthorizationException(
129  __(
130  "The consumer isn't authorized to access %resources.",
131  ['resources' => implode(', ', $serviceMethodInfo[ServiceMetadata::KEY_ACL_RESOURCES])]
132  )
133  );
134  }
135  $service = $this->_objectManager->get($serviceClass);
136  $inputData = $this->_prepareRequestData($serviceClass, $serviceMethod, $arguments);
137  $outputData = call_user_func_array([$service, $serviceMethod], $inputData);
138  return $this->_prepareResponseData($outputData, $serviceClass, $serviceMethod);
139  }
140 
149  protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments)
150  {
152  $arguments = reset($arguments);
153  $arguments = $this->_dataObjectConverter->convertStdObjectToArray($arguments, true);
154  return $this->serviceInputProcessor->process($serviceClass, $serviceMethod, $arguments);
155  }
156 
166  protected function _prepareResponseData($data, $serviceClassName, $serviceMethodName)
167  {
169  $dataType = $this->methodsMapProcessor->getMethodReturnType($serviceClassName, $serviceMethodName);
170  $result = null;
171  if (is_object($data)) {
172  $result = $this->_dataObjectConverter
173  ->convertKeysToCamelCase($this->_dataObjectProcessor->buildOutputDataArray($data, $dataType));
174  } elseif (is_array($data)) {
175  $dataType = substr($dataType, 0, -2);
176  foreach ($data as $key => $value) {
177  if ($value instanceof $dataType
178  // the following two options are supported for backward compatibility
179  || $value instanceof ExtensibleDataInterface
180  || $value instanceof MetadataObjectInterface
181  ) {
182  $result[] = $this->_dataObjectConverter
183  ->convertKeysToCamelCase($this->_dataObjectProcessor->buildOutputDataArray($value, $dataType));
184  } else {
185  $result[$key] = $value;
186  }
187  }
188  } elseif (is_scalar($data) || $data === null) {
189  $result = $data;
190  } else {
191  throw new \InvalidArgumentException("Service returned result in invalid format.");
192  }
193  return [self::RESULT_NODE_NAME => $result];
194  }
195 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
__construct(SoapRequest $request, \Magento\Framework\ObjectManagerInterface $objectManager, SoapConfig $apiConfig, Authorization $authorization, SimpleDataObjectConverter $dataObjectConverter, ServiceInputProcessor $serviceInputProcessor, DataObjectProcessor $dataObjectProcessor, MethodsMap $methodsMapProcessor)
Definition: Handler.php:85
$value
Definition: gender.phtml:16
$arguments
_prepareRequestData($serviceClass, $serviceMethod, $arguments)
Definition: Handler.php:149