| Total Complexity | 46 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 3 |
Complex classes like PaylaterNotifyModuleFrontController 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 PaylaterNotifyModuleFrontController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class PagantisNotifyModuleFrontController extends AbstractController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var bool $processError |
||
| 33 | */ |
||
| 34 | protected $processError; |
||
| 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 \Pagantis\OrdersApiClient\Model\Order $pagantisOrder |
||
| 53 | */ |
||
| 54 | protected $pagantisOrder; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Pagantis\OrdersApiClient\Client $orderClient |
||
| 58 | */ |
||
| 59 | protected $orderClient; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var mixed $config |
||
| 63 | */ |
||
| 64 | protected $config; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Object $jsonResponse |
||
| 68 | */ |
||
| 69 | protected $jsonResponse; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @throws Exception |
||
| 73 | */ |
||
| 74 | public function postProcess() |
||
| 75 | { |
||
| 76 | try { |
||
| 77 | $this->checkConcurrency(); |
||
| 78 | $this->getMerchantOrder(); |
||
| 79 | $this->getPagantisOrderId(); |
||
| 80 | $this->getPagantisOrder(); |
||
| 81 | $this->checkOrderStatus(); |
||
| 82 | $this->checkMerchantOrderStatus(); |
||
| 83 | $this->validateAmount(); |
||
| 84 | $this->processMerchantOrder(); |
||
| 85 | } catch (\Exception $exception) { |
||
| 86 | $this->jsonResponse = new JsonExceptionResponse(); |
||
| 87 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 88 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 89 | $this->jsonResponse->setException($exception); |
||
| 90 | return $this->cancelProcess($this->jsonResponse); |
||
| 91 | } |
||
| 92 | |||
| 93 | try { |
||
| 94 | $this->jsonResponse = new JsonSuccessResponse(); |
||
| 95 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 96 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 97 | $this->confirmPagantisOrder(); |
||
| 98 | } catch (\Exception $exception) { |
||
| 99 | $this->rollbackMerchantOrder(); |
||
| 100 | $this->jsonResponse = new JsonExceptionResponse(); |
||
| 101 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 102 | $this->jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 103 | $this->jsonResponse->setException($exception); |
||
| 104 | return $this->cancelProcess($this->jsonResponse); |
||
| 105 | } |
||
| 106 | |||
| 107 | try { |
||
| 108 | $this->unblockConcurrency(); |
||
| 109 | } catch (\Exception $exception) { |
||
| 110 | // Do nothing |
||
| 111 | } |
||
| 112 | |||
| 113 | return $this->finishProcess(false); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check the concurrency of the purchase |
||
| 118 | * |
||
| 119 | * @throws Exception |
||
| 120 | */ |
||
| 121 | public function checkConcurrency() |
||
| 122 | { |
||
| 123 | $this->prepareVariables(); |
||
| 124 | $this->unblockConcurrency(); |
||
| 125 | $this->blockConcurrency($this->merchantOrderId); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Find and init variables needed to process payment |
||
| 130 | * |
||
| 131 | * @throws Exception |
||
| 132 | */ |
||
| 133 | public function prepareVariables() |
||
| 134 | { |
||
| 135 | $this->processError = false; |
||
| 136 | $callbackOkUrl = $this->context->link->getPageLink( |
||
| 137 | 'order-confirmation', |
||
| 138 | null, |
||
| 139 | null |
||
| 140 | ); |
||
| 141 | $callbackKoUrl = $this->context->link->getPageLink( |
||
| 142 | 'order', |
||
| 143 | null, |
||
| 144 | null, |
||
| 145 | array('step'=>3) |
||
| 146 | ); |
||
| 147 | try { |
||
| 148 | $this->config = array( |
||
| 149 | 'urlOK' => (Pagantis::getExtraConfig('PAGANTIS_URL_OK') !== '') ? |
||
| 150 | Pagantis::getExtraConfig('PAGANTIS_URL_OK') : $callbackOkUrl, |
||
| 151 | 'urlKO' => (Pagantis::getExtraConfig('PAGANTIS_URL_KO') !== '') ? |
||
| 152 | Pagantis::getExtraConfig('PAGANTIS_URL_KO') : $callbackKoUrl, |
||
| 153 | 'publicKey' => Configuration::get('pagantis_public_key'), |
||
| 154 | 'privateKey' => Configuration::get('pagantis_private_key'), |
||
| 155 | 'secureKey' => Tools::getValue('key'), |
||
| 156 | ); |
||
| 157 | } catch (\Exception $exception) { |
||
| 158 | throw new ConfigurationNotFoundException(); |
||
| 159 | } |
||
| 160 | |||
| 161 | $this->merchantOrderId = Tools::getValue('id_cart'); |
||
| 162 | if ($this->merchantOrderId == '') { |
||
| 163 | throw new QuoteNotFoundException(); |
||
| 164 | } |
||
| 165 | |||
| 166 | |||
| 167 | if (!($this->config['secureKey'] && $this->merchantOrderId && Module::isEnabled(self::PAGANTIS_CODE))) { |
||
| 168 | // This exception is only for Prestashop |
||
| 169 | throw new UnknownException('Module may not be enabled'); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Retrieve the merchant order by id |
||
| 175 | * |
||
| 176 | * @throws Exception |
||
| 177 | */ |
||
| 178 | public function getMerchantOrder() |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Find PAGANTIS Order Id in AbstractController::PAGANTIS_ORDERS_TABLE |
||
| 193 | * |
||
| 194 | * @throws Exception |
||
| 195 | */ |
||
| 196 | private function getPagantisOrderId() |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Find PAGANTIS Order in Orders Server using Pagantis\OrdersApiClient |
||
| 213 | * |
||
| 214 | * @throws Exception |
||
| 215 | */ |
||
| 216 | private function getPagantisOrder() |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Compare statuses of merchant order and PAGANTIS order, witch have to be the same. |
||
| 227 | * |
||
| 228 | * @throws Exception |
||
| 229 | */ |
||
| 230 | public function checkOrderStatus() |
||
| 231 | { |
||
| 232 | if ($this->pagantisOrder->getStatus() === PagantisModelOrder::STATUS_CONFIRMED) { |
||
| 233 | return $this->finishProcess(true); |
||
| 234 | } |
||
| 235 | |||
| 236 | if ($this->pagantisOrder->getStatus() !== PagantisModelOrder::STATUS_AUTHORIZED) { |
||
| 237 | $status = '-'; |
||
| 238 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 239 | $status = $this->pagantisOrder->getStatus(); |
||
| 240 | } |
||
| 241 | throw new WrongStatusException($status); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Check that the merchant order was not previously processes and is ready to be paid |
||
| 247 | * |
||
| 248 | * @throws Exception |
||
| 249 | */ |
||
| 250 | public function checkMerchantOrderStatus() |
||
| 251 | { |
||
| 252 | if ($this->merchantOrder->orderExists() !== false) { |
||
| 253 | throw new WrongStatusException('already_processed'); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Check that the merchant order and the order in PAGANTIS have the same amount to prevent hacking |
||
| 259 | * |
||
| 260 | * @throws Exception |
||
| 261 | */ |
||
| 262 | public function validateAmount() |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Process the merchant order and notify client |
||
| 274 | * |
||
| 275 | * @throws Exception |
||
| 276 | */ |
||
| 277 | public function processMerchantOrder() |
||
| 278 | { |
||
| 279 | try { |
||
| 280 | $this->module->validateOrder( |
||
| 281 | $this->merchantOrderId, |
||
| 282 | Configuration::get('PS_OS_PAYMENT'), |
||
| 283 | $this->merchantOrder->getOrderTotal(true), |
||
| 284 | $this->module->displayName, |
||
| 285 | 'pagantisOrderId: ' . $this->pagantisOrder->getId(). |
||
| 286 | 'pagantisOrderStatus: '. $this->pagantisOrder->getStatus(), |
||
| 287 | array('transaction_id' => $this->pagantisOrderId), |
||
| 288 | null, |
||
| 289 | false, |
||
| 290 | $this->config['secureKey'] |
||
| 291 | ); |
||
| 292 | } catch (\Exception $exception) { |
||
| 293 | throw new UnknownException($exception->getMessage()); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Confirm the order in PAGANTIS |
||
| 299 | * |
||
| 300 | * @throws Exception |
||
| 301 | */ |
||
| 302 | private function confirmPagantisOrder() |
||
| 303 | { |
||
| 304 | try { |
||
| 305 | $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
| 306 | } catch (\Exception $exception) { |
||
| 307 | throw new UnknownException($exception->getMessage()); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Leave the merchant order as it was previously |
||
| 313 | * |
||
| 314 | * @throws Exception |
||
| 315 | */ |
||
| 316 | public function rollbackMerchantOrder() |
||
| 317 | { |
||
| 318 | // Do nothing because the order is created only when the purchase was successfully |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Lock the concurrency to prevent duplicated inputs |
||
| 324 | * |
||
| 325 | * @param $orderId |
||
| 326 | * @throws Exception |
||
| 327 | */ |
||
| 328 | protected function blockConcurrency($orderId) |
||
| 329 | { |
||
| 330 | try { |
||
| 331 | Db::getInstance()->insert('pagantis_cart_process', array('id' => $orderId, 'timestamp' => (time()))); |
||
| 332 | } catch (\Exception $exception) { |
||
| 333 | throw new ConcurrencyException(); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Unlock the concurrency |
||
| 339 | * |
||
| 340 | * @throws Exception |
||
| 341 | */ |
||
| 342 | protected function unblockConcurrency() |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Do all the necessary actions to cancel the confirmation process in case of error |
||
| 353 | * 1. Unblock concurrency |
||
| 354 | * 2. Save log |
||
| 355 | * |
||
| 356 | * @param String|null $response Response as json |
||
| 357 | * |
||
| 358 | */ |
||
| 359 | public function cancelProcess($response = null) |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Redirect the request to the e-commerce or show the output in json |
||
| 394 | * |
||
| 395 | * @param bool $error |
||
| 396 | */ |
||
| 397 | public function finishProcess($error = true) |
||
| 398 | { |
||
| 399 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 400 | $this->jsonResponse->printResponse(); |
||
| 401 | } |
||
| 402 | |||
| 403 | $parameters = array( |
||
| 404 | 'id_cart' => $this->merchantOrderId, |
||
| 405 | 'key' => $this->config['secureKey'], |
||
| 406 | 'id_module' => $this->module->id, |
||
| 413 |
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