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