| Total Complexity | 60 |
| Total Lines | 484 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 | /** @var mixed $pagantisOrder */ |
||
| 26 | protected $pagantisOrder; |
||
| 27 | |||
| 28 | /** @var $string $origin */ |
||
|
|
|||
| 29 | public $origin; |
||
| 30 | |||
| 31 | /** @var $string */ |
||
| 32 | public $order; |
||
| 33 | |||
| 34 | /** @var mixed $oscommerceOrderId */ |
||
| 35 | protected $oscommerceOrderId = ''; |
||
| 36 | |||
| 37 | /** @var mixed $cfg */ |
||
| 38 | protected $cfg; |
||
| 39 | |||
| 40 | /** @var Client $orderClient */ |
||
| 41 | protected $orderClient; |
||
| 42 | |||
| 43 | /** @var Order $oscommerceOrder */ |
||
| 44 | protected $oscommerceOrder; |
||
| 45 | |||
| 46 | /** @var mixed $pagantisOrderId */ |
||
| 47 | protected $pagantisOrderId = ''; |
||
| 48 | |||
| 49 | /** @var array $extraConfig */ |
||
| 50 | protected $extraConfig; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * notifyController constructor. |
||
| 54 | */ |
||
| 55 | public function __construct() |
||
| 56 | { |
||
| 57 | $this->extraConfig = $this->getExtraConfig(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Validation vs PagantisClient |
||
| 62 | * |
||
| 63 | * @return array|Array_ |
||
| 64 | * @throws Exception |
||
| 65 | */ |
||
| 66 | public function processInformation() |
||
| 67 | { |
||
| 68 | require_once('vendor/autoload.php'); |
||
| 69 | try { |
||
| 70 | $this->checkConcurrency(); |
||
| 71 | $this->getMerchantOrder(); |
||
| 72 | $this->getPagantisOrderId(); |
||
| 73 | $this->getPagantisOrder(); |
||
| 74 | $this->checkOrderStatus(); |
||
| 75 | $this->checkMerchantOrderStatus(); |
||
| 76 | $this->validateAmount(); |
||
| 77 | //$this->processMerchantOrder(); //ESTE PASO SE HACE EN EL CHECKOUT_PROCESS |
||
| 78 | } catch (\Exception $exception) { |
||
| 79 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 80 | $jsonResponse = new JsonExceptionResponse(); |
||
| 81 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 82 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 83 | $jsonResponse->setException($exception); |
||
| 84 | $this->insertLog($exception); |
||
| 85 | |||
| 86 | if ($this->extraConfig['PAGANTIS_URL_KO'] =! '') { |
||
| 87 | $koUrl = $this->extraConfig['PAGANTIS_URL_KO']; |
||
| 88 | } else { |
||
| 89 | $koUrl = trim(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL', false)); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($this->origin == 'notify') { |
||
| 93 | $jsonResponse->printResponse(); |
||
| 94 | } else { |
||
| 95 | if ($exception->getMessage() == AlreadyProcessedException::ERROR_MESSAGE) { |
||
| 96 | if ($this->extraConfig['PAGANTIS_URL_OK']!='') { |
||
| 97 | $confirmationUrl = $this->extraConfig['PAGANTIS_URL_OK']; |
||
| 98 | $confirmationUrl.="?order_id=$this->merchantOrderId"; |
||
| 99 | } else { |
||
| 100 | $confirmationUrl = trim(tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, '', 'SSL', false)); |
||
| 101 | $confirmationUrl.="?order_id=$this->merchantOrderId"; |
||
| 102 | } |
||
| 103 | |||
| 104 | header("Location: $confirmationUrl"); |
||
| 105 | exit; |
||
| 106 | } |
||
| 107 | |||
| 108 | header("Location: $koUrl"); |
||
| 109 | exit; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function confirmInformation() |
||
| 115 | { |
||
| 116 | try { |
||
| 117 | $this->confirmPagantisOrder(); |
||
| 118 | $this->updateBdInfo(); |
||
| 119 | $jsonResponse = new JsonSuccessResponse(); |
||
| 120 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 121 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 122 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 123 | } catch (\Exception $exception) { |
||
| 124 | $this->rollbackMerchantOrder(); |
||
| 125 | $this->unblockConcurrency($this->oscommerceOrderId); |
||
| 126 | $jsonResponse = new JsonExceptionResponse(); |
||
| 127 | $jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
| 128 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 129 | $jsonResponse->setException($exception); |
||
| 130 | $jsonResponse->toJson(); |
||
| 131 | $this->insertLog($exception); |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($this->origin == 'notify') { |
||
| 135 | $jsonResponse->printResponse(); |
||
| 136 | } else { |
||
| 137 | return $jsonResponse; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * COMMON FUNCTIONS |
||
| 143 | */ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @throws Exception |
||
| 147 | */ |
||
| 148 | private function checkConcurrency() |
||
| 149 | { |
||
| 150 | $this->getQuoteId(); |
||
| 151 | $this->checkConcurrencyTable(); |
||
| 152 | $this->unblockConcurrency(); |
||
| 153 | $this->blockConcurrency(); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @throws MerchantOrderNotFoundException |
||
| 158 | */ |
||
| 159 | private function getMerchantOrder() |
||
| 160 | { |
||
| 161 | global $order; |
||
| 162 | $this->oscommerceOrder = $order; |
||
| 163 | if (!isset($order->info)) { |
||
| 164 | throw new MerchantOrderNotFoundException(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @throws NoIdentificationException |
||
| 170 | */ |
||
| 171 | private function getPagantisOrderId() |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @throws OrderNotFoundException |
||
| 186 | */ |
||
| 187 | private function getPagantisOrder() |
||
| 188 | { |
||
| 189 | try { |
||
| 190 | $publicKey = trim(MODULE_PAYMENT_PAGANTIS_PK); |
||
| 191 | $secretKey = trim(MODULE_PAYMENT_PAGANTIS_SK); |
||
| 192 | $this->orderClient = new \Pagantis\OrdersApiClient\Client($publicKey, $secretKey); |
||
| 193 | /** @var Pagantis\OrdersApiClient\Model\Order pagantisOrder */ |
||
| 194 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
| 195 | } catch (\Exception $e) { |
||
| 196 | throw new OrderNotFoundException(); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @throws AlreadyProcessedException |
||
| 202 | * @throws WrongStatusException |
||
| 203 | */ |
||
| 204 | private function checkOrderStatus() |
||
| 205 | { |
||
| 206 | try { |
||
| 207 | $this->checkPagantisStatus(array('AUTHORIZED')); |
||
| 208 | } catch (\Exception $e) { |
||
| 209 | if ($this->findOscommerceOrderId()!='') { |
||
| 210 | throw new AlreadyProcessedException(); |
||
| 211 | } else { |
||
| 212 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 213 | $status = $this->pagantisOrder->getStatus(); |
||
| 214 | } else { |
||
| 215 | $status = '-'; |
||
| 216 | } |
||
| 217 | throw new WrongStatusException($status); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @throws AlreadyProcessedException |
||
| 224 | */ |
||
| 225 | private function checkMerchantOrderStatus() |
||
| 226 | { |
||
| 227 | global $order; |
||
| 228 | |||
| 229 | if ($order->info['order_status']!=='1') { |
||
| 230 | throw new AlreadyProcessedException(); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @throws AmountMismatchException |
||
| 236 | */ |
||
| 237 | private function validateAmount() |
||
| 238 | { |
||
| 239 | $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); |
||
| 240 | $ocAmount = intval($this->oscommerceOrder->info['total'] * 100); |
||
| 241 | |||
| 242 | if ($pagantisAmount != $ocAmount) { |
||
| 243 | throw new AmountMismatchException($pagantisAmount, $ocAmount); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return false|string |
||
| 249 | * @throws UnknownException |
||
| 250 | */ |
||
| 251 | private function confirmPagantisOrder() |
||
| 252 | { |
||
| 253 | try { |
||
| 254 | $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
| 255 | } catch (\Exception $e) { |
||
| 256 | throw new UnknownException($e->getMessage()); |
||
| 257 | } |
||
| 258 | |||
| 259 | $jsonResponse = new JsonSuccessResponse(); |
||
| 260 | return $jsonResponse->toJson(); |
||
| 261 | } |
||
| 262 | /** |
||
| 263 | * UTILS FUNCTIONS |
||
| 264 | */ |
||
| 265 | |||
| 266 | /** STEP 1 CC - Check concurrency */ |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @throws QuoteNotFoundException |
||
| 270 | */ |
||
| 271 | private function getQuoteId() |
||
| 272 | { |
||
| 273 | if ($this->oscommerceOrderId == "") { |
||
| 274 | throw new QuoteNotFoundException(); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Check if concurrency table exists |
||
| 280 | */ |
||
| 281 | private function checkConcurrencyTable() |
||
| 282 | { |
||
| 283 | $checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONCURRENCY."'"); |
||
| 284 | if (tep_db_num_rows($checkTable) == 0) { |
||
| 285 | $sql = "CREATE TABLE IF NOT EXISTS ".TABLE_PAGANTIS_CONCURRENCY." ( |
||
| 286 | id int NOT NULL, |
||
| 287 | `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
| 288 | UNIQUE KEY id(id))"; |
||
| 289 | tep_db_query($sql); |
||
| 290 | } |
||
| 291 | return; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Unlock the concurrency |
||
| 296 | * |
||
| 297 | * @param null $orderId |
||
| 298 | * @throws Exception |
||
| 299 | */ |
||
| 300 | private function unblockConcurrency($orderId = null) |
||
| 301 | { |
||
| 302 | try { |
||
| 303 | if ($orderId == null) { |
||
| 304 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where timestamp<".(time() - 5); |
||
| 305 | tep_db_query($query); |
||
| 306 | } elseif ($orderId!='') { |
||
| 307 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where id='$orderId'"; |
||
| 308 | tep_db_query($query); |
||
| 309 | } |
||
| 310 | } catch (Exception $exception) { |
||
| 311 | throw new ConcurrencyException(); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @throws \Exception |
||
| 317 | */ |
||
| 318 | private function blockConcurrency() |
||
| 319 | { |
||
| 320 | try { |
||
| 321 | $query = "SELECT timestamp FROM ".TABLE_PAGANTIS_CONCURRENCY." where id='$this->oscommerceOrderId'"; |
||
| 322 | $resultsSelect = tep_db_query($query); |
||
| 323 | $orderRow = tep_db_fetch_array($resultsSelect); |
||
| 324 | |||
| 325 | if (isset($orderRow['timestamp'])) { |
||
| 326 | throw new ConcurrencyException(); |
||
| 327 | } |
||
| 328 | |||
| 329 | $query = "INSERT INTO ".TABLE_PAGANTIS_CONCURRENCY." (id) VALUES ('$this->oscommerceOrderId')"; |
||
| 330 | tep_db_query($query); |
||
| 331 | |||
| 332 | } catch (Exception $exception) { |
||
| 333 | throw new ConcurrencyException(); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** STEP 2 GMO - Get Merchant Order */ |
||
| 338 | /** STEP 3 GPOI - Get Pagantis OrderId */ |
||
| 339 | /** STEP 4 GPO - Get Pagantis Order */ |
||
| 340 | /** STEP 5 COS - Check Order Status */ |
||
| 341 | /** |
||
| 342 | * @param $statusArray |
||
| 343 | * |
||
| 344 | * @throws \Exception |
||
| 345 | */ |
||
| 346 | private function checkPagantisStatus($statusArray) |
||
| 347 | { |
||
| 348 | $pagantisStatus = array(); |
||
| 349 | foreach ($statusArray as $status) { |
||
| 350 | $pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); |
||
| 351 | } |
||
| 352 | |||
| 353 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 354 | $payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); |
||
| 355 | if (!$payed) { |
||
| 356 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 357 | $status = $this->pagantisOrder->getStatus(); |
||
| 358 | } else { |
||
| 359 | $status = '-'; |
||
| 360 | } |
||
| 361 | throw new WrongStatusException($status); |
||
| 362 | } |
||
| 363 | } else { |
||
| 364 | throw new OrderNotFoundException(); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | private function findOscommerceOrderId() |
||
| 372 | { |
||
| 373 | $query = "select os_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_reference='".$this->oscommerceOrderId."'"; |
||
| 374 | $resultsSelect = tep_db_query($query); |
||
| 375 | $orderRow = tep_db_fetch_array($resultsSelect); |
||
| 376 | $this->merchantOrderId = $orderRow['os_order_id']; |
||
| 377 | |||
| 378 | return $orderRow['os_order_id']; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** STEP 6 CMOS - Check Merchant Order Status */ |
||
| 382 | /** STEP 7 VA - Validate Amount */ |
||
| 383 | /** STEP 8 PMO - Process Merchant Order */ |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Save the order status with the related identification |
||
| 387 | */ |
||
| 388 | private function updateBdInfo() |
||
| 389 | { |
||
| 390 | global $insert_id; |
||
| 391 | $this->merchantOrderId = $insert_id; |
||
| 392 | $query = "update ".TABLE_PAGANTIS_ORDERS." set os_order_id='$insert_id' where os_order_reference='$this->oscommerceOrderId'"; |
||
| 393 | tep_db_query($query); |
||
| 394 | |||
| 395 | $metadataOrder = $this->pagantisOrder->getMetadata(); |
||
| 396 | |||
| 397 | $metadataInfo = ''; |
||
| 398 | foreach ($metadataOrder as $metadataKey => $metadataValue) { |
||
| 399 | if ($metadataKey == 'promotedProduct') { |
||
| 400 | $metadataInfo.= "/Producto promocionado = $metadataValue"; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | $comment = "Pagantis id=$this->pagantisOrderId/Via=".ucfirst($this->origin)."/".$metadataInfo; |
||
| 405 | $query = "insert into ".TABLE_ORDERS_STATUS_HISTORY ."(comments, orders_id, orders_status_id, customer_notified, date_added) values |
||
| 406 | ('$comment', ".$insert_id.", '2', -1, now() )"; |
||
| 407 | tep_db_query($query); |
||
| 408 | |||
| 409 | $query = "update ".TABLE_ORDERS." set orders_status='2' where orders_id='$insert_id'"; |
||
| 410 | tep_db_query($query); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** STEP 9 CPO - Confirmation Pagantis Order */ |
||
| 414 | private function rollbackMerchantOrder() |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param $exception |
||
| 426 | */ |
||
| 427 | private function insertLog($exception) |
||
| 428 | { |
||
| 429 | if ($exception instanceof \Exception) { |
||
| 430 | $logEntry= new LogEntry(); |
||
| 431 | $logEntryJson = $logEntry->error($exception)->toJson(); |
||
| 432 | |||
| 433 | $query = "insert into ".TABLE_PAGANTIS_LOG."(log) values ('$logEntryJson')"; |
||
| 434 | tep_db_query($query); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | private function getExtraConfig() |
||
| 455 | } |
||
| 456 | |||
| 457 | /*** |
||
| 458 | * SETTERS Y GETTERS |
||
| 459 | */ |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return mixed |
||
| 463 | */ |
||
| 464 | public function getOscommerceOrderId() |
||
| 465 | { |
||
| 466 | return $this->oscommerceOrderId; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param $oscommerceOrderId |
||
| 471 | */ |
||
| 472 | public function setOscommerceOrderId($oscommerceOrderId) |
||
| 473 | { |
||
| 474 | $this->oscommerceOrderId = $oscommerceOrderId; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return mixed |
||
| 479 | */ |
||
| 480 | public function getOrigin() |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param mixed $origin |
||
| 487 | */ |
||
| 488 | public function setOrigin($origin) |
||
| 489 | { |
||
| 490 | $this->origin = $origin; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return String |
||
| 495 | */ |
||
| 496 | public function getOrderStatus() |
||
| 497 | { |
||
| 498 | return $this->orderStatus; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param String $orderStatus |
||
| 503 | */ |
||
| 504 | public function setOrderStatus($orderStatus) |
||
| 507 | } |
||
| 508 | } |
||
| 509 |