Conditions | 17 |
Paths | 7616 |
Total Lines | 213 |
Code Lines | 162 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 | $metadataOrder = new Metadata(); |
||
215 | $metadata = $this->getMetadata(); |
||
216 | foreach ($metadata as $key => $metadatum) { |
||
217 | $metadataOrder->addMetadata($key, $metadatum); |
||
218 | } |
||
219 | |||
220 | $details = new Details(); |
||
221 | $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value'); |
||
222 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
223 | $items = $quote->getAllVisibleItems(); |
||
224 | $promotedAmount = 0; |
||
225 | foreach ($items as $key => $item) { |
||
226 | $product = new Product(); |
||
227 | $product |
||
228 | ->setAmount(intval(100 * $item->getPrice())) |
||
229 | ->setQuantity($item->getQty()) |
||
230 | ->setDescription($item->getName()); |
||
231 | $details->addProduct($product); |
||
232 | |||
233 | $promotedProduct = $this->isPromoted($item); |
||
234 | if ($promotedProduct == 'true') { |
||
235 | $promotedAmount+=$product->getAmount()*$item->getQty(); |
||
236 | $promotedMessage = 'Promoted Item: ' . $item->getName() . |
||
237 | ' Price: ' . $item->getPrice() . |
||
238 | ' Qty: ' . $item->getQty() . |
||
239 | ' Item ID: ' . $item->getItemId(); |
||
240 | $metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | $orderShoppingCart = new ShoppingCart(); |
||
245 | $orderShoppingCart |
||
246 | ->setDetails($details) |
||
247 | ->setOrderReference($quote->getId()) |
||
248 | ->setPromotedAmount(0) |
||
249 | ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal()))) |
||
250 | ; |
||
251 | |||
252 | $orderConfigurationUrls = new Urls(); |
||
253 | $quoteId = $quote->getId(); |
||
254 | $okUrl = $this->_url->getUrl( |
||
255 | 'pagantis/notify/index', |
||
256 | ['_query' => ['quoteId'=>$quoteId]] |
||
257 | ); |
||
258 | if (version_compare($metadata['magento'], '2.3.0') >= 0) { |
||
259 | $okUrl = $this->_url->getUrl('pagantis/notify/indexV2', ['_query' => ['quoteId'=>$quoteId]]); |
||
260 | } |
||
261 | |||
262 | $orderConfigurationUrls |
||
263 | ->setCancel($cancelUrl) |
||
264 | ->setKo($okUrl) |
||
265 | ->setAuthorizedNotificationCallback($okUrl) |
||
266 | ->setRejectedNotificationCallback($okUrl) |
||
267 | ->setOk($okUrl) |
||
268 | ; |
||
269 | |||
270 | $orderChannel = new Channel(); |
||
271 | $orderChannel |
||
272 | ->setAssistedSale(false) |
||
273 | ->setType(Channel::ONLINE) |
||
274 | ; |
||
275 | |||
276 | $haystack = $this->store->getLocale(); |
||
277 | $language = strstr($haystack, '_', true); |
||
278 | $orderConfiguration = new Configuration(); |
||
279 | $orderConfiguration |
||
280 | ->setChannel($orderChannel) |
||
281 | ->setUrls($orderConfigurationUrls) |
||
282 | ->setPurchaseCountry($language) |
||
283 | ; |
||
284 | |||
285 | |||
286 | $order = new Order(); |
||
287 | $order |
||
288 | ->setConfiguration($orderConfiguration) |
||
289 | ->setMetadata($metadataOrder) |
||
290 | ->setShoppingCart($orderShoppingCart) |
||
291 | ->setUser($orderUser) |
||
292 | ; |
||
293 | |||
294 | if ($this->config['pagantis_public_key']=='' || $this->config['pagantis_private_key']=='') { |
||
295 | throw new \Exception('Public and Secret Key not found'); |
||
296 | } |
||
297 | |||
298 | $orderClient = new Client( |
||
299 | $this->config['pagantis_public_key'], |
||
300 | $this->config['pagantis_private_key'] |
||
301 | ); |
||
302 | |||
303 | $order = $orderClient->createOrder($order); |
||
304 | if ($order instanceof Order) { |
||
305 | $url = $order->getActionUrls()->getForm(); |
||
306 | $result = $this->insertRow($quote->getId(), $order->getId()); |
||
307 | if (!$result) { |
||
308 | throw new \Exception('Unable to save pagantis-order-id'); |
||
309 | } |
||
310 | } else { |
||
311 | throw new \Exception('Order not created'); |
||
312 | } |
||
313 | } catch (\Exception $exception) { |
||
314 | $this->insertLog($exception); |
||
315 | echo $cancelUrl; |
||
316 | exit; |
||
317 | } |
||
318 | |||
319 | $displayMode = $this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE']; |
||
320 | if ($displayMode==='0') { |
||
321 | echo $url; |
||
322 | exit; |
||
323 | } else { |
||
324 | $iframeUrl = $this->_url->getUrl( |
||
325 | "pagantis/Payment/iframe", |
||
326 | ['_query' => ["orderId"=>$order->getId()]] |
||
327 | ); |
||
328 | echo $iframeUrl; |
||
329 | exit; |
||
330 | } |
||
484 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: