| Total Complexity | 51 | 
| Total Lines | 379 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 1 | 
Complex classes like PagantisPaymentModuleFrontController 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 PagantisPaymentModuleFrontController, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 18 | class PagantisPaymentModuleFrontController extends AbstractController  | 
            ||
| 19 | { | 
            ||
| 20 | /** @var string $language */  | 
            ||
| 21 | protected $language;  | 
            ||
| 22 | |||
| 23 | /**  | 
            ||
| 24 | * @param $customer  | 
            ||
| 25 | * @param $exception  | 
            ||
| 26 | */  | 
            ||
| 27 | protected function addLog($customer, $exception)  | 
            ||
| 28 |     { | 
            ||
| 29 |         if (_PS_VERSION_ < 1.6) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 30 | Logger::addLog(  | 
            ||
| 31 | 'Pagantis Exception For user ' .  | 
            ||
| 32 | $customer->email .  | 
            ||
| 33 | ' : ' .  | 
            ||
| 34 | $exception->getMessage(),  | 
            ||
| 35 | 3,  | 
            ||
| 36 | $exception->getCode(),  | 
            ||
| 37 | null,  | 
            ||
| 38 | null,  | 
            ||
| 39 | true  | 
            ||
| 40 | );  | 
            ||
| 41 |         } else { | 
            ||
| 42 | PrestaShopLogger::addLog(  | 
            ||
| 43 | 'Pagantis Exception For user ' .  | 
            ||
| 44 | $customer->email .  | 
            ||
| 45 | ' : ' .  | 
            ||
| 46 | $exception->getMessage(),  | 
            ||
| 47 | 3,  | 
            ||
| 48 | $exception->getCode(),  | 
            ||
| 49 | null,  | 
            ||
| 50 | null,  | 
            ||
| 51 | true  | 
            ||
| 52 | );  | 
            ||
| 53 | }  | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * Process Post Request  | 
            ||
| 58 | *  | 
            ||
| 59 | * @throws \Exception  | 
            ||
| 60 | */  | 
            ||
| 61 | public function postProcess()  | 
            ||
| 62 |     { | 
            ||
| 63 | $this->getUserLanguage();  | 
            ||
| 64 | |||
| 65 | /** @var Cart $cart */  | 
            ||
| 66 | $cart = $this->context->cart;  | 
            ||
| 67 | |||
| 68 |         if (!$cart->id) { | 
            ||
| 69 |             Tools::redirect('index.php?controller=order'); | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | /** @var Customer $customer */  | 
            ||
| 73 | $customer = $this->context->customer;  | 
            ||
| 74 | $query = array(  | 
            ||
| 75 | 'id_cart' => $cart->id,  | 
            ||
| 76 | 'key' => $cart->secure_key,  | 
            ||
| 77 | );  | 
            ||
| 78 | |||
| 79 | $koUrl = $this->context->link->getPageLink(  | 
            ||
| 80 | 'order',  | 
            ||
| 81 | null,  | 
            ||
| 82 | null,  | 
            ||
| 83 |             array('step'=>3) | 
            ||
| 84 | );  | 
            ||
| 85 |         $iframe = Pagantis::getExtraConfig('PAGANTIS_FORM_DISPLAY_TYPE'); | 
            ||
| 86 |         $cancelUrl = (Pagantis::getExtraConfig('PAGANTIS_URL_KO') !== '') ? | 
            ||
| 87 |             Pagantis::getExtraConfig('PAGANTIS_URL_KO') : $koUrl; | 
            ||
| 88 |         $pagantisPublicKey = Configuration::get('pagantis_public_key'); | 
            ||
| 89 |         $pagantisPrivateKey = Configuration::get('pagantis_private_key'); | 
            ||
| 90 | $okUrl = _PS_BASE_URL_SSL_.__PS_BASE_URI__  | 
            ||
| 91 | .'index.php?canonical=true&fc=module&module=pagantis&controller=notify&'  | 
            ||
| 92 | .http_build_query($query)  | 
            ||
| 93 | ;  | 
            ||
| 94 | |||
| 95 | $shippingAddress = new Address($cart->id_address_delivery);  | 
            ||
| 96 | $billingAddress = new Address($cart->id_address_invoice);  | 
            ||
| 97 | $curlInfo = curl_version();  | 
            ||
| 98 | $curlVersion = $curlInfo['version'];  | 
            ||
| 99 | $metadata = array(  | 
            ||
| 100 | 'ps' => _PS_VERSION_,  | 
            ||
| 101 | 'pagantis' => $this->module->version,  | 
            ||
| 102 | 'php' => phpversion(),  | 
            ||
| 103 | 'curl' => $curlVersion,  | 
            ||
| 104 | );  | 
            ||
| 105 | |||
| 106 |         try { | 
            ||
| 107 | $userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address();  | 
            ||
| 108 | $userAddress  | 
            ||
| 109 | ->setZipCode($shippingAddress->postcode)  | 
            ||
| 110 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname)  | 
            ||
| 111 | ->setCountryCode($this->language)  | 
            ||
| 112 | ->setCity($shippingAddress->city)  | 
            ||
| 113 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2)  | 
            ||
| 114 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 115 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 116 | ;  | 
            ||
| 117 | |||
| 118 | $orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address();  | 
            ||
| 119 | $orderShippingAddress  | 
            ||
| 120 | ->setZipCode($shippingAddress->postcode)  | 
            ||
| 121 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname)  | 
            ||
| 122 | ->setCountryCode($this->language)  | 
            ||
| 123 | ->setCity($shippingAddress->city)  | 
            ||
| 124 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2)  | 
            ||
| 125 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 126 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 127 | ->setFixPhone($shippingAddress->phone)  | 
            ||
| 128 | ->setMobilePhone($shippingAddress->phone_mobile)  | 
            ||
| 129 | ;  | 
            ||
| 130 | |||
| 131 | $orderBillingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address();  | 
            ||
| 132 | $orderBillingAddress  | 
            ||
| 133 | ->setZipCode($billingAddress->postcode)  | 
            ||
| 134 | ->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname)  | 
            ||
| 135 | ->setCountryCode($this->language)  | 
            ||
| 136 | ->setCity($billingAddress->city)  | 
            ||
| 137 | ->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2)  | 
            ||
| 138 | ->setTaxId($this->getTaxId($customer, $billingAddress, $shippingAddress))  | 
            ||
| 139 | ->setNationalId($this->getNationalId($customer, $billingAddress, $shippingAddress))  | 
            ||
| 140 | ->setFixPhone($billingAddress->phone)  | 
            ||
| 141 | ->setMobilePhone($billingAddress->phone_mobile)  | 
            ||
| 142 | ;  | 
            ||
| 143 | |||
| 144 | $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User();  | 
            ||
| 145 | $orderUser  | 
            ||
| 146 | ->setAddress($userAddress)  | 
            ||
| 147 | ->setFullName($orderShippingAddress->getFullName())  | 
            ||
| 148 | ->setBillingAddress($orderBillingAddress)  | 
            ||
| 149 | ->setEmail($this->context->cookie->logged ? $this->context->cookie->email : $customer->email)  | 
            ||
| 150 | ->setFixPhone($shippingAddress->phone)  | 
            ||
| 151 | ->setMobilePhone($shippingAddress->phone_mobile)  | 
            ||
| 152 | ->setShippingAddress($orderShippingAddress)  | 
            ||
| 153 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 154 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress))  | 
            ||
| 155 | ;  | 
            ||
| 156 | |||
| 157 |             if ($customer->birthday!='0000-00-00') { | 
            ||
| 158 | $orderUser->setDateOfBirth($customer->birthday);  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | $orders = Order::getCustomerOrders($customer->id);  | 
            ||
| 162 | /** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */  | 
            ||
| 163 |             foreach ($orders as $order) { | 
            ||
| 164 |                 if ($order['valid']) { | 
            ||
| 165 | $orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory();  | 
            ||
| 166 | $orderHistory  | 
            ||
| 167 | ->setAmount((string) floor(100 * $order['total_paid']))  | 
            ||
| 168 | ->setDate(new \DateTime($order['date_add']))  | 
            ||
| 169 | ;  | 
            ||
| 170 | $orderUser->addOrderHistory($orderHistory);  | 
            ||
| 171 | }  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | $metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata();  | 
            ||
| 175 |             foreach ($metadata as $key => $metadatum) { | 
            ||
| 176 | $metadataOrder->addMetadata($key, $metadatum);  | 
            ||
| 177 | }  | 
            ||
| 178 | |||
| 179 | $details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details();  | 
            ||
| 180 | $details->setShippingCost((string) floor(100 * $cart->getTotalShippingCost()));  | 
            ||
| 181 | $items = $cart->getProducts();  | 
            ||
| 182 | $promotedAmount = 0;  | 
            ||
| 183 |             foreach ($items as $key => $item) { | 
            ||
| 184 | $promotedProduct = $this->isPromoted($item['id_product']);  | 
            ||
| 185 | $product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product();  | 
            ||
| 186 | $product  | 
            ||
| 187 | ->setAmount((string) floor(100 * $item['price_wt']))  | 
            ||
| 188 | ->setQuantity($item['quantity'])  | 
            ||
| 189 | ->setDescription($item['name']);  | 
            ||
| 190 |                 if ($promotedProduct) { | 
            ||
| 191 | $promotedAmount+=$product->getAmount();  | 
            ||
| 192 | $productId = $item['id_product'];  | 
            ||
| 193 | $finalPrice = Product::getPriceStatic($productId);  | 
            ||
| 194 | $promotedMessage = 'Promoted Item: ' . $product->getDescription() .  | 
            ||
| 195 | ' Price: ' . $finalPrice .  | 
            ||
| 196 | ' Qty: ' . $product->getQuantity() .  | 
            ||
| 197 | ' Item ID: ' . $item['id_product'];  | 
            ||
| 198 |                     $metadataOrder->addMetadata('promotedProduct', $promotedMessage); | 
            ||
| 199 | }  | 
            ||
| 200 | $details->addProduct($product);  | 
            ||
| 201 | }  | 
            ||
| 202 | |||
| 203 | |||
| 204 | $orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart();  | 
            ||
| 205 | $totalAmount = (string) floor(100 * $cart->getOrderTotal(true));  | 
            ||
| 206 | $orderShoppingCart  | 
            ||
| 207 | ->setDetails($details)  | 
            ||
| 208 | ->setOrderReference($cart->id)  | 
            ||
| 209 | ->setTotalAmount($totalAmount)  | 
            ||
| 210 | ->setPromotedAmount($promotedAmount)  | 
            ||
| 211 | ;  | 
            ||
| 212 | |||
| 213 | $orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls();  | 
            ||
| 214 | $orderConfigurationUrls  | 
            ||
| 215 | ->setCancel($cancelUrl)  | 
            ||
| 216 | ->setKo($cancelUrl)  | 
            ||
| 217 | ->setAuthorizedNotificationCallback($okUrl)  | 
            ||
| 218 | ->setRejectedNotificationCallback($okUrl)  | 
            ||
| 219 | ->setOk($okUrl)  | 
            ||
| 220 | ;  | 
            ||
| 221 | |||
| 222 | $orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel();  | 
            ||
| 223 | $orderChannel  | 
            ||
| 224 | ->setAssistedSale(false)  | 
            ||
| 225 | ->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE)  | 
            ||
| 226 | ;  | 
            ||
| 227 | |||
| 228 | $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration();  | 
            ||
| 229 | $orderConfiguration  | 
            ||
| 230 | ->setChannel($orderChannel)  | 
            ||
| 231 | ->setUrls($orderConfigurationUrls)  | 
            ||
| 232 | ->setPurchaseCountry($this->language)  | 
            ||
| 233 | ;  | 
            ||
| 234 | |||
| 235 | $order = new \Pagantis\OrdersApiClient\Model\Order();  | 
            ||
| 236 | $order  | 
            ||
| 237 | ->setConfiguration($orderConfiguration)  | 
            ||
| 238 | ->setMetadata($metadataOrder)  | 
            ||
| 239 | ->setShoppingCart($orderShoppingCart)  | 
            ||
| 240 | ->setUser($orderUser)  | 
            ||
| 241 | ;  | 
            ||
| 242 |         } catch (\Exception $exception) { | 
            ||
| 243 | $this->saveLog(array(), $exception);  | 
            ||
| 244 | Tools::redirect($cancelUrl);  | 
            ||
| 245 | }  | 
            ||
| 246 | |||
| 247 | $url ='';  | 
            ||
| 248 |         try { | 
            ||
| 249 | $orderClient = new \Pagantis\OrdersApiClient\Client(  | 
            ||
| 250 | $pagantisPublicKey,  | 
            ||
| 251 | $pagantisPrivateKey  | 
            ||
| 252 | );  | 
            ||
| 253 | $order = $orderClient->createOrder($order);  | 
            ||
| 254 | |||
| 255 |             if ($order instanceof \Pagantis\OrdersApiClient\Model\Order) { | 
            ||
| 256 | $url = $order->getActionUrls()->getForm();  | 
            ||
| 257 | $orderId = $order->getId();  | 
            ||
| 258 | $sql = "INSERT INTO `" . _DB_PREFIX_ . "pagantis_order` (`id`, `order_id`)  | 
            ||
| 259 |                      VALUES ('$cart->id','$orderId')  | 
            ||
| 260 | ON DUPLICATE KEY UPDATE `order_id` = '$orderId'";  | 
            ||
| 261 | $result = Db::getInstance()->execute($sql);  | 
            ||
| 262 |                 if (!$result) { | 
            ||
| 263 |                     throw new UnknownException('Unable to save pagantis-order-id in database: '. $sql); | 
            ||
| 264 | }  | 
            ||
| 265 |             } else { | 
            ||
| 266 | throw new OrderNotFoundException();  | 
            ||
| 267 | }  | 
            ||
| 268 |         } catch (\Exception $exception) { | 
            ||
| 269 | $this->saveLog(array(), $exception);  | 
            ||
| 270 | Tools::redirect($cancelUrl);  | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 |         if (!$iframe) { | 
            ||
| 274 | Tools::redirect($url);  | 
            ||
| 275 |         } else { | 
            ||
| 276 | $this->context->smarty->assign(array(  | 
            ||
| 277 | 'url' => $url,  | 
            ||
| 278 | 'checkoutUrl' => $cancelUrl,  | 
            ||
| 279 | ));  | 
            ||
| 280 | |||
| 281 |             try { | 
            ||
| 282 |                 if (_PS_VERSION_ < 1.7) { | 
            ||
| 283 |                     $this->setTemplate('payment-15.tpl'); | 
            ||
| 284 |                 } else { | 
            ||
| 285 |                     $this->setTemplate('module:pagantis/views/templates/front/payment-17.tpl'); | 
            ||
| 286 | }  | 
            ||
| 287 |             } catch (\Exception $exception) { | 
            ||
| 288 | $this->saveLog(array(), $exception);  | 
            ||
| 289 | Tools::redirect($url);  | 
            ||
| 290 | }  | 
            ||
| 291 | }  | 
            ||
| 292 | }  | 
            ||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * @param null $customer  | 
            ||
| 296 | * @param null $addressOne  | 
            ||
| 297 | * @param null $addressTwo  | 
            ||
| 298 | * @return mixed|null  | 
            ||
| 299 | */  | 
            ||
| 300 | private function getNationalId($customer = null, $addressOne = null, $addressTwo = null)  | 
            ||
| 301 |     { | 
            ||
| 302 |         if ($customer !== null && isset($customer->national_id)) { | 
            ||
| 303 | return $customer->national_id;  | 
            ||
| 304 |         } elseif ($addressOne !== null and isset($addressOne->national_id)) { | 
            ||
| 305 | return $addressOne->national_id;  | 
            ||
| 306 |         } elseif ($addressOne !== null and isset($addressOne->dni)) { | 
            ||
| 307 | return $addressOne->dni;  | 
            ||
| 308 |         } elseif ($addressTwo !== null and isset($addressTwo->national_id)) { | 
            ||
| 309 | return $addressTwo->national_id;  | 
            ||
| 310 |         } elseif ($addressTwo !== null and isset($addressTwo->dni)) { | 
            ||
| 311 | return $addressTwo->dni;  | 
            ||
| 312 |         } else { | 
            ||
| 313 | return null;  | 
            ||
| 314 | }  | 
            ||
| 315 | }  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * @param null $customer  | 
            ||
| 319 | * @param null $addressOne  | 
            ||
| 320 | * @param null $addressTwo  | 
            ||
| 321 | * @return mixed|null  | 
            ||
| 322 | */  | 
            ||
| 323 | private function getTaxId($customer = null, $addressOne = null, $addressTwo = null)  | 
            ||
| 335 | }  | 
            ||
| 336 | }  | 
            ||
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * @param $item  | 
            ||
| 340 | *  | 
            ||
| 341 | * @return bool  | 
            ||
| 342 | */  | 
            ||
| 343 | private function isPromoted($itemId)  | 
            ||
| 350 | }  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * Get user language  | 
            ||
| 354 | */  | 
            ||
| 355 | private function getUserLanguage()  | 
            ||
| 365 | }  | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * @param array $input  | 
            ||
| 369 | * @param $columnKey  | 
            ||
| 370 | * @param null $indexKey  | 
            ||
| 371 | *  | 
            ||
| 372 | * @return array|bool  | 
            ||
| 373 | */  | 
            ||
| 374 | private function arrayColumn(array $input, $columnKey, $indexKey = null)  | 
            ||
| 399 |