| Total Complexity | 64 | 
| Total Lines | 493 | 
| Duplicated Lines | 0 % | 
| Changes | 15 | ||
| Bugs | 3 | 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 = 10; | ||
| 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() | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | /** | ||
| 394 | * Leave the merchant order as it was previously | ||
| 395 | * | ||
| 396 | * @throws Exception | ||
| 397 | */ | ||
| 398 | public function rollbackMerchantOrder() | ||
| 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 |             if (Db::getInstance()->insert($table, array('id' => $orderId, 'timestamp' => (time()))) === false) { | ||
| 423 |                 if ($_SERVER['REQUEST_METHOD'] == 'POST') { | ||
| 424 | throw new ConcurrencyException(); | ||
| 425 | } | ||
| 426 | |||
| 427 | $query = sprintf( | ||
| 428 | "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, FROM_UNIXTIME(timestamp)) as rest FROM %s WHERE %s", | ||
| 429 | self::CONCURRENCY_TIMEOUT, | ||
| 430 | _DB_PREFIX_.$table, | ||
| 431 | "id=$orderId" | ||
| 432 | ); | ||
| 433 | $resultSeconds = Db::getInstance()->getValue($query); | ||
| 434 | $restSeconds = isset($resultSeconds) ? ($resultSeconds) : 0; | ||
| 435 | $secondsToExpire = ($restSeconds>self::CONCURRENCY_TIMEOUT) ? self::CONCURRENCY_TIMEOUT : $restSeconds; | ||
| 436 | |||
| 437 | $logMessage = sprintf( | ||
| 438 | "Redirect concurrency, User have to wait %s seconds, default seconds %s, bd time to expire %s seconds", | ||
| 439 | $secondsToExpire, | ||
| 440 | self::CONCURRENCY_TIMEOUT, | ||
| 441 | $restSeconds | ||
| 442 | ); | ||
| 443 | |||
| 444 | $this->saveLog(array( | ||
| 445 | 'message' => $logMessage | ||
| 446 | )); | ||
| 447 | sleep($secondsToExpire+1); | ||
| 448 | // After waiting...user continue the confirmation, hoping that previous call have finished. | ||
| 449 | return true; | ||
| 450 | } | ||
| 451 |         } catch (\Exception $exception) { | ||
| 452 | throw new ConcurrencyException(); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | /** | ||
| 457 | * @param null $orderId | ||
| 458 | * | ||
| 459 | * @throws ConcurrencyException | ||
| 460 | */ | ||
| 461 | private function unblockConcurrency($orderId = null) | ||
| 462 |     { | ||
| 463 |         try { | ||
| 464 |             if (is_null($orderId)) { | ||
| 465 | Db::getInstance()->delete( | ||
| 466 | 'pagantis_cart_process', | ||
| 467 | 'timestamp < ' . (time() - self::CONCURRENCY_TIMEOUT) | ||
| 468 | ); | ||
| 469 | return; | ||
| 470 | } | ||
| 471 |             Db::getInstance()->delete('pagantis_cart_process', 'id = \'' . $orderId . '\''); | ||
| 472 |         } catch (\Exception $exception) { | ||
| 473 | throw new ConcurrencyException(); | ||
| 474 | } | ||
| 475 | } | ||
| 476 | |||
| 477 | /** | ||
| 478 | * Do all the necessary actions to cancel the confirmation process in case of error | ||
| 479 | * 1. Unblock concurrency | ||
| 480 | * 2. Save log | ||
| 481 | * | ||
| 482 | * @param \Exception $exception | ||
| 483 | * | ||
| 484 | */ | ||
| 485 | public function cancelProcess($exception = null) | ||
| 486 |     { | ||
| 487 | $debug = debug_backtrace(); | ||
| 488 | $method = $debug[1]['function']; | ||
| 489 | $line = $debug[1]['line']; | ||
| 490 | $data = array( | ||
| 491 | 'merchantOrderId' => $this->merchantOrderId, | ||
| 492 | 'pagantisOrderId' => $this->pagantisOrderId, | ||
| 493 | 'message' => ($exception)? $exception->getMessage() : 'Unable to get Exception message', | ||
| 494 | 'statusCode' => ($exception)? $exception->getCode() : 'Unable to get Exception statusCode', | ||
| 495 | 'method' => $method, | ||
| 496 | 'file' => __FILE__, | ||
| 497 | 'line' => $line, | ||
| 498 | ); | ||
| 499 | $this->saveLog($data); | ||
| 500 | return $this->finishProcess(true); | ||
| 501 | } | ||
| 502 | |||
| 503 | /** | ||
| 504 | * Redirect the request to the e-commerce or show the output in json | ||
| 505 | * | ||
| 506 | * @param bool $error | ||
| 507 | */ | ||
| 508 | public function finishProcess($error = true) | ||
| 522 | } | ||
| 523 | } | 
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