Total Complexity | 45 |
Total Lines | 373 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 0 | Features | 3 |
Complex classes like PaylaterNotifyModuleFrontController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PaylaterNotifyModuleFrontController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class PaylaterNotifyModuleFrontController extends AbstractController |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var string $merchantOrderId |
||
34 | */ |
||
35 | protected $merchantOrderId; |
||
36 | |||
37 | /** |
||
38 | * @var \Cart $merchantOrder |
||
|
|||
39 | */ |
||
40 | protected $merchantOrder; |
||
41 | |||
42 | /** |
||
43 | * @var string $pmtOrderId |
||
44 | */ |
||
45 | protected $pmtOrderId; |
||
46 | |||
47 | /** |
||
48 | * @var \PagaMasTarde\OrdersApiClient\Model\Order $pmtOrder |
||
49 | */ |
||
50 | protected $pmtOrder; |
||
51 | |||
52 | /** |
||
53 | * @var PagaMasTarde\OrdersApiClient\Client $orderClient |
||
54 | */ |
||
55 | protected $orderClient; |
||
56 | |||
57 | /** |
||
58 | * @var mixed $config |
||
59 | */ |
||
60 | protected $config; |
||
61 | |||
62 | /** |
||
63 | * @var Object $jsonResponse |
||
64 | */ |
||
65 | protected $jsonResponse; |
||
66 | |||
67 | /** |
||
68 | * @throws Exception |
||
69 | */ |
||
70 | public function postProcess() |
||
71 | { |
||
72 | $response = null; |
||
73 | try { |
||
74 | $this->checkConcurrency(); |
||
75 | $this->getMerchantOrder(); |
||
76 | $this->getPmtOrderId(); |
||
77 | $this->getPmtOrder(); |
||
78 | $this->checkOrderStatus(); |
||
79 | $this->checkMerchantOrderStatus(); |
||
80 | $this->validateAmount(); |
||
81 | $this->processMerchantOrder(); |
||
82 | } catch (\Exception $exception) { |
||
83 | $this->jsonResponse = new JsonExceptionResponse(); |
||
84 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
85 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
86 | $this->jsonResponse->setException($exception); |
||
87 | $response = $this->jsonResponse->toJson(); |
||
88 | return $this->cancelProcess(); |
||
89 | } |
||
90 | |||
91 | try { |
||
92 | if (!isset($response)) { |
||
93 | $this->jsonResponse = new JsonSuccessResponse(); |
||
94 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
95 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
96 | $this->confirmPmtOrder(); |
||
97 | } |
||
98 | } catch (\Exception $exception) { |
||
99 | $this->rollbackMerchantOrder(); |
||
100 | $this->jsonResponse = new JsonExceptionResponse(); |
||
101 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
102 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
103 | $this->jsonResponse->setException($exception); |
||
104 | $this->jsonResponse->toJson(); |
||
105 | return $this->cancelProcess(); |
||
106 | } |
||
107 | |||
108 | try { |
||
109 | $this->unblockConcurrency(); |
||
110 | } catch (\Exception $exception) { |
||
111 | // Do nothing |
||
112 | } |
||
113 | |||
114 | return $this->finishProcess(false); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Find and init variables needed to process payment |
||
119 | * |
||
120 | * @throws Exception |
||
121 | */ |
||
122 | public function prepareVariables() |
||
123 | { |
||
124 | $callbackOkUrl = $this->context->link->getPageLink( |
||
125 | 'order-confirmation', |
||
126 | null, |
||
127 | null |
||
128 | ); |
||
129 | $callbackKoUrl = $this->context->link->getPageLink( |
||
130 | 'order', |
||
131 | null, |
||
132 | null, |
||
133 | array('step'=>3) |
||
134 | ); |
||
135 | try { |
||
136 | $this->config = array( |
||
137 | 'urlOK' => (getenv('PMT_URL_OK') !== '') ? getenv('PMT_URL_OK') : $callbackOkUrl, |
||
138 | 'urlKO' => (getenv('PMT_URL_KO') !== '') ? getenv('PMT_URL_KO') : $callbackKoUrl, |
||
139 | 'publicKey' => Configuration::get('pmt_public_key'), |
||
140 | 'privateKey' => Configuration::get('pmt_private_key'), |
||
141 | 'secureKey' => Tools::getValue('key'), |
||
142 | ); |
||
143 | } catch (\Exception $exception) { |
||
144 | throw new ConfigurationNotFoundException(); |
||
145 | } |
||
146 | |||
147 | $this->merchantOrderId = Tools::getValue('id_cart'); |
||
148 | if ($this->merchantOrderId == '') { |
||
149 | throw new QuoteNotFoundException(); |
||
150 | } |
||
151 | |||
152 | |||
153 | if (!($this->config['secureKey'] && $this->merchantOrderId && Module::isEnabled(self::PMT_CODE))) { |
||
154 | // This exception is only for Prestashop |
||
155 | throw new UnknownException('Module may not be enabled'); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Check the concurrency of the purchase |
||
161 | * |
||
162 | * @throws Exception |
||
163 | */ |
||
164 | public function checkConcurrency() |
||
165 | { |
||
166 | $this->prepareVariables(); |
||
167 | $this->unblockConcurrency(); |
||
168 | $this->blockConcurrency($this->merchantOrderId); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Retrieve the merchant order by id |
||
173 | * |
||
174 | * @throws Exception |
||
175 | */ |
||
176 | public function getMerchantOrder() |
||
177 | { |
||
178 | try { |
||
179 | $this->merchantOrder = new Cart($this->merchantOrderId); |
||
180 | if (!Validate::isLoadedObject($this->merchantOrder)) { |
||
181 | // This exception is only for Prestashop |
||
182 | throw new UnknownException('Unable to load cart'); |
||
183 | } |
||
184 | } catch (\Exception $exception) { |
||
185 | throw new MerchantOrderNotFoundException(); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Find PMT Order Id in AbstractController::PMT_ORDERS_TABLE |
||
191 | * |
||
192 | * @throws Exception |
||
193 | */ |
||
194 | private function getPmtOrderId() |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Find PMT Order in Orders Server using PagaMasTarde\OrdersApiClient |
||
211 | * |
||
212 | * @throws Exception |
||
213 | */ |
||
214 | private function getPmtOrder() |
||
215 | { |
||
216 | $this->orderClient = new PmtClient($this->config['publicKey'], $this->config['privateKey']); |
||
217 | $this->pmtOrder = $this->orderClient->getOrder($this->pmtOrderId); |
||
218 | if (!($this->pmtOrder instanceof PmtModelOrder)) { |
||
219 | throw new OrderNotFoundException(); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Compare statuses of merchant order and PMT order, witch have to be the same. |
||
225 | * |
||
226 | * @throws Exception |
||
227 | */ |
||
228 | public function checkOrderStatus() |
||
229 | { |
||
230 | if ($this->pmtOrder->getStatus() !== PmtModelOrder::STATUS_AUTHORIZED) { |
||
231 | $status = '-'; |
||
232 | if ($this->pmtOrder instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
||
233 | $status = $this->pmtOrder->getStatus(); |
||
234 | } |
||
235 | throw new WrongStatusException($status); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Check that the merchant order was not previously processes and is ready to be paid |
||
241 | * |
||
242 | * @throws Exception |
||
243 | */ |
||
244 | public function checkMerchantOrderStatus() |
||
245 | { |
||
246 | if ($this->merchantOrder->orderExists() !== false) { |
||
247 | throw new WrongStatusException('already_processed'); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Check that the merchant order and the order in PMT have the same amount to prevent hacking |
||
253 | * |
||
254 | * @throws Exception |
||
255 | */ |
||
256 | public function validateAmount() |
||
257 | { |
||
258 | $totalAmount = $this->pmtOrder->getShoppingCart()->getTotalAmount(); |
||
259 | $merchantAmount = (int)((string) (100 * $this->merchantOrder->getOrderTotal(true))); |
||
260 | if ($totalAmount != $merchantAmount) { |
||
261 | throw new AmountMismatchException($totalAmount, $merchantAmount); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Process the merchant order and notify client |
||
267 | * |
||
268 | * @throws Exception |
||
269 | */ |
||
270 | public function processMerchantOrder() |
||
271 | { |
||
272 | try { |
||
273 | $this->module->validateOrder( |
||
274 | $this->merchantOrderId, |
||
275 | Configuration::get('PS_OS_PAYMENT'), |
||
276 | $this->merchantOrder->getOrderTotal(true), |
||
277 | $this->module->displayName, |
||
278 | 'pmtOrderId: ' . $this->pmtOrder->getId(), |
||
279 | array('transaction_id' => $this->pmtOrderId), |
||
280 | null, |
||
281 | false, |
||
282 | $this->config['secureKey'] |
||
283 | ); |
||
284 | } catch (\Exception $exception) { |
||
285 | throw new UnknownException($exception->getMessage()); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Confirm the order in PMT |
||
291 | * |
||
292 | * @throws Exception |
||
293 | */ |
||
294 | private function confirmPmtOrder() |
||
295 | { |
||
296 | try { |
||
297 | $this->orderClient->confirmOrder($this->pmtOrderId); |
||
298 | } catch (\Exception $exception) { |
||
299 | throw new UnknownException($exception->getMessage()); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Leave the merchant order as it was previously |
||
305 | * |
||
306 | * @throws Exception |
||
307 | */ |
||
308 | public function rollbackMerchantOrder() |
||
309 | { |
||
310 | // Do nothing because the order is created only when the purchase was successfully |
||
311 | } |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Lock the concurrency to prevent duplicated inputs |
||
316 | * |
||
317 | * @param $orderId |
||
318 | * @throws Exception |
||
319 | */ |
||
320 | protected function blockConcurrency($orderId) |
||
321 | { |
||
322 | try { |
||
323 | Db::getInstance()->insert('pmt_cart_process', array('id' => $orderId, 'timestamp' => (time()))); |
||
324 | } catch (\Exception $exception) { |
||
325 | throw new ConcurrencyException(); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Unlock the concurrency |
||
331 | * |
||
332 | * @throws Exception |
||
333 | */ |
||
334 | protected function unblockConcurrency() |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Do all the necessary actions to cancel the confirmation process in case of error |
||
345 | * 1. Unblock concurrency |
||
346 | * 2. Save log |
||
347 | * |
||
348 | */ |
||
349 | public function cancelProcess() |
||
350 | { |
||
351 | if ($this->merchantOrder) { |
||
352 | $id = (!is_null($this->pmtOrder))?$this->pmtOrder->getId():null; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Redirect the request to the e-commerce or show the output in json |
||
385 | * |
||
386 | * @param bool $error |
||
387 | */ |
||
388 | public function finishProcess($error = true) |
||
404 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths