| Total Complexity | 58 | 
| Total Lines | 474 | 
| Duplicated Lines | 0 % | 
| Changes | 4 | ||
| 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() | ||
| 172 |     { | ||
| 173 | $query = "select pagantis_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_reference='".$this->oscommerceOrderId."'"; | ||
| 174 | $resultsSelect = tep_db_query($query); | ||
| 175 |         while ($orderRow = tep_db_fetch_array($resultsSelect)) { | ||
| 176 | $this->pagantisOrderId = $orderRow['pagantis_order_id']; | ||
| 177 | } | ||
| 178 | |||
| 179 |         if ($this->pagantisOrderId == '') { | ||
| 180 | throw new NoIdentificationException(); | ||
| 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 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); | ||
| 194 |         } catch (\Exception $e) { | ||
| 195 | throw new OrderNotFoundException(); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | /** | ||
| 200 | * @throws AlreadyProcessedException | ||
| 201 | * @throws WrongStatusException | ||
| 202 | */ | ||
| 203 | private function checkOrderStatus() | ||
| 204 |     { | ||
| 205 |         try { | ||
| 206 |             $this->checkPagantisStatus(array('AUTHORIZED')); | ||
| 207 |         } catch (\Exception $e) { | ||
| 208 |             if ($this->findOscommerceOrderId()!='') { | ||
| 209 | throw new AlreadyProcessedException(); | ||
| 210 |             } else { | ||
| 211 |                 if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { | ||
| 212 | $status = $this->pagantisOrder->getStatus(); | ||
| 213 |                 } else { | ||
| 214 | $status = '-'; | ||
| 215 | } | ||
| 216 | throw new WrongStatusException($status); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * @throws AlreadyProcessedException | ||
| 223 | */ | ||
| 224 | private function checkMerchantOrderStatus() | ||
| 225 |     { | ||
| 226 | global $order; | ||
| 227 | |||
| 228 |         if ($order->info['order_status']!=='1') { | ||
| 229 | throw new AlreadyProcessedException(); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | /** | ||
| 234 | * @throws AmountMismatchException | ||
| 235 | */ | ||
| 236 | private function validateAmount() | ||
| 237 |     { | ||
| 238 | $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); | ||
| 239 | $ocAmount = intval($this->oscommerceOrder->info['total'] * 100); | ||
| 240 | |||
| 241 |         if ($pagantisAmount != $ocAmount) { | ||
| 242 | throw new AmountMismatchException($pagantisAmount, $ocAmount); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | /** | ||
| 247 | * @return false|string | ||
| 248 | * @throws UnknownException | ||
| 249 | */ | ||
| 250 | private function confirmPagantisOrder() | ||
| 251 |     { | ||
| 252 |         try { | ||
| 253 | $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); | ||
| 254 |         } catch (\Exception $e) { | ||
| 255 | throw new UnknownException($e->getMessage()); | ||
| 256 | } | ||
| 257 | |||
| 258 | $jsonResponse = new JsonSuccessResponse(); | ||
| 259 | return $jsonResponse->toJson(); | ||
| 260 | } | ||
| 261 | /** | ||
| 262 | * UTILS FUNCTIONS | ||
| 263 | */ | ||
| 264 | |||
| 265 | /** STEP 1 CC - Check concurrency */ | ||
| 266 | |||
| 267 | /** | ||
| 268 | * @throws QuoteNotFoundException | ||
| 269 | */ | ||
| 270 | private function getQuoteId() | ||
| 271 |     { | ||
| 272 |         if ($this->oscommerceOrderId == "") { | ||
| 273 | throw new QuoteNotFoundException(); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Check if concurrency table exists | ||
| 279 | */ | ||
| 280 | private function checkConcurrencyTable() | ||
| 281 |     { | ||
| 282 |         $checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONCURRENCY."'"); | ||
| 283 |         if (tep_db_num_rows($checkTable) == 0) { | ||
| 284 | $sql = "CREATE TABLE IF NOT EXISTS ".TABLE_PAGANTIS_CONCURRENCY." ( | ||
| 285 | id int NOT NULL, | ||
| 286 | `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 287 | UNIQUE KEY id(id))"; | ||
| 288 | tep_db_query($sql); | ||
| 289 | } | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | |||
| 293 | /** | ||
| 294 | * Unlock the concurrency | ||
| 295 | * | ||
| 296 | * @param null $orderId | ||
| 297 | * @throws Exception | ||
| 298 | */ | ||
| 299 | private function unblockConcurrency($orderId = null) | ||
| 300 |     { | ||
| 301 |         try { | ||
| 302 |             if ($orderId == null) { | ||
| 303 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where timestamp<".(time() - 5); | ||
| 304 | tep_db_query($query); | ||
| 305 |             } elseif ($orderId!='') { | ||
| 306 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where id='$orderId'"; | ||
| 307 | tep_db_query($query); | ||
| 308 | } | ||
| 309 |         } catch (Exception $exception) { | ||
| 310 | throw new ConcurrencyException(); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * @throws \Exception | ||
| 316 | */ | ||
| 317 | private function blockConcurrency() | ||
| 318 |     { | ||
| 319 |         try { | ||
| 320 | $query = "SELECT timestamp FROM ".TABLE_PAGANTIS_CONCURRENCY." where id='$this->oscommerceOrderId'"; | ||
| 321 | $resultsSelect = tep_db_query($query); | ||
| 322 | $orderRow = tep_db_fetch_array($resultsSelect); | ||
| 323 | |||
| 324 |             if (isset($orderRow['timestamp'])) { | ||
| 325 | throw new ConcurrencyException(); | ||
| 326 | } | ||
| 327 | |||
| 328 |             $query = "INSERT INTO ".TABLE_PAGANTIS_CONCURRENCY." (id) VALUES ('$this->oscommerceOrderId')"; | ||
| 329 | tep_db_query($query); | ||
| 330 | |||
| 331 |         } catch (Exception $exception) { | ||
| 332 | throw new ConcurrencyException(); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | /** STEP 2 GMO - Get Merchant Order */ | ||
| 337 | /** STEP 3 GPOI - Get Pagantis OrderId */ | ||
| 338 | /** STEP 4 GPO - Get Pagantis Order */ | ||
| 339 | /** STEP 5 COS - Check Order Status */ | ||
| 340 | /** | ||
| 341 | * @param $statusArray | ||
| 342 | * | ||
| 343 | * @throws \Exception | ||
| 344 | */ | ||
| 345 | private function checkPagantisStatus($statusArray) | ||
| 346 |     { | ||
| 347 | $pagantisStatus = array(); | ||
| 348 |         foreach ($statusArray as $status) { | ||
| 349 |             $pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); | ||
| 350 | } | ||
| 351 | |||
| 352 |         if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { | ||
| 353 | $payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); | ||
| 354 |             if (!$payed) { | ||
| 355 |                 if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { | ||
| 356 | $status = $this->pagantisOrder->getStatus(); | ||
| 357 |                 } else { | ||
| 358 | $status = '-'; | ||
| 359 | } | ||
| 360 | throw new WrongStatusException($status); | ||
| 361 | } | ||
| 362 |         } else { | ||
| 363 | throw new OrderNotFoundException(); | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | /** | ||
| 368 | * @return mixed | ||
| 369 | */ | ||
| 370 | private function findOscommerceOrderId() | ||
| 371 |     { | ||
| 372 | $query = "select os_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_reference='".$this->oscommerceOrderId."'"; | ||
| 373 | $resultsSelect = tep_db_query($query); | ||
| 374 | $orderRow = tep_db_fetch_array($resultsSelect); | ||
| 375 | $this->merchantOrderId = $orderRow['os_order_id']; | ||
| 376 | |||
| 377 | return $orderRow['os_order_id']; | ||
| 378 | } | ||
| 379 | |||
| 380 | /** STEP 6 CMOS - Check Merchant Order Status */ | ||
| 381 | /** STEP 7 VA - Validate Amount */ | ||
| 382 | /** STEP 8 PMO - Process Merchant Order */ | ||
| 383 | |||
| 384 | /** | ||
| 385 | * Save the order status with the related identification | ||
| 386 | */ | ||
| 387 | private function updateBdInfo() | ||
| 388 |     { | ||
| 389 | global $insert_id; | ||
| 390 | $this->merchantOrderId = $insert_id; | ||
| 391 | $query = "update ".TABLE_PAGANTIS_ORDERS." set os_order_id='$insert_id' where os_order_reference='$this->oscommerceOrderId'"; | ||
| 392 | tep_db_query($query); | ||
| 393 | |||
| 394 | $comment = "Pagantis id=$this->pagantisOrderId/Via=".ucfirst($this->origin); | ||
| 395 | $query = "insert into ".TABLE_ORDERS_STATUS_HISTORY ."(comments, orders_id, orders_status_id, customer_notified, date_added) values | ||
| 396 |             ('$comment', ".$insert_id.", '2', -1, now() )"; | ||
| 397 | tep_db_query($query); | ||
| 398 | |||
| 399 | $query = "update ".TABLE_ORDERS." set orders_status='2' where orders_id='$insert_id'"; | ||
| 400 | tep_db_query($query); | ||
| 401 | } | ||
| 402 | |||
| 403 | /** STEP 9 CPO - Confirmation Pagantis Order */ | ||
| 404 | private function rollbackMerchantOrder() | ||
| 412 | } | ||
| 413 | |||
| 414 | /** | ||
| 415 | * @param $exception | ||
| 416 | */ | ||
| 417 | private function insertLog($exception) | ||
| 418 |     { | ||
| 419 |         if ($exception instanceof \Exception) { | ||
| 420 | $logEntry= new LogEntry(); | ||
| 421 | $logEntryJson = $logEntry->error($exception)->toJson(); | ||
| 422 | |||
| 423 |             $query = "insert into ".TABLE_PAGANTIS_LOG."(log) values ('$logEntryJson')"; | ||
| 424 | tep_db_query($query); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | /** | ||
| 429 | * @return array | ||
| 430 | */ | ||
| 431 | private function getExtraConfig() | ||
| 445 | } | ||
| 446 | |||
| 447 | /*** | ||
| 448 | * SETTERS Y GETTERS | ||
| 449 | */ | ||
| 450 | |||
| 451 | /** | ||
| 452 | * @return mixed | ||
| 453 | */ | ||
| 454 | public function getOscommerceOrderId() | ||
| 455 |     { | ||
| 456 | return $this->oscommerceOrderId; | ||
| 457 | } | ||
| 458 | |||
| 459 | /** | ||
| 460 | * @param $oscommerceOrderId | ||
| 461 | */ | ||
| 462 | public function setOscommerceOrderId($oscommerceOrderId) | ||
| 463 |     { | ||
| 464 | $this->oscommerceOrderId = $oscommerceOrderId; | ||
| 465 | } | ||
| 466 | |||
| 467 | /** | ||
| 468 | * @return mixed | ||
| 469 | */ | ||
| 470 | public function getOrigin() | ||
| 473 | } | ||
| 474 | |||
| 475 | /** | ||
| 476 | * @param mixed $origin | ||
| 477 | */ | ||
| 478 | public function setOrigin($origin) | ||
| 479 |     { | ||
| 480 | $this->origin = $origin; | ||
| 481 | } | ||
| 482 | |||
| 483 | /** | ||
| 484 | * @return String | ||
| 485 | */ | ||
| 486 | public function getOrderStatus() | ||
| 487 |     { | ||
| 488 | return $this->orderStatus; | ||
| 489 | } | ||
| 490 | |||
| 491 | /** | ||
| 492 | * @param String $orderStatus | ||
| 493 | */ | ||
| 494 | public function setOrderStatus($orderStatus) | ||
| 497 | } | ||
| 498 | } | ||
| 499 |