| Total Complexity | 66 |
| Total Lines | 560 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like notifyController 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 notifyController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class notifyController |
||
| 24 | { |
||
| 25 | /** Seconds to expire a locked request */ |
||
| 26 | const CONCURRENCY_TIMEOUT = 10; |
||
| 27 | |||
| 28 | /** @var mixed $pagantisOrder */ |
||
| 29 | protected $pagantisOrder; |
||
| 30 | |||
| 31 | /** @var $string $origin */ |
||
|
|
|||
| 32 | public $origin; |
||
| 33 | |||
| 34 | /** @var $string */ |
||
| 35 | public $order; |
||
| 36 | |||
| 37 | /** @var mixed $oscommerceOrderId */ |
||
| 38 | protected $oscommerceOrderId = ''; |
||
| 39 | |||
| 40 | /** @var mixed $cfg */ |
||
| 41 | protected $cfg; |
||
| 42 | |||
| 43 | /** @var Client $orderClient */ |
||
| 44 | protected $orderClient; |
||
| 45 | |||
| 46 | /** @var Order $oscommerceOrder */ |
||
| 47 | protected $oscommerceOrder; |
||
| 48 | |||
| 49 | /** @var mixed $pagantisOrderId */ |
||
| 50 | protected $pagantisOrderId = ''; |
||
| 51 | |||
| 52 | /** @var array $extraConfig */ |
||
| 53 | protected $extraConfig; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * notifyController constructor. |
||
| 57 | */ |
||
| 58 | public function __construct() |
||
| 59 | { |
||
| 60 | $this->extraConfig = $this->getExtraConfig(); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Validation vs PagantisClient |
||
| 65 | * |
||
| 66 | * @return array|Array_ |
||
| 67 | * @throws Exception |
||
| 68 | */ |
||
| 69 | public function processInformation() |
||
| 70 | { |
||
| 71 | require_once('vendor/autoload.php'); |
||
| 72 | try { |
||
| 73 | $this->checkConcurrency(); |
||
| 74 | $this->getMerchantOrder(); |
||
| 75 | $this->getPagantisOrderId(); |
||
| 76 | $this->getPagantisOrder(); |
||
| 77 | $this->checkOrderStatus(); |
||
| 78 | $this->checkMerchantOrderStatus(); |
||
| 79 | $this->validateAmount(); |
||
| 80 | //$this->processMerchantOrder(); //ESTE PASO SE HACE EN EL CHECKOUT_PROCESS |
||
| 81 | } catch (\Exception $exception) { |
||
| 82 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 83 | $jsonResponse = new JsonExceptionResponse(); |
||
| 84 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 85 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 86 | $jsonResponse->setException($exception); |
||
| 87 | $this->insertLog($exception); |
||
| 88 | |||
| 89 | if ($this->extraConfig['PAGANTIS_URL_KO'] =! '') { |
||
| 90 | $koUrl = $this->extraConfig['PAGANTIS_URL_KO']; |
||
| 91 | } else { |
||
| 92 | $koUrl = trim(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL', false)); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($this->origin == 'notify') { |
||
| 96 | $jsonResponse->printResponse(); |
||
| 97 | } else { |
||
| 98 | if ($exception->getMessage() == AlreadyProcessedException::ERROR_MESSAGE) { |
||
| 99 | if ($this->extraConfig['PAGANTIS_URL_OK']!='') { |
||
| 100 | $confirmationUrl = $this->extraConfig['PAGANTIS_URL_OK']; |
||
| 101 | $confirmationUrl.="?order_id=$this->merchantOrderId"; |
||
| 102 | } else { |
||
| 103 | $confirmationUrl = trim(tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, '', 'SSL', false)); |
||
| 104 | $confirmationUrl.="?order_id=$this->merchantOrderId"; |
||
| 105 | } |
||
| 106 | |||
| 107 | header("Location: $confirmationUrl"); |
||
| 108 | exit; |
||
| 109 | } |
||
| 110 | |||
| 111 | header("Location: $koUrl"); |
||
| 112 | exit; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | public function confirmInformation() |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | $this->confirmPagantisOrder(); |
||
| 121 | $this->updateBdInfo(); |
||
| 122 | $jsonResponse = new JsonSuccessResponse(); |
||
| 123 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 124 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 125 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 126 | } catch (\Exception $exception) { |
||
| 127 | $this->rollbackMerchantOrder(); |
||
| 128 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 129 | $jsonResponse = new JsonExceptionResponse(); |
||
| 130 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 131 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 132 | $jsonResponse->setException($exception); |
||
| 133 | $jsonResponse->toJson(); |
||
| 134 | $this->insertLog($exception); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($this->origin == 'notify') { |
||
| 138 | $jsonResponse->printResponse(); |
||
| 139 | } else { |
||
| 140 | return $jsonResponse; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * COMMON FUNCTIONS |
||
| 146 | */ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @throws Exception |
||
| 150 | */ |
||
| 151 | private function checkConcurrency() |
||
| 152 | { |
||
| 153 | $this->getQuoteId(); |
||
| 154 | $this->checkConcurrencyTable(); |
||
| 155 | $this->unblockConcurrency(); |
||
| 156 | $this->blockConcurrency(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @throws MerchantOrderNotFoundException |
||
| 161 | */ |
||
| 162 | private function getMerchantOrder() |
||
| 163 | { |
||
| 164 | global $order; |
||
| 165 | $this->oscommerceOrder = $order; |
||
| 166 | if (!isset($order->info)) { |
||
| 167 | throw new MerchantOrderNotFoundException(); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @throws NoIdentificationException |
||
| 173 | */ |
||
| 174 | private function getPagantisOrderId() |
||
| 175 | { |
||
| 176 | $query = sprintf( |
||
| 177 | "select pagantis_order_id from %s where os_order_reference='%s'", |
||
| 178 | TABLE_PAGANTIS_ORDERS, |
||
| 179 | $this->oscommerceOrderId |
||
| 180 | ); |
||
| 181 | $resultsSelect = tep_db_query($query); |
||
| 182 | while ($orderRow = tep_db_fetch_array($resultsSelect)) { |
||
| 183 | $this->pagantisOrderId = $orderRow['pagantis_order_id']; |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($this->pagantisOrderId == '') { |
||
| 187 | throw new NoIdentificationException(); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @throws OrderNotFoundException |
||
| 193 | */ |
||
| 194 | private function getPagantisOrder() |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @throws AlreadyProcessedException |
||
| 209 | * @throws WrongStatusException |
||
| 210 | */ |
||
| 211 | private function checkOrderStatus() |
||
| 212 | { |
||
| 213 | try { |
||
| 214 | $this->checkPagantisStatus(array('AUTHORIZED')); |
||
| 215 | } catch (\Exception $e) { |
||
| 216 | if ($this->findOscommerceOrderId()!='') { |
||
| 217 | throw new AlreadyProcessedException(); |
||
| 218 | } else { |
||
| 219 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 220 | $status = $this->pagantisOrder->getStatus(); |
||
| 221 | } else { |
||
| 222 | $status = '-'; |
||
| 223 | } |
||
| 224 | throw new WrongStatusException($status); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @throws AlreadyProcessedException |
||
| 231 | */ |
||
| 232 | private function checkMerchantOrderStatus() |
||
| 233 | { |
||
| 234 | global $order; |
||
| 235 | |||
| 236 | if ($order->info['order_status']!=='1') { |
||
| 237 | throw new AlreadyProcessedException(); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @throws AmountMismatchException |
||
| 243 | */ |
||
| 244 | private function validateAmount() |
||
| 245 | { |
||
| 246 | $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); |
||
| 247 | $ocAmount = intval($this->oscommerceOrder->info['total'] * 100); |
||
| 248 | |||
| 249 | if ($pagantisAmount != $ocAmount) { |
||
| 250 | throw new AmountMismatchException($pagantisAmount, $ocAmount); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return false|string |
||
| 256 | * @throws UnknownException |
||
| 257 | */ |
||
| 258 | private function confirmPagantisOrder() |
||
| 259 | { |
||
| 260 | try { |
||
| 261 | $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
| 262 | } catch (\Exception $e) { |
||
| 263 | /** @var Pagantis\OrdersApiClient\Model\Order pagantisOrder */ |
||
| 264 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
| 265 | if ($this->pagantisOrder->getStatus() !== Order::STATUS_CONFIRMED) { |
||
| 266 | throw new UnknownException($e->getMessage()); |
||
| 267 | } else { |
||
| 268 | $logMessage = 'Concurrency issue: Order_id '.$this->pagantisOrderId.' was confirmed by other process'; |
||
| 269 | $this->insertLog(null, $logMessage); |
||
| 270 | } |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | $jsonResponse = new JsonSuccessResponse(); |
||
| 275 | return $jsonResponse->toJson(); |
||
| 276 | } |
||
| 277 | /** |
||
| 278 | * UTILS FUNCTIONS |
||
| 279 | */ |
||
| 280 | |||
| 281 | /** STEP 1 CC - Check concurrency */ |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @throws QuoteNotFoundException |
||
| 285 | */ |
||
| 286 | private function getQuoteId() |
||
| 287 | { |
||
| 288 | if ($this->oscommerceOrderId == "") { |
||
| 289 | throw new QuoteNotFoundException(); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if concurrency table exists |
||
| 295 | */ |
||
| 296 | private function checkConcurrencyTable() |
||
| 297 | { |
||
| 298 | $checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONCURRENCY."'"); |
||
| 299 | if (tep_db_num_rows($checkTable) == 0) { |
||
| 300 | $sql = "CREATE TABLE IF NOT EXISTS ".TABLE_PAGANTIS_CONCURRENCY." ( |
||
| 301 | id int NOT NULL, |
||
| 302 | `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
| 303 | UNIQUE KEY id(id))"; |
||
| 304 | tep_db_query($sql); |
||
| 305 | } |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Unlock the concurrency |
||
| 311 | * |
||
| 312 | * @param null $orderId |
||
| 313 | * @throws Exception |
||
| 314 | */ |
||
| 315 | private function unblockConcurrency($orderId = null) |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @throws \Exception |
||
| 336 | */ |
||
| 337 | private function blockConcurrency() |
||
| 338 | { |
||
| 339 | try { |
||
| 340 | $query = "SELECT timestamp FROM ".TABLE_PAGANTIS_CONCURRENCY." where id='$this->oscommerceOrderId'"; |
||
| 341 | $resultsSelect = tep_db_query($query); |
||
| 342 | $orderRow = tep_db_fetch_array($resultsSelect); |
||
| 343 | if (isset($orderRow['timestamp'])) { |
||
| 344 | if ($this->origin == 'notify') { |
||
| 345 | throw new ConcurrencyException(); |
||
| 346 | } else { |
||
| 347 | $query = sprintf( |
||
| 348 | "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, timestamp) as rest FROM %s %s", |
||
| 349 | self::CONCURRENCY_TIMEOUT, |
||
| 350 | TABLE_PAGANTIS_CONCURRENCY, |
||
| 351 | "WHERE id='".$this->oscommerceOrderId."'" |
||
| 352 | ); |
||
| 353 | $resultsSelect = tep_db_query($query); |
||
| 354 | $resultsArray = tep_db_fetch_array($resultsSelect); |
||
| 355 | $restSeconds = isset($resultsArray['rest']) ? ($resultsArray['rest']) : 0; |
||
| 356 | $expirationSec = ($restSeconds>self::CONCURRENCY_TIMEOUT)? self::CONCURRENCY_TIMEOUT : $restSeconds; |
||
| 357 | if ($expirationSec > 0) { |
||
| 358 | sleep($expirationSec + 1); |
||
| 359 | } |
||
| 360 | |||
| 361 | //Check if the notification have been processed |
||
| 362 | $query = sprintf( |
||
| 363 | "select os_order_id from %s where os_order_reference='%s'", |
||
| 364 | TABLE_PAGANTIS_ORDERS, |
||
| 365 | $this->oscommerceOrderId |
||
| 366 | ); |
||
| 367 | $resultsSelect = tep_db_query($query); |
||
| 368 | $os_order_id = tep_db_fetch_array($resultsSelect); |
||
| 369 | if (isset($os_order_id['os_order_id'])) { |
||
| 370 | $redirectUrl = trim(tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, '', 'SSL', false)); |
||
| 371 | $redirectUrl.="?order_id=$this->oscommerceOrderId"; |
||
| 372 | } else { |
||
| 373 | $redirectUrl = trim(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL', false)); |
||
| 374 | } |
||
| 375 | |||
| 376 | $logMessage = sprintf( |
||
| 377 | "User waiting %s seconds, default seconds %s, bd time to expire %s seconds => Url: %s", |
||
| 378 | $expirationSec, |
||
| 379 | self::CONCURRENCY_TIMEOUT, |
||
| 380 | $restSeconds, |
||
| 381 | $redirectUrl |
||
| 382 | ); |
||
| 383 | $this->insertLog(null, $logMessage); |
||
| 384 | |||
| 385 | header("Location: $redirectUrl"); |
||
| 386 | exit; |
||
| 387 | } |
||
| 388 | } else { |
||
| 389 | $query = "INSERT INTO ".TABLE_PAGANTIS_CONCURRENCY." (id) VALUES ('$this->oscommerceOrderId');"; |
||
| 390 | tep_db_query($query); |
||
| 391 | } |
||
| 392 | } catch (Exception $exception) { |
||
| 393 | throw new ConcurrencyException(); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** STEP 2 GMO - Get Merchant Order */ |
||
| 398 | /** STEP 3 GPOI - Get Pagantis OrderId */ |
||
| 399 | /** STEP 4 GPO - Get Pagantis Order */ |
||
| 400 | /** STEP 5 COS - Check Order Status */ |
||
| 401 | /** |
||
| 402 | * @param $statusArray |
||
| 403 | * |
||
| 404 | * @throws \Exception |
||
| 405 | */ |
||
| 406 | private function checkPagantisStatus($statusArray) |
||
| 407 | { |
||
| 408 | $pagantisStatus = array(); |
||
| 409 | foreach ($statusArray as $status) { |
||
| 410 | $pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); |
||
| 411 | } |
||
| 412 | |||
| 413 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 414 | $payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); |
||
| 415 | if (!$payed) { |
||
| 416 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 417 | $status = $this->pagantisOrder->getStatus(); |
||
| 418 | } else { |
||
| 419 | $status = '-'; |
||
| 420 | } |
||
| 421 | throw new WrongStatusException($status); |
||
| 422 | } |
||
| 423 | } else { |
||
| 424 | throw new OrderNotFoundException(); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | private function findOscommerceOrderId() |
||
| 432 | { |
||
| 433 | $query = sprintf( |
||
| 434 | "select os_order_id from %s where os_order_reference='%s'", |
||
| 435 | TABLE_PAGANTIS_ORDERS, |
||
| 436 | $this->oscommerceOrderId |
||
| 437 | ); |
||
| 438 | $resultsSelect = tep_db_query($query); |
||
| 439 | $orderRow = tep_db_fetch_array($resultsSelect); |
||
| 440 | $this->merchantOrderId = $orderRow['os_order_id']; |
||
| 441 | |||
| 442 | return $orderRow['os_order_id']; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** STEP 6 CMOS - Check Merchant Order Status */ |
||
| 446 | /** STEP 7 VA - Validate Amount */ |
||
| 447 | /** STEP 8 PMO - Process Merchant Order */ |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Save the order status with the related identification |
||
| 451 | */ |
||
| 452 | private function updateBdInfo() |
||
| 453 | { |
||
| 454 | global $insert_id; |
||
| 455 | $this->merchantOrderId = $insert_id; |
||
| 456 | $query = sprintf( |
||
| 457 | "update %s set os_order_id='%s' where os_order_reference='%s'", |
||
| 458 | TABLE_PAGANTIS_ORDERS, |
||
| 459 | $insert_id, |
||
| 460 | $this->oscommerceOrderId |
||
| 461 | ); |
||
| 462 | tep_db_query($query); |
||
| 463 | |||
| 464 | $metadataOrder = $this->pagantisOrder->getMetadata(); |
||
| 465 | |||
| 466 | $metadataInfo = ''; |
||
| 467 | foreach ($metadataOrder as $metadataKey => $metadataValue) { |
||
| 468 | if ($metadataKey == 'promotedProduct') { |
||
| 469 | $metadataInfo.= "/Producto promocionado = $metadataValue"; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | $comment = "Pagantis id=$this->pagantisOrderId/Via=".ucfirst($this->origin)."/".$metadataInfo; |
||
| 474 | $query = "insert into ".TABLE_ORDERS_STATUS_HISTORY ."(comments, orders_id, orders_status_id, customer_notified, |
||
| 475 | date_added) values ('$comment', ".$insert_id.", '2', -1, now() )"; |
||
| 476 | tep_db_query($query); |
||
| 477 | |||
| 478 | $query = "update ".TABLE_ORDERS." set orders_status='2' where orders_id='$insert_id'"; |
||
| 479 | tep_db_query($query); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** STEP 9 CPO - Confirmation Pagantis Order */ |
||
| 483 | private function rollbackMerchantOrder() |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param null $exception |
||
| 500 | * @param null $message |
||
| 501 | */ |
||
| 502 | private function insertLog($exception = null, $message = null) |
||
| 503 | { |
||
| 504 | $logEntry= new LogEntry(); |
||
| 505 | if ($exception instanceof \Exception) { |
||
| 506 | $logEntryJson = $logEntry->error($exception)->toJson(); |
||
| 507 | } else { |
||
| 508 | $logEntryJson = $logEntry->info($message)->toJson(); |
||
| 509 | } |
||
| 510 | $query = "insert into ".TABLE_PAGANTIS_LOG."(log) values ('$logEntryJson')"; |
||
| 511 | tep_db_query($query); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | private function getExtraConfig() |
||
| 531 | } |
||
| 532 | |||
| 533 | /*** |
||
| 534 | * SETTERS Y GETTERS |
||
| 535 | */ |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return mixed |
||
| 539 | */ |
||
| 540 | public function getOscommerceOrderId() |
||
| 541 | { |
||
| 542 | return $this->oscommerceOrderId; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param $oscommerceOrderId |
||
| 547 | */ |
||
| 548 | public function setOscommerceOrderId($oscommerceOrderId) |
||
| 549 | { |
||
| 550 | $this->oscommerceOrderId = $oscommerceOrderId; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return mixed |
||
| 555 | */ |
||
| 556 | public function getOrigin() |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param mixed $origin |
||
| 563 | */ |
||
| 564 | public function setOrigin($origin) |
||
| 565 | { |
||
| 566 | $this->origin = $origin; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @return String |
||
| 571 | */ |
||
| 572 | public function getOrderStatus() |
||
| 573 | { |
||
| 574 | return $this->orderStatus; |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param String $orderStatus |
||
| 579 | */ |
||
| 580 | public function setOrderStatus($orderStatus) |
||
| 583 | } |
||
| 584 | } |
||
| 585 |