Total Complexity | 67 |
Total Lines | 576 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Index often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Index extends Action |
||
40 | { |
||
41 | /** Orders tablename */ |
||
42 | const ORDERS_TABLE = 'cart_process'; |
||
43 | |||
44 | /** Concurrency tablename */ |
||
45 | const CONCURRENCY_TABLE = 'pmt_orders'; |
||
46 | |||
47 | /** Concurrency tablename */ |
||
48 | const LOGS_TABLE = 'pmt_logs'; |
||
49 | |||
50 | /** Payment code */ |
||
51 | const PAYMENT_METHOD = 'paylater'; |
||
52 | |||
53 | /** |
||
54 | * EXCEPTION RESPONSES |
||
55 | */ |
||
56 | const CPO_ERR_MSG = 'Order not confirmed'; |
||
57 | const CPO_OK_MSG = 'Order confirmed'; |
||
58 | |||
59 | /** @var QuoteManagement */ |
||
60 | protected $quoteManagement; |
||
61 | |||
62 | /** @var PaymentInterface $paymentInterface */ |
||
63 | protected $paymentInterface; |
||
64 | |||
65 | /** @var OrderRepositoryInterface $orderRepositoryInterface */ |
||
66 | protected $orderRepositoryInterface; |
||
67 | |||
68 | /** @var Quote $quote */ |
||
69 | protected $quote; |
||
70 | |||
71 | /** @var QuoteRepository $quoteRepository */ |
||
72 | protected $quoteRepository; |
||
73 | |||
74 | /** @var mixed $config */ |
||
75 | protected $config; |
||
76 | |||
77 | /** @var mixed $quoteId */ |
||
78 | protected $quoteId; |
||
79 | |||
80 | /** @var array $notifyResult */ |
||
81 | protected $notifyResult; |
||
82 | |||
83 | /** @var mixed $magentoOrderId */ |
||
84 | protected $magentoOrderId; |
||
85 | |||
86 | /** @var mixed $pmtOrder */ |
||
87 | protected $pmtOrder; |
||
88 | |||
89 | /** @var ResourceConnection $dbObject */ |
||
90 | protected $dbObject; |
||
91 | |||
92 | /** @var Session $checkoutSession */ |
||
93 | protected $checkoutSession; |
||
94 | |||
95 | /** @var Client $orderClient */ |
||
96 | protected $orderClient; |
||
97 | |||
98 | /** @var mixed $pmtOrderId */ |
||
99 | protected $pmtOrderId; |
||
100 | |||
101 | /** @var OrderInterface $magentoOrder */ |
||
102 | protected $magentoOrder; |
||
103 | |||
104 | /** @var ExtraConfig $extraConfig */ |
||
105 | protected $extraConfig; |
||
106 | |||
107 | /** |
||
108 | * Index constructor. |
||
109 | * |
||
110 | * @param Context $context |
||
111 | * @param Quote $quote |
||
112 | * @param QuoteManagement $quoteManagement |
||
113 | * @param PaymentInterface $paymentInterface |
||
114 | * @param Config $config |
||
115 | * @param QuoteRepository $quoteRepository |
||
116 | * @param OrderRepositoryInterface $orderRepositoryInterface |
||
117 | * @param ResourceConnection $dbObject |
||
118 | * @param Session $checkoutSession |
||
119 | * @param ExtraConfig $extraConfig |
||
120 | */ |
||
121 | public function __construct( |
||
122 | Context $context, |
||
123 | Quote $quote, |
||
124 | QuoteManagement $quoteManagement, |
||
125 | PaymentInterface $paymentInterface, |
||
126 | Config $config, |
||
127 | QuoteRepository $quoteRepository, |
||
128 | OrderRepositoryInterface $orderRepositoryInterface, |
||
129 | ResourceConnection $dbObject, |
||
130 | Session $checkoutSession, |
||
131 | ExtraConfig $extraConfig |
||
132 | ) { |
||
133 | parent::__construct($context); |
||
134 | $this->quote = $quote; |
||
135 | $this->quoteManagement = $quoteManagement; |
||
136 | $this->paymentInterface = $paymentInterface; |
||
137 | $this->extraConfig = $extraConfig->getExtraConfig(); |
||
|
|||
138 | $this->config = $config->getConfig(); |
||
139 | $this->quoteRepository = $quoteRepository; |
||
140 | $this->orderRepositoryInterface = $orderRepositoryInterface; |
||
141 | $this->dbObject = $dbObject; |
||
142 | $this->checkoutSession = $checkoutSession; |
||
143 | |||
144 | // CsrfAwareAction Magento2.3 compatibility |
||
145 | if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) { |
||
146 | $request = $this->getRequest(); |
||
147 | if ($request instanceof HttpRequest && $request->isPost() && empty($request->getParam('form_key'))) { |
||
148 | $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class); |
||
149 | $request->setParam('form_key', $formKey->getFormKey()); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void |
||
156 | * @throws UnknownException |
||
157 | */ |
||
158 | public function execute() |
||
159 | { |
||
160 | try { |
||
161 | $this->checkConcurrency(); |
||
162 | $this->getMerchantOrder(); |
||
163 | $this->getPmtOrderId(); |
||
164 | $this->getPmtOrder(); |
||
165 | $this->checkOrderStatus(); |
||
166 | $this->checkMerchantOrderStatus(); |
||
167 | $this->validateAmount(); |
||
168 | $this->processMerchantOrder(); |
||
169 | } catch (\Exception $exception) { |
||
170 | $jsonResponse = new JsonExceptionResponse(); |
||
171 | $jsonResponse->setMerchantOrderId($this->magentoOrderId); |
||
172 | $jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
173 | $jsonResponse->setException($exception); |
||
174 | $response = $jsonResponse->toJson(); |
||
175 | $this->insertLog($exception); |
||
176 | } |
||
177 | |||
178 | try { |
||
179 | if (!isset($response)) { |
||
180 | $this->confirmPmtOrder(); |
||
181 | $jsonResponse = new JsonSuccessResponse(); |
||
182 | $jsonResponse->setMerchantOrderId($this->magentoOrderId); |
||
183 | $jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
184 | } |
||
185 | } catch (\Exception $exception) { |
||
186 | $this->rollbackMerchantOrder(); |
||
187 | $jsonResponse = new JsonExceptionResponse(); |
||
188 | $jsonResponse->setMerchantOrderId($this->magentoOrderId); |
||
189 | $jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
190 | $jsonResponse->setException($exception); |
||
191 | $jsonResponse->toJson(); |
||
192 | $this->insertLog($exception); |
||
193 | } |
||
194 | |||
195 | $this->unblockConcurrency(true); |
||
196 | |||
197 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
198 | $jsonResponse->printResponse(); |
||
199 | } else { |
||
200 | $returnUrl = $this->getRedirectUrl(); |
||
201 | $this->_redirect($returnUrl); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * COMMON FUNCTIONS |
||
207 | */ |
||
208 | |||
209 | /** |
||
210 | * @throws QuoteNotFoundException |
||
211 | * @throws UnknownException |
||
212 | */ |
||
213 | private function checkConcurrency() |
||
214 | { |
||
215 | $this->getQuoteId(); |
||
216 | $this->checkDbTable(); |
||
217 | $this->unblockConcurrency(); |
||
218 | $this->blockConcurrency(); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @throws MerchantOrderNotFoundException |
||
223 | */ |
||
224 | private function getMerchantOrder() |
||
225 | { |
||
226 | try { |
||
227 | /** @var Quote quote */ |
||
228 | $this->quote = $this->quoteRepository->get($this->quoteId); |
||
229 | } catch (\Exception $e) { |
||
230 | throw new MerchantOrderNotFoundException(); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @throws UnknownException |
||
236 | */ |
||
237 | private function getPmtOrderId() |
||
238 | { |
||
239 | try { |
||
240 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
241 | $dbConnection = $this->dbObject->getConnection(); |
||
242 | $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
||
243 | $query = "select order_id from $tableName where id='$this->quoteId'"; |
||
244 | $queryResult = $dbConnection->fetchRow($query); |
||
245 | $this->pmtOrderId = $queryResult['order_id']; |
||
246 | if ($this->pmtOrderId == '') { |
||
247 | throw new NoIdentificationException(); |
||
248 | } |
||
249 | } catch (\Exception $e) { |
||
250 | throw new UnknownException($e->getMessage()); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @throws OrderNotFoundException |
||
256 | */ |
||
257 | private function getPmtOrder() |
||
258 | { |
||
259 | try { |
||
260 | $this->orderClient = new Client($this->config['pmt_public_key'], $this->config['pmt_private_key']); |
||
261 | $this->pmtOrder = $this->orderClient->getOrder($this->pmtOrderId); |
||
262 | } catch (\Exception $e) { |
||
263 | throw new OrderNotFoundException(); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @throws AlreadyProcessedException |
||
269 | * @throws WrongStatusException |
||
270 | */ |
||
271 | private function checkOrderStatus() |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @throws AlreadyProcessedException |
||
287 | */ |
||
288 | private function checkMerchantOrderStatus() |
||
289 | { |
||
290 | if ($this->quote->getIsActive()=='0') { |
||
291 | $this->getMagentoOrderId(); |
||
292 | throw new AlreadyProcessedException(); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @throws AmountMismatchException |
||
298 | */ |
||
299 | private function validateAmount() |
||
300 | { |
||
301 | $pmtAmount = $this->pmtOrder->getShoppingCart()->getTotalAmount(); |
||
302 | $merchantAmount = intval(strval(100 * $this->quote->getGrandTotal())); |
||
303 | if ($pmtAmount != $merchantAmount) { |
||
304 | throw new AmountMismatchException($pmtAmount, $merchantAmount); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @throws UnknownException |
||
310 | */ |
||
311 | private function processMerchantOrder() |
||
312 | { |
||
313 | try { |
||
314 | $this->saveOrder(); |
||
315 | $this->updateBdInfo(); |
||
316 | } catch (\Exception $e) { |
||
317 | throw new UnknownException($e->getMessage()); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @return false|string |
||
323 | * @throws UnknownException |
||
324 | */ |
||
325 | private function confirmPmtOrder() |
||
326 | { |
||
327 | try { |
||
328 | $this->pmtOrder = $this->orderClient->confirmOrder($this->pmtOrderId); |
||
329 | } catch (\Exception $e) { |
||
330 | throw new UnknownException($e->getMessage()); |
||
331 | } |
||
332 | |||
333 | $jsonResponse = new JsonSuccessResponse(); |
||
334 | $jsonResponse->setStatusCode(200); |
||
335 | $jsonResponse->setMerchantOrderId($this->magentoOrderId); |
||
336 | $jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
337 | $jsonResponse->setResult(self::CPO_OK_MSG); |
||
338 | return $jsonResponse->toJson(); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * UTILS FUNCTIONS |
||
343 | */ |
||
344 | |||
345 | /** STEP 1 CC - Check concurrency |
||
346 | * @throws QuoteNotFoundException |
||
347 | */ |
||
348 | private function getQuoteId() |
||
349 | { |
||
350 | $this->quoteId = $this->getRequest()->getParam('quoteId'); |
||
351 | if ($this->quoteId == '') { |
||
352 | throw new QuoteNotFoundException(); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @return \Zend_Db_Statement_Interface |
||
358 | * @throws UnknownException |
||
359 | */ |
||
360 | private function checkDbTable() |
||
361 | { |
||
362 | try { |
||
363 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
364 | $dbConnection = $this->dbObject->getConnection(); |
||
365 | $tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
||
366 | $query = "CREATE TABLE IF NOT EXISTS $tableName(`id` int not null,`timestamp` int not null,PRIMARY KEY (`id`))"; |
||
367 | |||
368 | return $dbConnection->query($query); |
||
369 | } catch (\Exception $e) { |
||
370 | throw new UnknownException($e->getMessage()); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @return void|\Zend_Db_Statement_Interface |
||
376 | * @throws UnknownException |
||
377 | */ |
||
378 | private function checkDbLogTable() |
||
379 | { |
||
380 | try { |
||
381 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
382 | $dbConnection = $this->dbObject->getConnection(); |
||
383 | $tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
||
384 | if (!$dbConnection->isTableExists($tableName)) { |
||
385 | $table = $dbConnection |
||
386 | ->newTable($tableName) |
||
387 | ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)) |
||
388 | ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false)) |
||
389 | ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT)); |
||
390 | return $dbConnection->createTable($table); |
||
391 | } |
||
392 | |||
393 | return; |
||
394 | } catch (\Exception $e) { |
||
395 | throw new UnknownException($e->getMessage()); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @param bool $mode |
||
401 | * |
||
402 | * @throws \Exception |
||
403 | */ |
||
404 | private function unblockConcurrency($mode = false) |
||
405 | { |
||
406 | try { |
||
407 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
408 | $dbConnection = $this->dbObject->getConnection(); |
||
409 | $tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
||
410 | if ($mode == false) { |
||
411 | $dbConnection->delete($tableName, "timestamp<".(time() - 5)); |
||
412 | } elseif ($this->quoteId!='') { |
||
413 | $dbConnection->delete($tableName, "id=".$this->quoteId); |
||
414 | } |
||
415 | } catch (Exception $exception) { |
||
416 | throw new ConcurrencyException(); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @throws \Exception |
||
422 | */ |
||
423 | private function blockConcurrency() |
||
424 | { |
||
425 | try { |
||
426 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
427 | $dbConnection = $this->dbObject->getConnection(); |
||
428 | $tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
||
429 | $dbConnection->insert($tableName, array('id'=>$this->quoteId, 'timestamp'=>time())); |
||
430 | } catch (Exception $exception) { |
||
431 | throw new ConcurrencyException(); |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** STEP 2 GMO - Get Merchant Order */ |
||
436 | /** STEP 3 GPOI - Get Pmt OrderId */ |
||
437 | /** STEP 4 GPO - Get Pmt Order */ |
||
438 | /** STEP 5 COS - Check Order Status */ |
||
439 | /** |
||
440 | * @param $statusArray |
||
441 | * |
||
442 | * @throws \Exception |
||
443 | */ |
||
444 | private function checkPmtStatus($statusArray) |
||
454 | } |
||
455 | } |
||
456 | |||
457 | /** STEP 6 CMOS - Check Merchant Order Status */ |
||
458 | /** |
||
459 | * @throws \Exception |
||
460 | */ |
||
461 | private function getMagentoOrderId() |
||
474 | } |
||
475 | } |
||
476 | |||
477 | /** STEP 7 VA - Validate Amount */ |
||
478 | /** STEP 8 PMO - Process Merchant Order */ |
||
479 | /** |
||
480 | * @throws UnknownException |
||
481 | */ |
||
482 | private function saveOrder() |
||
483 | { |
||
484 | try { |
||
485 | $this->paymentInterface->setMethod(self::PAYMENT_METHOD); |
||
486 | $this->magentoOrderId = $this->quoteManagement->placeOrder($this->quoteId, $this->paymentInterface); |
||
487 | /** @var \Magento\Sales\Api\Data\OrderInterface magentoOrder */ |
||
488 | $this->magentoOrder = $this->orderRepositoryInterface->get($this->magentoOrderId); |
||
489 | |||
490 | if ($this->magentoOrderId == '') { |
||
491 | throw new UnknownException('Order can not be saved'); |
||
492 | } |
||
493 | } catch (\Exception $e) { |
||
494 | throw new UnknownException($e->getMessage()); |
||
495 | } |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * @throws UnknownException |
||
500 | */ |
||
501 | private function updateBdInfo() |
||
502 | { |
||
503 | try { |
||
504 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
505 | $dbConnection = $this->dbObject->getConnection(); |
||
506 | $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
||
507 | $pmtOrderId = $this->pmtOrder->getId(); |
||
508 | $dbConnection->update( |
||
509 | $tableName, |
||
510 | array('mg_order_id' => $this->magentoOrderId), |
||
511 | "order_id='$pmtOrderId' and id='$this->quoteId'" |
||
512 | ); |
||
513 | } catch (\Exception $e) { |
||
514 | throw new UnknownException($e->getMessage()); |
||
515 | } |
||
516 | } |
||
517 | |||
518 | /** STEP 9 CPO - Confirmation Pmt Order */ |
||
519 | /** |
||
520 | * @throws UnknownException |
||
521 | */ |
||
522 | private function rollbackMerchantOrder() |
||
523 | { |
||
524 | try { |
||
525 | $this->magentoOrder->setState(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT, true); |
||
526 | $this->magentoOrder->setStatus(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT); |
||
527 | $this->magentoOrder->save(); |
||
528 | } catch (\Exception $e) { |
||
529 | throw new UnknownException($e->getMessage()); |
||
530 | } |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @return string |
||
535 | */ |
||
536 | private function getRedirectUrl() |
||
537 | { |
||
538 | //$returnUrl = 'checkout/#payment'; |
||
539 | $returnUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
||
540 | if ($this->magentoOrderId!='') { |
||
541 | /** @var Order $this->magentoOrder */ |
||
542 | $this->magentoOrder = $this->orderRepositoryInterface->get($this->magentoOrderId); |
||
543 | if (!$this->_objectManager->get(\Magento\Checkout\Model\Session\SuccessValidator::class)->isValid()) { |
||
544 | $this->checkoutSession |
||
545 | ->setLastOrderId($this->magentoOrderId) |
||
546 | ->setLastRealOrderId($this->magentoOrder->getIncrementId()) |
||
547 | ->setLastQuoteId($this->quoteId) |
||
548 | ->setLastSuccessQuoteId($this->quoteId) |
||
549 | ->setLastOrderStatus($this->magentoOrder->getStatus()); |
||
550 | } |
||
551 | |||
552 | //Magento status flow => https://docs.magento.com/m2/ce/user_guide/sales/order-status-workflow.html |
||
553 | //Order Workflow => https://docs.magento.com/m2/ce/user_guide/sales/order-workflow.html |
||
554 | $orderStatus = strtolower($this->magentoOrder->getStatus()); |
||
555 | $acceptedStatus = array('processing', 'completed'); |
||
556 | if (in_array($orderStatus, $acceptedStatus)) { |
||
557 | if (isset($this->extraConfig['PMT_OK_URL']) && $this->extraConfig['PMT_OK_URL']!= '') { |
||
558 | $returnUrl = $this->extraConfig['PMT_OK_URL']; |
||
559 | } else { |
||
560 | $returnUrl = 'checkout/onepage/success'; |
||
561 | } |
||
562 | } else { |
||
563 | if (isset($this->extraConfig['PMT_KO_URL']) && $this->extraConfig['PMT_KO_URL'] != '') { |
||
564 | $returnUrl = $this->extraConfig['PMT_KO_URL']; |
||
565 | } else { |
||
566 | //$returnUrl = 'checkout/#payment'; |
||
567 | $returnUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
||
568 | } |
||
569 | } |
||
570 | } |
||
571 | return $returnUrl; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @param $exceptionMessage |
||
576 | * |
||
577 | * @throws UnknownException |
||
578 | */ |
||
579 | private function insertLog($exceptionMessage) |
||
594 | } |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * @param RequestInterface $request |
||
599 | * |
||
600 | * @return InvalidRequestException|null |
||
601 | */ |
||
602 | public function createCsrfValidationException(RequestInterface $request): ? InvalidRequestException |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @param RequestInterface $request |
||
609 | * |
||
610 | * @return bool|null |
||
611 | */ |
||
612 | public function validateForCsrf(RequestInterface $request): ? bool |
||
615 | } |
||
616 | } |
||
617 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..