Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurlHandler.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
28  private $operation;
29 
35  private $entityObject;
36 
42  private $operationDefinition;
43 
49  private $requestData;
50 
56  private $storeCode;
57 
63  private $isJson;
64 
70  private static $curlMethodMapping = [
71  'create' => CurlInterface::POST,
72  'delete' => CurlInterface::DELETE,
73  'update' => CurlInterface::PUT,
74  'get' => CurlInterface::GET,
75  ];
76 
84  public function __construct($operation, $entityObject, $storeCode = null)
85  {
86  $this->operation = $operation;
87  $this->entityObject = $entityObject;
88  $this->storeCode = $storeCode;
89 
90  $this->operationDefinition = OperationDefinitionObjectHandler::getInstance()->getOperationDefinition(
91  $this->operation,
92  $this->entityObject->getType()
93  );
94  $this->isJson = false;
95  }
96 
105  public function executeRequest($dependentEntities)
106  {
107  $executor = null;
108  $successRegex = null;
109  $returnRegex = null;
110 
111  if ((null !== $dependentEntities) && is_array($dependentEntities)) {
112  $entities = array_merge([$this->entityObject], $dependentEntities);
113  } else {
114  $entities = [$this->entityObject];
115  }
116  $apiUrl = $this->resolveUrlReference($this->operationDefinition->getApiUrl(), $entities);
117  $headers = $this->operationDefinition->getHeaders();
118  $authorization = $this->operationDefinition->getAuth();
119  $contentType = $this->operationDefinition->getContentType();
120  $successRegex = $this->operationDefinition->getSuccessRegex();
121  $returnRegex = $this->operationDefinition->getReturnRegex();
122 
123  $operationDataResolver = new OperationDataArrayResolver($dependentEntities);
124  $this->requestData = $operationDataResolver->resolveOperationDataArray(
125  $this->entityObject,
126  $this->operationDefinition->getOperationMetadata(),
127  $this->operationDefinition->getOperation(),
128  false
129  );
130 
131  if (($contentType === 'application/json') && ($authorization === 'adminOauth')) {
132  $this->isJson = true;
133  $executor = new WebapiExecutor($this->storeCode);
134  } elseif ($authorization === 'adminFormKey') {
135  $executor = new AdminExecutor($this->operationDefinition->removeUrlBackend());
136  } elseif ($authorization === 'customerFormKey') {
137  $executor = new FrontendExecutor(
138  $this->requestData['customer_email'],
139  $this->requestData['customer_password']
140  );
141  } elseif ($authorization === 'anonymous') {
142  $this->isJson = true;
143  $executor = new WebapiNoAuthExecutor($this->storeCode);
144  }
145 
146  if (!$executor) {
147  throw new TestFrameworkException(
148  sprintf(
149  "Invalid content type and/or auth type. content type = %s, auth type = %s\n",
150  $contentType,
151  $authorization
152  )
153  );
154  }
155 
156  $executor->write(
157  $apiUrl,
158  $this->requestData,
159  self::$curlMethodMapping[$this->operation],
160  $headers
161  );
162 
163  $response = $executor->read($successRegex, $returnRegex);
164  $executor->close();
165 
166  return $response;
167  }
168 
174  public function getRequestDataArray()
175  {
176  return $this->requestData;
177  }
178 
184  public function isContentTypeJson()
185  {
186  return $this->isJson;
187  }
188 
196  private function resolveUrlReference($urlIn, $entityObjects)
197  {
198  $urlOut = $urlIn;
199  $matchedParams = [];
200  preg_match_all("/[{](.+?)[}]/", $urlIn, $matchedParams);
201 
202  if (!empty($matchedParams)) {
203  foreach ($matchedParams[0] as $paramKey => $paramValue) {
204  foreach ($entityObjects as $entityObject) {
205  $param = $entityObject->getDataByName(
206  $matchedParams[1][$paramKey],
208  );
209  if (null !== $param) {
210  $urlOut = str_replace($paramValue, $param, $urlOut);
211  continue;
212  }
213  }
214  }
215  }
216  return $urlOut;
217  }
218 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
__construct($operation, $entityObject, $storeCode=null)
Definition: CurlHandler.php:84