| Total Complexity | 63 |
| Total Lines | 492 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 2 | Features | 3 |
Complex classes like PagantisNotifyModuleFrontController 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 PagantisNotifyModuleFrontController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class PagantisNotifyModuleFrontController extends AbstractController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Seconds to expire a locked request |
||
| 33 | */ |
||
| 34 | const CONCURRENCY_TIMEOUT = 20; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string $merchantOrderId |
||
| 38 | */ |
||
| 39 | protected $merchantOrderId; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Cart $merchantOrder |
||
|
|
|||
| 43 | */ |
||
| 44 | protected $merchantOrder; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string $pagantisOrderId |
||
| 48 | */ |
||
| 49 | protected $pagantisOrderId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string $amountMismatchError |
||
| 53 | */ |
||
| 54 | protected $amountMismatchError = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Pagantis\OrdersApiClient\Model\Order $pagantisOrder |
||
| 58 | */ |
||
| 59 | protected $pagantisOrder; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Pagantis\OrdersApiClient\Client $orderClient |
||
| 63 | */ |
||
| 64 | protected $orderClient; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var mixed $config |
||
| 68 | */ |
||
| 69 | protected $config; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Object $jsonResponse |
||
| 73 | */ |
||
| 74 | protected $jsonResponse; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @throws Exception |
||
| 78 | */ |
||
| 79 | public function postProcess() |
||
| 80 | { |
||
| 81 | try { |
||
| 82 | $this->prepareVariables(); |
||
| 83 | $this->checkConcurrency(); |
||
| 84 | $this->getMerchantOrder(); |
||
| 85 | $this->getPagantisOrderId(); |
||
| 86 | $this->getPagantisOrder(); |
||
| 87 | if ($this->checkOrderStatus()) { |
||
| 88 | return $this->finishProcess(false); |
||
| 89 | } |
||
| 90 | $this->validateAmount(); |
||
| 91 | if ($this->checkMerchantOrderStatus()) { |
||
| 92 | $this->processMerchantOrder(); |
||
| 93 | } |
||
| 94 | } catch (\Exception $exception) { |
||
| 95 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 96 | $this->jsonResponse = new JsonExceptionResponse(); |
||
| 97 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 98 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 99 | $this->jsonResponse->setException($exception); |
||
| 100 | } |
||
| 101 | return $this->cancelProcess($exception); |
||
| 102 | } |
||
| 103 | |||
| 104 | try { |
||
| 105 | $this->jsonResponse = new JsonSuccessResponse(); |
||
| 106 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 107 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 108 | $this->confirmPagantisOrder(); |
||
| 109 | } catch (\Exception $exception) { |
||
| 110 | $this->rollbackMerchantOrder(); |
||
| 111 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 112 | $this->jsonResponse = new JsonExceptionResponse(); |
||
| 113 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 114 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 115 | $this->jsonResponse->setException($exception); |
||
| 116 | } |
||
| 117 | return $this->cancelProcess($exception); |
||
| 118 | } |
||
| 119 | |||
| 120 | try { |
||
| 121 | $this->unblockConcurrency($this->merchantOrderId); |
||
| 122 | } catch (\Exception $exception) { |
||
| 123 | // Do nothing |
||
| 124 | } |
||
| 125 | |||
| 126 | return $this->finishProcess(false); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Check the concurrency of the purchase |
||
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | */ |
||
| 134 | public function checkConcurrency() |
||
| 135 | { |
||
| 136 | $this->unblockConcurrency(); |
||
| 137 | $this->blockConcurrency($this->merchantOrderId); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Find and init variables needed to process payment |
||
| 142 | * |
||
| 143 | * @throws Exception |
||
| 144 | */ |
||
| 145 | public function prepareVariables() |
||
| 146 | { |
||
| 147 | $callbackOkUrl = $this->context->link->getPageLink( |
||
| 148 | 'order-confirmation', |
||
| 149 | null, |
||
| 150 | null |
||
| 151 | ); |
||
| 152 | $callbackKoUrl = $this->context->link->getPageLink( |
||
| 153 | 'order', |
||
| 154 | null, |
||
| 155 | null, |
||
| 156 | array('step'=>3) |
||
| 157 | ); |
||
| 158 | try { |
||
| 159 | $this->config = array( |
||
| 160 | 'urlOK' => (Pagantis::getExtraConfig('PAGANTIS_URL_OK') !== '') ? |
||
| 161 | Pagantis::getExtraConfig('PAGANTIS_URL_OK') : $callbackOkUrl, |
||
| 162 | 'urlKO' => (Pagantis::getExtraConfig('PAGANTIS_URL_KO') !== '') ? |
||
| 163 | Pagantis::getExtraConfig('PAGANTIS_URL_KO') : $callbackKoUrl, |
||
| 164 | 'publicKey' => Configuration::get('pagantis_public_key'), |
||
| 165 | 'privateKey' => Configuration::get('pagantis_private_key'), |
||
| 166 | 'secureKey' => Tools::getValue('key'), |
||
| 167 | ); |
||
| 168 | } catch (\Exception $exception) { |
||
| 169 | throw new ConfigurationNotFoundException(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->merchantOrderId = Tools::getValue('id_cart'); |
||
| 173 | if ($this->merchantOrderId == '') { |
||
| 174 | throw new QuoteNotFoundException(); |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | if (!($this->config['secureKey'] && $this->merchantOrderId && Module::isEnabled(self::PAGANTIS_CODE))) { |
||
| 179 | // This exception is only for Prestashop |
||
| 180 | throw new UnknownException('Module may not be enabled'); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve the merchant order by id |
||
| 186 | * |
||
| 187 | * @throws Exception |
||
| 188 | */ |
||
| 189 | public function getMerchantOrder() |
||
| 190 | { |
||
| 191 | try { |
||
| 192 | $this->merchantOrder = new Cart($this->merchantOrderId); |
||
| 193 | if (!Validate::isLoadedObject($this->merchantOrder)) { |
||
| 194 | // This exception is only for Prestashop |
||
| 195 | throw new UnknownException('Unable to load cart'); |
||
| 196 | } |
||
| 197 | } catch (\Exception $exception) { |
||
| 198 | throw new MerchantOrderNotFoundException(); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Find PAGANTIS Order Id in AbstractController::PAGANTIS_ORDERS_TABLE |
||
| 204 | * |
||
| 205 | * @throws Exception |
||
| 206 | */ |
||
| 207 | private function getPagantisOrderId() |
||
| 208 | { |
||
| 209 | try { |
||
| 210 | $this->pagantisOrderId= Db::getInstance()->getValue( |
||
| 211 | 'select order_id from '._DB_PREFIX_.'pagantis_order where id = '.$this->merchantOrderId |
||
| 212 | ); |
||
| 213 | |||
| 214 | if (is_null($this->pagantisOrderId)) { |
||
| 215 | throw new NoIdentificationException(); |
||
| 216 | } |
||
| 217 | } catch (\Exception $exception) { |
||
| 218 | throw new NoIdentificationException(); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Find PAGANTIS Order in Orders Server using Pagantis\OrdersApiClient |
||
| 224 | * |
||
| 225 | * @throws Exception |
||
| 226 | */ |
||
| 227 | private function getPagantisOrder() |
||
| 228 | { |
||
| 229 | $this->orderClient = new PagantisClient($this->config['publicKey'], $this->config['privateKey']); |
||
| 230 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
| 231 | if (!($this->pagantisOrder instanceof PagantisModelOrder)) { |
||
| 232 | throw new OrderNotFoundException(); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Compare statuses of merchant order and PAGANTIS order, witch have to be the same. |
||
| 238 | * |
||
| 239 | * @throws Exception |
||
| 240 | */ |
||
| 241 | public function checkOrderStatus() |
||
| 242 | { |
||
| 243 | if ($this->pagantisOrder->getStatus() === PagantisModelOrder::STATUS_CONFIRMED) { |
||
| 244 | $this->jsonResponse = new JsonSuccessResponse(); |
||
| 245 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 246 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 247 | return true; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($this->pagantisOrder->getStatus() !== PagantisModelOrder::STATUS_AUTHORIZED) { |
||
| 251 | $status = '-'; |
||
| 252 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 253 | $status = $this->pagantisOrder->getStatus(); |
||
| 254 | } |
||
| 255 | throw new WrongStatusException($status); |
||
| 256 | } |
||
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Check that the merchant order and the order in PAGANTIS have the same amount to prevent hacking |
||
| 262 | * |
||
| 263 | * @throws Exception |
||
| 264 | */ |
||
| 265 | public function validateAmount() |
||
| 290 | // Do nothing |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Check that the merchant order was not previously processes and is ready to be paid |
||
| 297 | * |
||
| 298 | * @throws Exception |
||
| 299 | */ |
||
| 300 | public function checkMerchantOrderStatus() |
||
| 301 | { |
||
| 302 | try { |
||
| 303 | if ($this->merchantOrder->orderExists() !== false) { |
||
| 304 | throw new WrongStatusException('PS->orderExists() cart_id = ' |
||
| 305 | . $this->merchantOrderId . ' pagantis_id = ' |
||
| 306 | . $this->pagantisOrderId . '): already_processed'); |
||
| 307 | } |
||
| 308 | |||
| 309 | // Double check |
||
| 310 | $tableName = _DB_PREFIX_ . 'pagantis_order'; |
||
| 311 | $sql = ('select ps_order_id from `' . $tableName . '` where `id` = ' . $this->merchantOrderId |
||
| 312 | . ' and `order_id` = \'' . $this->pagantisOrderId . '\'' |
||
| 313 | . ' and `ps_order_id` is not null'); |
||
| 314 | $results = Db::getInstance()->ExecuteS($sql); |
||
| 315 | if (is_array($results) && count($results) === 1) { |
||
| 316 | throw new WrongStatusException('PS->record found in ' . $tableName |
||
| 317 | . ' (cart_id = ' . $this->merchantOrderId . ' pagantis_id = ' |
||
| 318 | . $this->pagantisOrderId . '): already_processed'); |
||
| 319 | } |
||
| 320 | } catch (\Exception $exception) { |
||
| 321 | throw new UnknownException($exception->getMessage()); |
||
| 322 | } |
||
| 323 | return true; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Process the merchant order and notify client |
||
| 328 | * |
||
| 329 | * @throws Exception |
||
| 330 | */ |
||
| 331 | public function processMerchantOrder() |
||
| 366 | // Do nothing |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Confirm the order in PAGANTIS |
||
| 372 | * |
||
| 373 | * @throws Exception |
||
| 374 | */ |
||
| 375 | private function confirmPagantisOrder() |
||
| 376 | { |
||
| 377 | try { |
||
| 378 | $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
| 379 | try { |
||
| 380 | $mode = ($_SERVER['REQUEST_METHOD'] == 'POST') ? 'NOTIFICATION' : 'REDIRECTION'; |
||
| 381 | $message = 'Order CONFIRMED. The order was confirmed by a ' . $mode . |
||
| 382 | '. Pagantis OrderId=' . $this->pagantisOrderId . |
||
| 383 | '. Prestashop OrderId=' . $this->module->currentOrder; |
||
| 384 | $this->saveLog(array('message' => $message)); |
||
| 385 | } catch (\Exception $exception) { |
||
| 386 | // Do nothing |
||
| 387 | } |
||
| 388 | } catch (\Exception $exception) { |
||
| 389 | throw new UnknownException($exception->getMessage()); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Leave the merchant order as it was previously |
||
| 395 | * |
||
| 396 | * @throws Exception |
||
| 397 | */ |
||
| 398 | public function rollbackMerchantOrder() |
||
| 399 | { |
||
| 400 | // Do nothing because the order is created only when the purchase was successfully |
||
| 401 | try { |
||
| 402 | $message = 'Roolback method: ' . |
||
| 403 | '. Pagantis OrderId=' . $this->pagantisOrderId . |
||
| 404 | '. Prestashop CartId=' . $this->merchantOrderId; |
||
| 405 | $this->saveLog(array('message' => $message)); |
||
| 406 | } catch (\Exception $exception) { |
||
| 407 | // Do nothing |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Lock the concurrency to prevent duplicated inputs |
||
| 413 | * |
||
| 414 | * @param $orderId |
||
| 415 | * @return bool|void |
||
| 416 | * @throws ConcurrencyException |
||
| 417 | */ |
||
| 418 | protected function blockConcurrency($orderId) |
||
| 419 | { |
||
| 420 | try { |
||
| 421 | $table = 'pagantis_cart_process'; |
||
| 422 | return Db::getInstance()->insert($table, array('id' => $orderId, 'timestamp' => (time()))); |
||
| 423 | } catch (\Exception $exception) { |
||
| 424 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 425 | throw new ConcurrencyException(); |
||
| 426 | } |
||
| 427 | |||
| 428 | $query = sprintf( |
||
| 429 | "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, FROM_UNIXTIME(timestamp)) as rest FROM %s WHERE %s", |
||
| 430 | self::CONCURRENCY_TIMEOUT, |
||
| 431 | _DB_PREFIX_.$table, |
||
| 432 | "id=$orderId" |
||
| 433 | ); |
||
| 434 | $resultSeconds = Db::getInstance()->getValue($query); |
||
| 435 | $restSeconds = isset($resultSeconds) ? ($resultSeconds) : 0; |
||
| 436 | $secondsToExpire = ($restSeconds>self::CONCURRENCY_TIMEOUT) ? self::CONCURRENCY_TIMEOUT : $restSeconds; |
||
| 437 | |||
| 438 | $logMessage = sprintf( |
||
| 439 | "Redirect concurrency, User have to wait %s seconds, default seconds %s, bd time to expire %s seconds", |
||
| 440 | $secondsToExpire, |
||
| 441 | self::CONCURRENCY_TIMEOUT, |
||
| 442 | $restSeconds |
||
| 443 | ); |
||
| 444 | |||
| 445 | $this->saveLog(array( |
||
| 446 | 'message' => $logMessage |
||
| 447 | )); |
||
| 448 | sleep($secondsToExpire+1); |
||
| 449 | // After waiting...user continue the confirmation, hoping that previous call have finished. |
||
| 450 | return true; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param null $orderId |
||
| 456 | * |
||
| 457 | * @throws ConcurrencyException |
||
| 458 | */ |
||
| 459 | private function unblockConcurrency($orderId = null) |
||
| 460 | { |
||
| 461 | try { |
||
| 462 | if (is_null($orderId)) { |
||
| 463 | Db::getInstance()->delete( |
||
| 464 | 'pagantis_cart_process', |
||
| 465 | 'timestamp < ' . (time() - self::CONCURRENCY_TIMEOUT |
||
| 466 | ) |
||
| 467 | ); |
||
| 468 | return; |
||
| 469 | } |
||
| 470 | Db::getInstance()->delete('pagantis_cart_process', 'id = \'' . $orderId . '\''); |
||
| 471 | } catch (\Exception $exception) { |
||
| 472 | throw new ConcurrencyException(); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Do all the necessary actions to cancel the confirmation process in case of error |
||
| 478 | * 1. Unblock concurrency |
||
| 479 | * 2. Save log |
||
| 480 | * |
||
| 481 | * @param \Exception $exception |
||
| 482 | * |
||
| 483 | */ |
||
| 484 | public function cancelProcess($exception = null) |
||
| 485 | { |
||
| 486 | $debug = debug_backtrace(); |
||
| 487 | $method = $debug[1]['function']; |
||
| 488 | $line = $debug[1]['line']; |
||
| 489 | $data = array( |
||
| 490 | 'merchantOrderId' => $this->merchantOrderId, |
||
| 491 | 'pagantisOrderId' => $this->pagantisOrderId, |
||
| 492 | 'message' => ($exception)? $exception->getMessage() : 'Unable to get Exception message', |
||
| 493 | 'statusCode' => ($exception)? $exception->getCode() : 'Unable to get Exception statusCode', |
||
| 494 | 'method' => $method, |
||
| 495 | 'file' => __FILE__, |
||
| 496 | 'line' => $line, |
||
| 497 | ); |
||
| 498 | $this->saveLog($data); |
||
| 499 | return $this->finishProcess(true); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Redirect the request to the e-commerce or show the output in json |
||
| 504 | * |
||
| 505 | * @param bool $error |
||
| 506 | */ |
||
| 507 | public function finishProcess($error = true) |
||
| 521 | } |
||
| 522 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths