| Conditions | 16 |
| Paths | 4976 |
| Total Lines | 202 |
| Code Lines | 153 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 117 | public function execute() |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | $cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
||
| 121 | $quote = $this->session->getQuote(); |
||
| 122 | /** @var Order $order */ |
||
| 123 | $lastOrder = $this->session->getLastRealOrder(); |
||
| 124 | $params = $this->getRequest()->getParams(); |
||
| 125 | $customer = $quote->getCustomer(); |
||
| 126 | $shippingAddress = $quote->getShippingAddress(); |
||
| 127 | |||
| 128 | if (isset($params['email']) && $params['email']!='') { |
||
| 129 | $this->session->setEmail($params['email']); //Get guest email after refresh page |
||
| 130 | $customer->setEmail($params['email']); |
||
| 131 | $quote->setCheckoutMethod('guest'); |
||
| 132 | $quote->getBillingAddress()->setEmail($params['email']); |
||
| 133 | } elseif ($customer->getEmail()=='') { |
||
| 134 | $customer->setEmail($this->session->getEmail()); |
||
| 135 | $quote->setCheckoutMethod('guest'); |
||
| 136 | $quote->getBillingAddress()->setEmail($this->session->getEmail()); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** @var Quote $currentQuote */ |
||
| 140 | $currentQuote = $this->quoteRepository->get($quote->getId()); |
||
| 141 | $currentQuote->setCustomerEmail($customer->getEmail()); |
||
| 142 | $this->quoteRepository->save($currentQuote); |
||
| 143 | |||
| 144 | $userAddress = new Address(); |
||
| 145 | $userAddress |
||
| 146 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 147 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 148 | ->setCountryCode('ES') |
||
| 149 | ->setCity($shippingAddress->getCity()) |
||
| 150 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 151 | ; |
||
| 152 | |||
| 153 | $tax_id = $this->getTaxId($quote->getBillingAddress()); |
||
| 154 | $orderShippingAddress = new Address(); |
||
| 155 | $orderShippingAddress |
||
| 156 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 157 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 158 | ->setCountryCode('ES') |
||
| 159 | ->setCity($shippingAddress->getCity()) |
||
| 160 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 161 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 162 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 163 | ->setTaxId($tax_id) |
||
| 164 | ; |
||
| 165 | |||
| 166 | $orderBillingAddress = new Address(); |
||
| 167 | $billingAddress = $quote->getBillingAddress(); |
||
| 168 | $orderBillingAddress |
||
| 169 | ->setZipCode($billingAddress->getPostcode()) |
||
| 170 | ->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 171 | ->setCountryCode('ES') |
||
| 172 | ->setCity($billingAddress->getCity()) |
||
| 173 | ->setAddress($billingAddress->getStreetFull()) |
||
| 174 | ->setFixPhone($billingAddress->getTelephone()) |
||
| 175 | ->setMobilePhone($billingAddress->getTelephone()) |
||
| 176 | ->setTaxId($tax_id) |
||
| 177 | ; |
||
| 178 | |||
| 179 | $orderUser = new User(); |
||
| 180 | $billingAddress->setEmail($customer->getEmail()); |
||
| 181 | $orderUser |
||
| 182 | ->setAddress($userAddress) |
||
| 183 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 184 | ->setBillingAddress($orderBillingAddress) |
||
| 185 | ->setEmail($customer->getEmail()) |
||
| 186 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 187 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 188 | ->setShippingAddress($orderShippingAddress) |
||
| 189 | ->setTaxId($tax_id) |
||
| 190 | ; |
||
| 191 | |||
| 192 | if ($customer->getDob()) { |
||
| 193 | $orderUser->setDateOfBirth($customer->getDob()); |
||
| 194 | } |
||
| 195 | if ($customer->getTaxvat()!='') { |
||
| 196 | $orderUser->setDni($customer->getTaxvat()); |
||
| 197 | $orderBillingAddress->setDni($customer->getTaxvat()); |
||
| 198 | $orderShippingAddress->setDni($customer->getTaxvat()); |
||
| 199 | $orderUser->setNationalId($customer->getTaxvat()); |
||
| 200 | $orderBillingAddress->setNationalId($customer->getTaxvat()); |
||
| 201 | $orderShippingAddress->setNationalId($customer->getTaxvat()); |
||
| 202 | } |
||
| 203 | |||
| 204 | $previousOrders = $this->getOrders($customer->getId()); |
||
| 205 | foreach ($previousOrders as $orderElement) { |
||
| 206 | $orderHistory = new OrderHistory(); |
||
| 207 | $orderHistory |
||
| 208 | ->setAmount(intval(100 * $orderElement['grand_total'])) |
||
| 209 | ->setDate(new \DateTime($orderElement['created_at'])) |
||
| 210 | ; |
||
| 211 | $orderUser->addOrderHistory($orderHistory); |
||
| 212 | } |
||
| 213 | |||
| 214 | $details = new Details(); |
||
| 215 | $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value'); |
||
| 216 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
| 217 | $items = $quote->getAllVisibleItems(); |
||
| 218 | foreach ($items as $key => $item) { |
||
| 219 | $product = new Product(); |
||
| 220 | $product |
||
| 221 | ->setAmount(intval(100 * $item->getPrice())) |
||
| 222 | ->setQuantity($item->getQty()) |
||
| 223 | ->setDescription($item->getName()); |
||
| 224 | $details->addProduct($product); |
||
| 225 | } |
||
| 226 | |||
| 227 | $orderShoppingCart = new ShoppingCart(); |
||
| 228 | $orderShoppingCart |
||
| 229 | ->setDetails($details) |
||
| 230 | ->setOrderReference($quote->getId()) |
||
| 231 | ->setPromotedAmount(0) |
||
| 232 | ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal()))) |
||
| 233 | ; |
||
| 234 | |||
| 235 | $metadataOrder = new Metadata(); |
||
| 236 | $metadata = $this->getMetadata(); |
||
| 237 | foreach ($metadata as $key => $metadatum) { |
||
| 238 | $metadataOrder->addMetadata($key, $metadatum); |
||
| 239 | } |
||
| 240 | |||
| 241 | $orderConfigurationUrls = new Urls(); |
||
| 242 | $quoteId = $quote->getId(); |
||
| 243 | $okUrl = $this->_url->getUrl( |
||
| 244 | 'pagantis/notify/index', |
||
| 245 | ['_query' => ['quoteId'=>$quoteId]] |
||
| 246 | ); |
||
| 247 | if (version_compare($metadata['magento'], '2.3.0') >= 0) { |
||
| 248 | $okUrl = $this->_url->getUrl('pagantis/notify/indexV2', ['_query' => ['quoteId'=>$quoteId]]); |
||
| 249 | } |
||
| 250 | |||
| 251 | $orderConfigurationUrls |
||
| 252 | ->setCancel($cancelUrl) |
||
| 253 | ->setKo($okUrl) |
||
| 254 | ->setAuthorizedNotificationCallback($okUrl) |
||
| 255 | ->setRejectedNotificationCallback($okUrl) |
||
| 256 | ->setOk($okUrl) |
||
| 257 | ; |
||
| 258 | |||
| 259 | $orderChannel = new Channel(); |
||
| 260 | $orderChannel |
||
| 261 | ->setAssistedSale(false) |
||
| 262 | ->setType(Channel::ONLINE) |
||
| 263 | ; |
||
| 264 | |||
| 265 | $haystack = $this->store->getLocale(); |
||
| 266 | $language = strstr($haystack, '_', true); |
||
| 267 | $orderConfiguration = new Configuration(); |
||
| 268 | $orderConfiguration |
||
| 269 | ->setChannel($orderChannel) |
||
| 270 | ->setUrls($orderConfigurationUrls) |
||
| 271 | ->setPurchaseCountry($language) |
||
| 272 | ; |
||
| 273 | |||
| 274 | |||
| 275 | $order = new Order(); |
||
| 276 | $order |
||
| 277 | ->setConfiguration($orderConfiguration) |
||
| 278 | ->setMetadata($metadataOrder) |
||
| 279 | ->setShoppingCart($orderShoppingCart) |
||
| 280 | ->setUser($orderUser) |
||
| 281 | ; |
||
| 282 | |||
| 283 | if ($this->config['pagantis_public_key']=='' || $this->config['pagantis_private_key']=='') { |
||
| 284 | throw new \Exception('Public and Secret Key not found'); |
||
| 285 | } |
||
| 286 | |||
| 287 | $orderClient = new Client( |
||
| 288 | $this->config['pagantis_public_key'], |
||
| 289 | $this->config['pagantis_private_key'] |
||
| 290 | ); |
||
| 291 | |||
| 292 | $order = $orderClient->createOrder($order); |
||
| 293 | if ($order instanceof Order) { |
||
| 294 | $url = $order->getActionUrls()->getForm(); |
||
| 295 | $result = $this->insertRow($quote->getId(), $order->getId()); |
||
| 296 | if (!$result) { |
||
| 297 | throw new \Exception('Unable to save pagantis-order-id'); |
||
| 298 | } |
||
| 299 | } else { |
||
| 300 | throw new \Exception('Order not created'); |
||
| 301 | } |
||
| 302 | } catch (\Exception $exception) { |
||
| 303 | $this->insertLog($exception); |
||
| 304 | echo $cancelUrl; |
||
| 305 | exit; |
||
| 306 | } |
||
| 307 | |||
| 308 | $displayMode = $this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE']; |
||
| 309 | if ($displayMode==='0') { |
||
| 310 | echo $url; |
||
| 311 | exit; |
||
| 312 | } else { |
||
| 313 | $iframeUrl = $this->_url->getUrl( |
||
| 314 | "pagantis/Payment/iframe", |
||
| 315 | ['_query' => ["orderId"=>$order->getId()]] |
||
| 316 | ); |
||
| 317 | echo $iframeUrl; |
||
| 318 | exit; |
||
| 319 | } |
||
| 460 |
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: