Total Complexity | 45 |
Total Lines | 369 |
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 |
||
28 | class PaylaterNotifyModuleFrontController extends AbstractController |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var string $merchantOrderId |
||
33 | */ |
||
34 | protected $merchantOrderId; |
||
35 | |||
36 | /** |
||
37 | * @var \Cart $merchantOrder |
||
|
|||
38 | */ |
||
39 | protected $merchantOrder; |
||
40 | |||
41 | /** |
||
42 | * @var string $pmtOrderId |
||
43 | */ |
||
44 | protected $pmtOrderId; |
||
45 | |||
46 | /** |
||
47 | * @var \PagaMasTarde\OrdersApiClient\Model\Order $pmtOrder |
||
48 | */ |
||
49 | protected $pmtOrder; |
||
50 | |||
51 | /** |
||
52 | * @var PagaMasTarde\OrdersApiClient\Client $orderClient |
||
53 | */ |
||
54 | protected $orderClient; |
||
55 | |||
56 | /** |
||
57 | * @var mixed $config |
||
58 | */ |
||
59 | protected $config; |
||
60 | |||
61 | /** |
||
62 | * @var Object $jsonResponse |
||
63 | */ |
||
64 | protected $jsonResponse; |
||
65 | |||
66 | /** |
||
67 | * @throws Exception |
||
68 | */ |
||
69 | public function postProcess() |
||
70 | { |
||
71 | $response = null; |
||
72 | try { |
||
73 | $this->checkConcurrency(); |
||
74 | $this->getMerchantOrder(); |
||
75 | $this->getPmtOrderId(); |
||
76 | $this->getPmtOrder(); |
||
77 | $this->checkOrderStatus(); |
||
78 | $this->checkMerchantOrderStatus(); |
||
79 | $this->validateAmount(); |
||
80 | $this->processMerchantOrder(); |
||
81 | } catch (\Exception $exception) { |
||
82 | $this->jsonResponse = new JsonExceptionResponse(); |
||
83 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
84 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
85 | $this->jsonResponse->setException($exception); |
||
86 | $response = $this->jsonResponse->toJson(); |
||
87 | return $this->cancelProcess(); |
||
88 | } |
||
89 | |||
90 | try { |
||
91 | if (!isset($response)) { |
||
92 | $this->jsonResponse = new JsonSuccessResponse(); |
||
93 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
94 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
95 | $this->confirmPmtOrder(); |
||
96 | } |
||
97 | } catch (\Exception $exception) { |
||
98 | $this->rollbackMerchantOrder(); |
||
99 | $this->jsonResponse = new JsonExceptionResponse(); |
||
100 | $this->jsonResponse->setMerchantOrderId($this->merchantOrderId); |
||
101 | $this->jsonResponse->setPmtOrderId($this->pmtOrderId); |
||
102 | $this->jsonResponse->setException($exception); |
||
103 | $this->jsonResponse->toJson(); |
||
104 | return $this->cancelProcess(); |
||
105 | } |
||
106 | |||
107 | try { |
||
108 | $this->unblockConcurrency(); |
||
109 | } catch (\Exception $exception) { |
||
110 | // Do nothing |
||
111 | } |
||
112 | |||
113 | return $this->finishProcess(false); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Find and init variables needed to process payment |
||
118 | * |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | public function prepareVariables() |
||
122 | { |
||
123 | $callbackOkUrl = $this->context->link->getPageLink( |
||
124 | 'order-confirmation', |
||
125 | null, |
||
126 | null |
||
127 | ); |
||
128 | $callbackKoUrl = $this->context->link->getPageLink( |
||
129 | 'order', |
||
130 | null, |
||
131 | null, |
||
132 | array('step'=>3) |
||
133 | ); |
||
134 | try { |
||
135 | $this->config = array( |
||
136 | 'urlOK' => (getenv('PMT_URL_OK') !== '') ? getenv('PMT_URL_OK') : $callbackOkUrl, |
||
137 | 'urlKO' => (getenv('PMT_URL_KO') !== '') ? getenv('PMT_URL_KO') : $callbackKoUrl, |
||
138 | 'publicKey' => Configuration::get('pmt_public_key'), |
||
139 | 'privateKey' => Configuration::get('pmt_private_key'), |
||
140 | 'secureKey' => Tools::getValue('key'), |
||
141 | ); |
||
142 | } catch (\Exception $exception) { |
||
143 | throw new UnknownException(self::CC_NO_CONFIG); |
||
144 | } |
||
145 | |||
146 | $this->merchantOrderId = Tools::getValue('id_cart'); |
||
147 | if ($this->merchantOrderId == '') { |
||
148 | throw new NoQuoteFoundException(); |
||
149 | } |
||
150 | |||
151 | |||
152 | if (!($this->config['secureKey'] && $this->merchantOrderId && Module::isEnabled(self::PMT_CODE))) { |
||
153 | throw new UnknownException(self::CC_MALFORMED); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Check the concurrency of the purchase |
||
159 | * |
||
160 | * @throws Exception |
||
161 | */ |
||
162 | public function checkConcurrency() |
||
163 | { |
||
164 | $this->prepareVariables(); |
||
165 | $this->unblockConcurrency(); |
||
166 | $this->blockConcurrency($this->merchantOrderId); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Retrieve the merchant order by id |
||
171 | * |
||
172 | * @throws Exception |
||
173 | */ |
||
174 | public function getMerchantOrder() |
||
175 | { |
||
176 | try { |
||
177 | $this->merchantOrder = new Cart($this->merchantOrderId); |
||
178 | if (!Validate::isLoadedObject($this->merchantOrder)) { |
||
179 | throw new \Exception(self::GMO_CART_NOT_LOADED, 500); |
||
180 | } |
||
181 | } catch (\Exception $exception) { |
||
182 | throw new MerchantOrderNotFoundException(); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Find PMT Order Id in AbstractController::PMT_ORDERS_TABLE |
||
188 | * |
||
189 | * @throws Exception |
||
190 | */ |
||
191 | private function getPmtOrderId() |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Find PMT Order in Orders Server using PagaMasTarde\OrdersApiClient |
||
208 | * |
||
209 | * @throws Exception |
||
210 | */ |
||
211 | private function getPmtOrder() |
||
212 | { |
||
213 | $this->orderClient = new PmtClient($this->config['publicKey'], $this->config['privateKey']); |
||
214 | $this->pmtOrder = $this->orderClient->getOrder($this->pmtOrderId); |
||
215 | if (!($this->pmtOrder instanceof PmtModelOrder)) { |
||
216 | throw new NoOrderFoundException(); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Compare statuses of merchant order and PMT order, witch have to be the same. |
||
222 | * |
||
223 | * @throws Exception |
||
224 | */ |
||
225 | public function checkOrderStatus() |
||
226 | { |
||
227 | if ($this->pmtOrder->getStatus() !== PmtModelOrder::STATUS_AUTHORIZED) { |
||
228 | $status = '-'; |
||
229 | if ($this->pmtOrder instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
||
230 | $status = $this->pmtOrder->getStatus(); |
||
231 | } |
||
232 | throw new WrongStatusException($status); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Check that the merchant order was not previously processes and is ready to be paid |
||
238 | * |
||
239 | * @throws Exception |
||
240 | */ |
||
241 | public function checkMerchantOrderStatus() |
||
242 | { |
||
243 | if ($this->merchantOrder->orderExists() !== false) { |
||
244 | throw new WrongStatusException('already_processed'); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Check that the merchant order and the order in PMT have the same amount to prevent hacking |
||
250 | * |
||
251 | * @throws Exception |
||
252 | */ |
||
253 | public function validateAmount() |
||
254 | { |
||
255 | $totalAmount = $this->pmtOrder->getShoppingCart()->getTotalAmount(); |
||
256 | $merchantAmount = (int)((string) (100 * $this->merchantOrder->getOrderTotal(true))); |
||
257 | if ($totalAmount != $merchantAmount) { |
||
258 | throw new AmountMismatchException($totalAmount, $merchantAmount); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Process the merchant order and notify client |
||
264 | * |
||
265 | * @throws Exception |
||
266 | */ |
||
267 | public function processMerchantOrder() |
||
268 | { |
||
269 | try { |
||
270 | $this->module->validateOrder( |
||
271 | $this->merchantOrderId, |
||
272 | Configuration::get('PS_OS_PAYMENT'), |
||
273 | $this->merchantOrder->getOrderTotal(true), |
||
274 | $this->module->displayName, |
||
275 | 'pmtOrderId: ' . $this->pmtOrder->getId(), |
||
276 | array('transaction_id' => $this->pmtOrderId), |
||
277 | null, |
||
278 | false, |
||
279 | $this->config['secureKey'] |
||
280 | ); |
||
281 | } catch (\Exception $exception) { |
||
282 | throw new UnknownException($exception->getMessage()); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Confirm the order in PMT |
||
288 | * |
||
289 | * @throws Exception |
||
290 | */ |
||
291 | private function confirmPmtOrder() |
||
292 | { |
||
293 | try { |
||
294 | $this->orderClient->confirmOrder($this->pmtOrderId); |
||
295 | } catch (\Exception $exception) { |
||
296 | throw new UnknownException($exception->getMessage()); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Leave the merchant order as it was previously |
||
302 | * |
||
303 | * @throws Exception |
||
304 | */ |
||
305 | public function rollbackMerchantOrder() |
||
306 | { |
||
307 | // Do nothing because the order is created only when the purchase was successfully |
||
308 | } |
||
309 | |||
310 | |||
311 | /** |
||
312 | * Lock the concurrency to prevent duplicated inputs |
||
313 | * |
||
314 | * @param $orderId |
||
315 | * @throws Exception |
||
316 | */ |
||
317 | protected function blockConcurrency($orderId) |
||
318 | { |
||
319 | try { |
||
320 | Db::getInstance()->insert('pmt_cart_process', array('id' => $orderId, 'timestamp' => (time()))); |
||
321 | } catch (\Exception $exception) { |
||
322 | throw new ConcurrencyException(); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Unlock the concurrency |
||
328 | * |
||
329 | * @throws Exception |
||
330 | */ |
||
331 | protected function unblockConcurrency() |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Do all the necessary actions to cancel the confirmation process in case of error |
||
342 | * 1. Unblock concurrency |
||
343 | * 2. Save log |
||
344 | * |
||
345 | */ |
||
346 | public function cancelProcess() |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Redirect the request to the e-commerce or show the output in json |
||
380 | * |
||
381 | * @param bool $error |
||
382 | */ |
||
383 | public function finishProcess($error = true) |
||
397 | } |
||
398 | } |
||
399 |
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