Conditions | 14 |
Paths | > 20000 |
Total Lines | 200 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
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 |
||
52 | public function execute($request): void |
||
53 | { |
||
54 | /** @var Capture $request */ |
||
55 | RequestNotSupportedException::assertSupports($this, $request); |
||
56 | |||
57 | /** @var SyliusPaymentInterface $payment */ |
||
58 | $payment = $request->getModel(); |
||
59 | |||
60 | $this->gateway->execute($status = new GetStatus($payment)); |
||
61 | |||
62 | if ($status->isNew()) { |
||
63 | /** @var OrderInterface $order */ |
||
64 | $order = $payment->getOrder(); |
||
65 | |||
66 | /** @var CustomerInterface $customer */ |
||
67 | $customer = $order->getCustomer(); |
||
68 | |||
69 | /** @var AddressInterface $billingAddress */ |
||
70 | $billingAddress = $order->getBillingAddress(); |
||
71 | |||
72 | // $items = $order->getItems(); |
||
73 | |||
74 | SDK::setAccessToken($this->api->getAccessToken()); |
||
75 | |||
76 | SDK::setIntegratorId('dev_11586dc9e7f311eab4a00242ac130004'); |
||
77 | |||
78 | $preference = new Preference(); |
||
79 | |||
80 | try { |
||
81 | $preferenceItems = []; |
||
82 | |||
83 | // TODO create items for products when MercadoPago fix the API |
||
84 | |||
85 | // foreach ($items as $item) { |
||
86 | // $preferenceItem = new Item(); |
||
87 | // |
||
88 | // $preferenceItem->__set('id', $item->getId()); |
||
89 | // $preferenceItem->__set('title', $item->getProductName()); |
||
90 | // $preferenceItem->__set('quantity', $item->getQuantity()); |
||
91 | // $preferenceItem->__set('currency_id', $order->getCurrencyCode()); |
||
92 | // $preferenceItem->__set('unit_price', $item->getUnitPrice() / 100); |
||
93 | // |
||
94 | // /** @var ProductInterface $product */ |
||
95 | // $product = $item->getProduct(); |
||
96 | // |
||
97 | // if (!$product->getImagesByType('thumbnail')->isEmpty()) { |
||
98 | // /** @var ImageInterface $image */ |
||
99 | // $image = $product->getImagesByType('thumbnail')->first(); |
||
100 | // |
||
101 | // $path = $this->imagineCacheManager->getBrowserPath( |
||
102 | // (string)parse_url($image->getPath() ?: '', PHP_URL_PATH), |
||
103 | // 'sylius_shop_product_tiny_thumbnail' |
||
104 | // ); |
||
105 | // } elseif ($product->getImages()->first()) { |
||
106 | // /** @var ImageInterface $image */ |
||
107 | // $image = $product->getImages()->first(); |
||
108 | // |
||
109 | // $path = $this->imagineCacheManager->getBrowserPath( |
||
110 | // (string)parse_url($image->getPath() ?: '', PHP_URL_PATH), |
||
111 | // 'sylius_shop_product_tiny_thumbnail' |
||
112 | // ); |
||
113 | // } else { |
||
114 | // $path = '//placehold.it/64x64'; |
||
115 | // } |
||
116 | // |
||
117 | // $preferenceItem->__set('picture_url', $path); |
||
118 | // |
||
119 | // $preferenceItems[] = $preferenceItem; |
||
120 | // } |
||
121 | // |
||
122 | // if ($order->getShippingTotal() > 0) { |
||
123 | // $shipment = new Item(); |
||
124 | // |
||
125 | // $shipment->__set('id', 'shipping'); |
||
126 | // $shipment->__set('title', 'Shipping'); |
||
127 | // $shipment->__set('quantity', 1); |
||
128 | // $shipment->__set('currency_id', $order->getCurrencyCode()); |
||
129 | // $shipment->__set('unit_price', $order->getShippingTotal() / 100); |
||
130 | // |
||
131 | // $preferenceItems[] = $shipment; |
||
132 | // } |
||
133 | // |
||
134 | // if ($order->getAdjustmentsTotalRecursively('tax') > 0) { |
||
135 | // $tax = new Item(); |
||
136 | // |
||
137 | // $tax->__set('id', 'tax'); |
||
138 | // $tax->__set('title', 'Tax'); |
||
139 | // $tax->__set('quantity', 1); |
||
140 | // $tax->__set('currency_id', $order->getCurrencyCode()); |
||
141 | // $tax->__set('unit_price', $order->getAdjustmentsTotalRecursively('tax') / 100); |
||
142 | // |
||
143 | // $preferenceItems[] = $tax; |
||
144 | // } |
||
145 | |||
146 | $preferenceItem = new Item(); |
||
147 | |||
148 | $orderTotal = $order->getTotal() / 100; |
||
149 | |||
150 | $preferenceItem->__set('id', $order->getId()); |
||
151 | $preferenceItem->__set('title', 'TOTAL'); |
||
152 | $preferenceItem->__set('quantity', 1); |
||
153 | $preferenceItem->__set('currency_id', $order->getCurrencyCode()); |
||
154 | $preferenceItem->__set('unit_price', $orderTotal); |
||
155 | |||
156 | $preferenceItems[] = $preferenceItem; |
||
157 | |||
158 | $preference->__set('items', $preferenceItems); |
||
159 | |||
160 | /** Todo try to implement installments |
||
161 | $preferencePayment = new Payment(); |
||
162 | |||
163 | $channel = $order->getChannel(); |
||
164 | $minimumForInstalments = $channel->getMinimumOrderTotalForInstalments(); |
||
165 | if ($minimumForInstalments) { |
||
166 | if ($orderTotal < $minimumForInstalments) { |
||
167 | $preferencePayment->__set('installments', 1); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $preference->__set('payment_methods', $preferencePayment); |
||
172 | **/ |
||
173 | |||
174 | $payer = new Payer(); |
||
175 | |||
176 | $payerFirstName = $customer->getFirstName() !== null ? $customer->getFirstName() : $billingAddress->getFirstName(); |
||
177 | $payerLastName = $customer->getLastName() !== null ? $customer->getLastName() : $billingAddress->getLastName(); |
||
178 | $payerEmail = $customer->getEmail() !== null ? $customer->getEmail() : null; |
||
179 | $payerPhoneNumber = $customer->getPhoneNumber() !== null ? $customer->getPhoneNumber() : $billingAddress->getPhoneNumber(); |
||
180 | |||
181 | if ($payerFirstName !== null) { |
||
182 | $payer->__set('name', $payerFirstName); |
||
183 | } |
||
184 | if ($payerLastName !== null) { |
||
185 | $payer->__set('surname', $payerLastName); |
||
186 | } |
||
187 | if ($payerEmail !== null) { |
||
188 | $payer->__set('email', $payerEmail); |
||
189 | } |
||
190 | if ($payerPhoneNumber !== null) { |
||
191 | $payer->__set('phone', [ |
||
192 | 'number' => $payerPhoneNumber |
||
193 | ]); |
||
194 | } |
||
195 | |||
196 | $payer->__set('address', [ |
||
197 | 'street_name' => $billingAddress->getStreet(), |
||
198 | 'zip_code' => $billingAddress->getPostcode(), |
||
199 | ]); |
||
200 | |||
201 | $preference->__set('payer', $payer); |
||
202 | |||
203 | $preference->__set('external_reference', $order->getNumber()); |
||
204 | |||
205 | $preference->__set('back_urls', [ |
||
206 | 'success' => $request->getToken()->getAfterUrl(), |
||
207 | 'failure' => $request->getToken()->getAfterUrl(), |
||
208 | 'pending' => $request->getToken()->getAfterUrl() |
||
209 | ]); |
||
210 | |||
211 | // Create notification url with Notify Token |
||
212 | $notifyToken = $this->tokenFactory->createNotifyToken( |
||
213 | $request->getToken()->getGatewayName(), |
||
214 | $request->getToken()->getDetails() |
||
215 | ); |
||
216 | $preference->__set('notification_url', $notifyToken->getTargetUrl().'?source_news=webhooks'); |
||
217 | |||
218 | $preference->__set('auto_return', 'all'); |
||
219 | |||
220 | $status = 400; |
||
221 | $message = 'KO'; |
||
222 | $preferenceData = null; |
||
223 | |||
224 | if ($preference->save() === true) { |
||
225 | $status = 200; |
||
226 | $message = 'Preference created!'; |
||
227 | $preferenceData = $preference->toArray(); |
||
228 | } |
||
229 | |||
230 | $response = [ |
||
231 | 'status' => $status, |
||
232 | 'message' => $message, |
||
233 | 'preference' => $preferenceData |
||
234 | ]; |
||
235 | } catch (\Exception $exception) { |
||
236 | $response = [ |
||
237 | 'status' => $exception->getCode(), |
||
238 | 'message' => $exception->getMessage(), |
||
239 | 'preference' => null |
||
240 | ]; |
||
241 | } finally { |
||
242 | $payment->setDetails($response); |
||
|
|||
243 | } |
||
244 | |||
245 | if ($response['status'] === 200) { |
||
246 | $initPoint = $this->api->isSandbox() |
||
247 | ? $preference->__get('sandbox_init_point') |
||
248 | : $preference->__get('init_point') |
||
249 | ; |
||
250 | |||
251 | throw new HttpRedirect($initPoint); |
||
252 | } |
||
279 |