| Total Complexity | 62 |
| Total Lines | 486 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 2 | Features | 2 |
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() |
||
| 332 | { |
||
| 333 | try { |
||
| 334 | $metadataOrder = $this->pagantisOrder->getMetadata(); |
||
| 335 | $metadataInfo = ''; |
||
| 336 | foreach ($metadataOrder as $metadataKey => $metadataValue) { |
||
| 337 | if ($metadataKey == 'promotedProduct') { |
||
| 338 | $metadataInfo .= $metadataValue; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | $this->module->validateOrder( |
||
| 343 | $this->merchantOrderId, |
||
| 344 | Configuration::get('PS_OS_PAYMENT'), |
||
| 345 | $this->merchantOrder->getOrderTotal(true), |
||
| 346 | $this->module->displayName, |
||
| 347 | 'pagantisOrderId: ' . $this->pagantisOrder->getId() . ' ' . |
||
| 348 | 'pagantisOrderStatus: '. $this->pagantisOrder->getStatus() . |
||
| 349 | $this->amountMismatchError . |
||
| 350 | $metadataInfo, |
||
| 351 | array('transaction_id' => $this->pagantisOrderId), |
||
| 352 | null, |
||
| 353 | false, |
||
| 354 | $this->config['secureKey'] |
||
| 355 | ); |
||
| 356 | } catch (\Exception $exception) { |
||
| 357 | throw new UnknownException($exception->getMessage()); |
||
| 358 | } |
||
| 359 | try { |
||
| 360 | Db::getInstance()->update( |
||
| 361 | 'pagantis_order', |
||
| 362 | array('ps_order_id' => $this->module->currentOrder), |
||
| 363 | 'id = \''. $this->merchantOrderId . '\' and order_id = \'' . $this->pagantisOrderId . '\'' |
||
| 364 | ); |
||
| 365 | } catch (\Exception $exception) { |
||
| 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( |
||
| 385 | 'message' => $message |
||
| 386 | )); |
||
| 387 | } catch (\Exception $exception) { |
||
| 388 | // Do nothing |
||
| 389 | } |
||
| 390 | } catch (\Exception $exception) { |
||
| 391 | throw new UnknownException($exception->getMessage()); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Leave the merchant order as it was previously |
||
| 397 | * |
||
| 398 | * @throws Exception |
||
| 399 | */ |
||
| 400 | public function rollbackMerchantOrder() |
||
| 401 | { |
||
| 402 | // Do nothing because the order is created only when the purchase was successfully |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Lock the concurrency to prevent duplicated inputs |
||
| 407 | * |
||
| 408 | * @param $orderId |
||
| 409 | * @return bool|void |
||
| 410 | * @throws ConcurrencyException |
||
| 411 | */ |
||
| 412 | protected function blockConcurrency($orderId) |
||
| 413 | { |
||
| 414 | try { |
||
| 415 | $table = 'pagantis_cart_process'; |
||
| 416 | return Db::getInstance()->insert($table, array('id' => $orderId, 'timestamp' => (time()))); |
||
| 417 | } catch (\Exception $exception) { |
||
| 418 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 419 | throw new ConcurrencyException(); |
||
| 420 | } |
||
| 421 | |||
| 422 | $query = sprintf( |
||
| 423 | "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, FROM_UNIXTIME(timestamp)) as rest FROM %s WHERE %s", |
||
| 424 | self::CONCURRENCY_TIMEOUT, |
||
| 425 | _DB_PREFIX_.$table, |
||
| 426 | "id=$orderId" |
||
| 427 | ); |
||
| 428 | $resultSeconds = Db::getInstance()->getValue($query); |
||
| 429 | $restSeconds = isset($resultSeconds) ? ($resultSeconds) : 0; |
||
| 430 | $secondsToExpire = ($restSeconds>self::CONCURRENCY_TIMEOUT) ? self::CONCURRENCY_TIMEOUT : $restSeconds; |
||
| 431 | |||
| 432 | $logMessage = sprintf( |
||
| 433 | "Redirect concurrency, User have to wait %s seconds, default seconds %s, bd time to expire %s seconds", |
||
| 434 | $secondsToExpire, |
||
| 435 | self::CONCURRENCY_TIMEOUT, |
||
| 436 | $restSeconds |
||
| 437 | ); |
||
| 438 | |||
| 439 | $this->saveLog(array( |
||
| 440 | 'message' => $logMessage |
||
| 441 | )); |
||
| 442 | sleep($secondsToExpire+1); |
||
| 443 | // After waiting...user continue the confirmation, hoping that previous call have finished. |
||
| 444 | return true; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param null $orderId |
||
| 450 | * |
||
| 451 | * @throws ConcurrencyException |
||
| 452 | */ |
||
| 453 | private function unblockConcurrency($orderId = null) |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Do all the necessary actions to cancel the confirmation process in case of error |
||
| 472 | * 1. Unblock concurrency |
||
| 473 | * 2. Save log |
||
| 474 | * |
||
| 475 | * @param \Exception $exception |
||
| 476 | * |
||
| 477 | */ |
||
| 478 | public function cancelProcess($exception = null) |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Redirect the request to the e-commerce or show the output in json |
||
| 498 | * |
||
| 499 | * @param bool $error |
||
| 500 | */ |
||
| 501 | public function finishProcess($error = true) |
||
| 515 | } |
||
| 516 | } |
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