| Conditions | 16 |
| Paths | 4571 |
| Total Lines | 190 |
| Code Lines | 143 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 99 | public function execute() |
||
| 100 | { |
||
| 101 | try { |
||
| 102 | $cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
||
| 103 | $quote = $this->session->getQuote(); |
||
| 104 | /** @var Order $order */ |
||
| 105 | $lastOrder = $this->session->getLastRealOrder(); |
||
| 106 | $params = $this->getRequest()->getParams(); |
||
| 107 | $customer = $quote->getCustomer(); |
||
| 108 | $shippingAddress = $quote->getShippingAddress(); |
||
| 109 | |||
| 110 | if (isset($params['email']) && $params['email']!='') { |
||
| 111 | $this->session->setEmail($params['email']); //Get guest email after refresh page |
||
| 112 | $customer->setEmail($params['email']); |
||
| 113 | $quote->setCheckoutMethod('guest'); |
||
| 114 | $quote->getBillingAddress()->setEmail($params['email']); |
||
| 115 | } elseif ($customer->getEmail()=='') { |
||
| 116 | $customer->setEmail($this->session->getEmail()); |
||
| 117 | $quote->setCheckoutMethod('guest'); |
||
| 118 | $quote->getBillingAddress()->setEmail($this->session->getEmail()); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** @var Quote $currentQuote */ |
||
| 122 | $currentQuote = $this->quoteRepository->get($quote->getId()); |
||
| 123 | $currentQuote->setCustomerEmail($customer->getEmail()); |
||
| 124 | $this->quoteRepository->save($currentQuote); |
||
| 125 | |||
| 126 | $userAddress = new Address(); |
||
| 127 | $userAddress |
||
| 128 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 129 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 130 | ->setCountryCode('ES') |
||
| 131 | ->setCity($shippingAddress->getCity()) |
||
| 132 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 133 | ; |
||
| 134 | |||
| 135 | $orderShippingAddress = new Address(); |
||
| 136 | $orderShippingAddress |
||
| 137 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 138 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 139 | ->setCountryCode('ES') |
||
| 140 | ->setCity($shippingAddress->getCity()) |
||
| 141 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 142 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 143 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 144 | ; |
||
| 145 | |||
| 146 | $orderBillingAddress = new Address(); |
||
| 147 | $billingAddress = $quote->getBillingAddress(); |
||
| 148 | $orderBillingAddress |
||
| 149 | ->setZipCode($billingAddress->getPostcode()) |
||
| 150 | ->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 151 | ->setCountryCode('ES') |
||
| 152 | ->setCity($billingAddress->getCity()) |
||
| 153 | ->setAddress($billingAddress->getStreetFull()) |
||
| 154 | ->setFixPhone($billingAddress->getTelephone()) |
||
| 155 | ->setMobilePhone($billingAddress->getTelephone()) |
||
| 156 | ; |
||
| 157 | |||
| 158 | $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
||
| 159 | $billingAddress->setEmail($customer->getEmail()); |
||
| 160 | $orderUser |
||
| 161 | ->setAddress($userAddress) |
||
| 162 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 163 | ->setBillingAddress($orderBillingAddress) |
||
| 164 | ->setEmail($customer->getEmail()) |
||
| 165 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 166 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 167 | ->setShippingAddress($orderShippingAddress) |
||
| 168 | ; |
||
| 169 | |||
| 170 | if ($customer->getDob()) { |
||
| 171 | $orderUser->setDateOfBirth($customer->getDob()); |
||
| 172 | } |
||
| 173 | if ($customer->getTaxvat()!='') { |
||
| 174 | $orderUser->setDni($customer->getTaxvat()); |
||
| 175 | $orderBillingAddress->setDni($customer->getTaxvat()); |
||
| 176 | $orderShippingAddress->setDni($customer->getTaxvat()); |
||
| 177 | } |
||
| 178 | |||
| 179 | $previousOrders = $this->getOrders($customer->getId()); |
||
| 180 | foreach ($previousOrders as $orderElement) { |
||
| 181 | $orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
| 182 | $orderHistory |
||
| 183 | ->setAmount(intval(100 * $orderElement['grand_total'])) |
||
| 184 | ->setDate(new \DateTime($orderElement['created_at'])) |
||
| 185 | ; |
||
| 186 | $orderUser->addOrderHistory($orderHistory); |
||
| 187 | } |
||
| 188 | |||
| 189 | $details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
| 190 | $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value'); |
||
| 191 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
| 192 | $items = $quote->getAllVisibleItems(); |
||
| 193 | foreach ($items as $key => $item) { |
||
| 194 | $product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
| 195 | $product |
||
| 196 | ->setAmount(intval(100 * $item->getPrice())) |
||
| 197 | ->setQuantity($item->getQty()) |
||
| 198 | ->setDescription($item->getName()); |
||
| 199 | $details->addProduct($product); |
||
| 200 | } |
||
| 201 | |||
| 202 | $orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
||
| 203 | $orderShoppingCart |
||
| 204 | ->setDetails($details) |
||
| 205 | ->setOrderReference($quote->getId()) |
||
| 206 | ->setPromotedAmount(0) |
||
| 207 | ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal()))) |
||
| 208 | ; |
||
| 209 | |||
| 210 | $metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata(); |
||
| 211 | $metadata = $this->getMetadata(); |
||
| 212 | foreach ($metadata as $key => $metadatum) { |
||
| 213 | $metadataOrder->addMetadata($key, $metadatum); |
||
| 214 | } |
||
| 215 | |||
| 216 | $orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
| 217 | $quoteId = $quote->getId(); |
||
| 218 | $okUrl = $this->_url->getUrl( |
||
| 219 | 'pagantis/notify/index', |
||
| 220 | ['_query' => ['quoteId'=>$quoteId]] |
||
| 221 | ); |
||
| 222 | if (version_compare($metadata['magento'], '2.3.0') >= 0) { |
||
| 223 | $okUrl = $this->_url->getUrl('pagantis/notify/indexV2', ['_query' => ['quoteId'=>$quoteId]]); |
||
| 224 | } |
||
| 225 | |||
| 226 | $orderConfigurationUrls |
||
| 227 | ->setCancel($cancelUrl) |
||
| 228 | ->setKo($okUrl) |
||
| 229 | ->setAuthorizedNotificationCallback($okUrl) |
||
| 230 | ->setRejectedNotificationCallback($okUrl) |
||
| 231 | ->setOk($okUrl) |
||
| 232 | ; |
||
| 233 | |||
| 234 | $orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
| 235 | $orderChannel |
||
| 236 | ->setAssistedSale(false) |
||
| 237 | ->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
||
| 238 | ; |
||
| 239 | $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
||
| 240 | $orderConfiguration |
||
| 241 | ->setChannel($orderChannel) |
||
| 242 | ->setUrls($orderConfigurationUrls) |
||
| 243 | ; |
||
| 244 | |||
| 245 | $order = new \Pagantis\OrdersApiClient\Model\Order(); |
||
| 246 | $order |
||
| 247 | ->setConfiguration($orderConfiguration) |
||
| 248 | ->setMetadata($metadataOrder) |
||
| 249 | ->setShoppingCart($orderShoppingCart) |
||
| 250 | ->setUser($orderUser) |
||
| 251 | ; |
||
| 252 | |||
| 253 | if ($this->config['pagantis_public_key']=='' || $this->config['pagantis_private_key']=='') { |
||
| 254 | throw new \Exception('Public and Secret Key not found'); |
||
| 255 | } |
||
| 256 | |||
| 257 | $orderClient = new \Pagantis\OrdersApiClient\Client( |
||
| 258 | $this->config['pagantis_public_key'], |
||
| 259 | $this->config['pagantis_private_key'] |
||
| 260 | ); |
||
| 261 | |||
| 262 | $order = $orderClient->createOrder($order); |
||
| 263 | if ($order instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 264 | $url = $order->getActionUrls()->getForm(); |
||
| 265 | $result = $this->insertRow($quote->getId(), $order->getId()); |
||
| 266 | if (!$result) { |
||
| 267 | throw new \Exception('Unable to save pagantis-order-id'); |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | throw new \Exception('Order not created'); |
||
| 271 | } |
||
| 272 | } catch (\Exception $exception) { |
||
| 273 | $this->insertLog($exception); |
||
| 274 | echo $cancelUrl; |
||
| 275 | exit; |
||
| 276 | } |
||
| 277 | |||
| 278 | $displayMode = $this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE']; |
||
| 279 | if ($displayMode==='0') { |
||
| 280 | echo $url; |
||
| 281 | exit; |
||
| 282 | } else { |
||
| 283 | $iframeUrl = $this->_url->getUrl( |
||
| 284 | "pagantis/Payment/iframe", |
||
| 285 | ['_query' => ["orderId"=>$order->getId()]] |
||
| 286 | ); |
||
| 287 | echo $iframeUrl; |
||
| 288 | exit; |
||
| 289 | } |
||
| 414 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: