Total Complexity | 42 |
Total Lines | 376 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Complex classes like B2binpay 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 B2binpay, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class B2binpay extends PaymentModule |
||
44 | { |
||
45 | public $provider; |
||
46 | public $b2binpay_currency; |
||
47 | |||
48 | public function __construct() |
||
49 | { |
||
50 | $this->name = 'b2binpay'; |
||
51 | $this->tab = 'payments_gateways'; |
||
52 | $this->version = '1.0.0'; |
||
53 | $this->is_eu_compatible = 1; |
||
54 | $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); |
||
55 | $this->module_key = 'c1ae0a3ef16e14e71d3ded99c5faf216'; |
||
56 | $this->author = 'B2BinPay'; |
||
57 | $this->controllers = array('redirect', 'callback'); |
||
58 | |||
59 | $this->currencies = true; |
||
60 | $this->currencies_mode = 'checkbox'; |
||
61 | |||
62 | $this->bootstrap = true; |
||
63 | |||
64 | parent::__construct(); |
||
65 | |||
66 | $this->provider = new Provider( |
||
67 | Configuration::get('B2BINPAY_AUTH_KEY'), |
||
68 | Configuration::get('B2BINPAY_AUTH_SECRET'), |
||
69 | Configuration::get('B2BINPAY_TEST_MODE') |
||
70 | ); |
||
71 | |||
72 | $this->b2binpay_currency = new B2BinpayCurrency(); |
||
73 | |||
74 | $this->displayName = $this->l('B2BinPay Crypto Payment Gateway'); |
||
75 | $this->description = $this->l( |
||
76 | 'Accept Bitcoin, Bitcoin Cash, Litecoin, Ethereum, and other CryptoCurrencies via B2BinPay.' |
||
77 | ); |
||
78 | |||
79 | $this->warning = Configuration::get('B2BINPAY_WARNING'); |
||
80 | |||
81 | if (!count(Currency::checkPaymentCurrencies($this->id))) { |
||
82 | $this->warning = $this->l('No currency has been set for this module.'); |
||
83 | } |
||
84 | |||
85 | if (empty(Configuration::get('B2BINPAY_AUTH_KEY')) |
||
86 | || empty(Configuration::get('B2BINPAY_AUTH_SECRET')) |
||
87 | ) { |
||
88 | $this->warning = $this->l( |
||
89 | 'B2BinPay Account details must be configured in order to use this module correctly.' |
||
90 | ); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public function install() |
||
95 | { |
||
96 | if (!parent::install() |
||
97 | || !$this->registerHook('paymentOptions') |
||
98 | || !$this->registerHook('header') |
||
99 | || !$this->registerHook('backOfficeHeader') |
||
100 | ) { |
||
101 | return false; |
||
102 | } |
||
103 | |||
104 | return true; |
||
105 | } |
||
106 | |||
107 | public function uninstall() |
||
108 | { |
||
109 | return ( |
||
110 | Configuration::deleteByName('B2BINPAY_TITLE') && |
||
111 | Configuration::deleteByName('B2BINPAY_TEST_MODE') && |
||
112 | Configuration::deleteByName('B2BINPAY_AUTH_KEY') && |
||
113 | Configuration::deleteByName('B2BINPAY_AUTH_SECRET') && |
||
114 | Configuration::deleteByName('B2BINPAY_WALLETS') && |
||
115 | Configuration::deleteByName('B2BINPAY_MARKUP') && |
||
116 | Configuration::deleteByName('B2BINPAY_LIFETIME') && |
||
117 | Configuration::deleteByName('B2BINPAY_WARNING') && |
||
118 | parent::uninstall() |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Load the configuration form |
||
124 | */ |
||
125 | public function getContent() |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Create the form that will be displayed in the configuration of your module. |
||
150 | */ |
||
151 | protected function renderForm() |
||
152 | { |
||
153 | $helper = new HelperForm(); |
||
154 | |||
155 | $helper->show_toolbar = false; |
||
156 | $helper->table = $this->table; |
||
157 | $helper->module = $this; |
||
158 | $helper->default_form_language = $this->context->language->id; |
||
159 | $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); |
||
160 | |||
161 | $helper->identifier = $this->identifier; |
||
162 | $helper->submit_action = 'submitB2binpayModule'; |
||
163 | $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) |
||
164 | .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; |
||
165 | $helper->token = Tools::getAdminTokenLite('AdminModules'); |
||
166 | |||
167 | $helper->tpl_vars = array( |
||
168 | 'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */ |
||
169 | 'languages' => $this->context->controller->getLanguages(), |
||
170 | 'id_language' => $this->context->language->id, |
||
171 | ); |
||
172 | |||
173 | return $helper->generateForm(array($this->getConfigForm())); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Create the structure of your form. |
||
178 | */ |
||
179 | protected function getConfigForm() |
||
180 | { |
||
181 | return array( |
||
182 | 'form' => array( |
||
183 | 'legend' => array( |
||
184 | 'title' => $this->l('Settings'), |
||
185 | 'icon' => 'icon-cogs', |
||
186 | ), |
||
187 | 'input' => array( |
||
188 | array( |
||
189 | 'type' => 'b2binpay-warning', |
||
190 | 'name' => 'B2BINPAY_WARNING', |
||
191 | ), |
||
192 | array( |
||
193 | 'col' => 2, |
||
194 | 'type' => 'text', |
||
195 | 'name' => 'B2BINPAY_TITLE', |
||
196 | 'label' => $this->l('Title'), |
||
197 | 'desc' => $this->l('The payment method title which a customer sees at the checkout'), |
||
198 | 'required' => true, |
||
199 | ), |
||
200 | array( |
||
201 | 'type' => 'switch', |
||
202 | 'label' => $this->l('Test Mode (Sandbox)'), |
||
203 | 'name' => 'B2BINPAY_TEST_MODE', |
||
204 | 'is_bool' => true, |
||
205 | 'desc' => $this->l( |
||
206 | 'Use this module in test mode. Warning: Sandbox and main gateway has their own credentials!' |
||
207 | ), |
||
208 | 'required' => true, |
||
209 | 'values' => array( |
||
210 | array( |
||
211 | 'id' => 'active_on', |
||
212 | 'value' => true, |
||
213 | 'label' => $this->l('Enabled'), |
||
214 | ), |
||
215 | array( |
||
216 | 'id' => 'active_off', |
||
217 | 'value' => false, |
||
218 | 'label' => $this->l('Disabled'), |
||
219 | ), |
||
220 | ), |
||
221 | ), |
||
222 | array( |
||
223 | 'col' => 2, |
||
224 | 'type' => 'text', |
||
225 | 'name' => 'B2BINPAY_AUTH_KEY', |
||
226 | 'label' => $this->l('Auth Key'), |
||
227 | 'desc' => $this->l('B2BinPay API Auth Key'), |
||
228 | 'required' => true, |
||
229 | ), |
||
230 | array( |
||
231 | 'col' => 2, |
||
232 | 'type' => 'text', |
||
233 | 'name' => 'B2BINPAY_AUTH_SECRET', |
||
234 | 'label' => $this->l('Auth Secret'), |
||
235 | 'desc' => $this->l('B2BinPay API Auth Secret'), |
||
236 | 'required' => true, |
||
237 | ), |
||
238 | array( |
||
239 | 'type' => 'b2binpay-wallets', |
||
240 | 'name' => 'B2BINPAY_WALLETS', |
||
241 | 'label' => $this->l('Wallets'), |
||
242 | 'required' => true, |
||
243 | ), |
||
244 | array( |
||
245 | 'col' => 2, |
||
246 | 'type' => 'text', |
||
247 | 'name' => 'B2BINPAY_MARKUP', |
||
248 | 'label' => $this->l('Markup (%)'), |
||
249 | 'desc' => $this->l('Markup percentage for each payment'), |
||
250 | ), |
||
251 | array( |
||
252 | 'col' => 2, |
||
253 | 'type' => 'text', |
||
254 | 'name' => 'B2BINPAY_LIFETIME', |
||
255 | 'label' => $this->l('Order lifetime (seconds)'), |
||
256 | 'desc' => $this->l('Lifetime for your orders in seconds'), |
||
257 | 'required' => true, |
||
258 | ), |
||
259 | ), |
||
260 | 'submit' => array( |
||
261 | 'title' => $this->l('Save'), |
||
262 | ), |
||
263 | ), |
||
264 | ); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Set values for the inputs. |
||
269 | */ |
||
270 | protected function getConfigFormValues() |
||
271 | { |
||
272 | return array( |
||
273 | 'B2BINPAY_TITLE' => Configuration::get('B2BINPAY_TITLE'), |
||
274 | 'B2BINPAY_TEST_MODE' => Configuration::get('B2BINPAY_TEST_MODE'), |
||
275 | 'B2BINPAY_AUTH_KEY' => Configuration::get('B2BINPAY_AUTH_KEY'), |
||
276 | 'B2BINPAY_AUTH_SECRET' => Configuration::get('B2BINPAY_AUTH_SECRET'), |
||
277 | 'B2BINPAY_WALLETS' => Configuration::get('B2BINPAY_WALLETS'), |
||
278 | 'B2BINPAY_MARKUP' => Configuration::get('B2BINPAY_MARKUP'), |
||
279 | 'B2BINPAY_LIFETIME' => Configuration::get('B2BINPAY_LIFETIME'), |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Save form data. |
||
285 | */ |
||
286 | protected function postProcess() |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Check B2BinPay authorization. |
||
306 | * @param $key |
||
307 | * @param $secret |
||
308 | * @param $test |
||
309 | * @param $wallets |
||
310 | * @return string|null |
||
311 | */ |
||
312 | protected function checkConfig($key, $secret, $test, $wallets) |
||
313 | { |
||
314 | if (empty($key) || empty($secret)) { |
||
315 | return $this->l('You need to enter B2BinPay Auth Key/Secret.'); |
||
316 | } |
||
317 | |||
318 | $this->provider = new Provider($key, $secret, $test); |
||
319 | |||
320 | try { |
||
321 | $this->provider->getAuthToken(); |
||
322 | } catch (ServerApiException $e) { |
||
323 | return $this->l('Wrong B2BinPay Auth Key/Secret.'); |
||
324 | } |
||
325 | |||
326 | if (empty(json_decode($wallets))) { |
||
327 | return $this->l('You need to enter B2BinPay Wallet(s).'); |
||
328 | } |
||
329 | |||
330 | $wallets_retrieved = $this->retrieveWallets($wallets); |
||
331 | Configuration::updateValue('B2BINPAY_WALLETS', json_encode($wallets_retrieved)); |
||
332 | |||
333 | if (empty($wallets_retrieved)) { |
||
334 | return $this->l('You entered wrong B2BinPay Wallet(s).'); |
||
335 | } |
||
336 | |||
337 | return null; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param string $wallets_json |
||
342 | * @return array |
||
343 | */ |
||
344 | protected function retrieveWallets($wallets_json) |
||
345 | { |
||
346 | $wallets = json_decode($wallets_json); |
||
347 | $wallets_updated = array(); |
||
348 | |||
349 | foreach ($wallets as $wallet) { |
||
350 | if (empty($wallet->id)) { |
||
351 | continue; |
||
352 | } |
||
353 | |||
354 | try { |
||
355 | $b2binpay_wallet = $this->provider->getWallet((int)$wallet->id); |
||
356 | } catch (ServerApiException $e) { |
||
357 | continue; |
||
358 | } |
||
359 | |||
360 | $currency = (empty($wallet->currency)) |
||
361 | ? $this->b2binpay_currency->getName($b2binpay_wallet->currency->iso) : $wallet->currency; |
||
362 | |||
363 | array_push( |
||
364 | $wallets_updated, |
||
365 | array( |
||
366 | 'id' => $wallet->id, |
||
367 | 'currency' => $currency, |
||
368 | 'alpha' => $b2binpay_wallet->currency->alpha, |
||
369 | 'iso' => $b2binpay_wallet->currency->iso, |
||
370 | ) |
||
371 | ); |
||
372 | } |
||
373 | |||
374 | return $wallets_updated; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Add the CSS & JavaScript files you want to be loaded in the BO. |
||
379 | */ |
||
380 | public function hookBackOfficeHeader() |
||
381 | { |
||
382 | $this->context->controller->addJS($this->_path.'views/js/back.js'); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Add the CSS & JavaScript files you want to be added on the FO. |
||
387 | */ |
||
388 | public function hookHeader() |
||
389 | { |
||
390 | $this->context->controller->addCSS($this->_path.'/views/css/front.css'); |
||
391 | } |
||
392 | |||
393 | public function hookPaymentOptions($params) |
||
394 | { |
||
395 | if (!$this->active || !empty($this->warning)) { |
||
396 | return; |
||
397 | } |
||
398 | |||
399 | $paymentOption = new PaymentOption(); |
||
400 | $paymentOption->setCallToActionText(Configuration::get('B2BINPAY_TITLE')) |
||
401 | ->setForm($this->generateForm()) |
||
402 | ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/logo.png')); |
||
403 | |||
404 | $payment_options = array($paymentOption); |
||
405 | |||
406 | return $payment_options; |
||
407 | } |
||
408 | |||
409 | protected function generateForm() |
||
419 | } |
||
420 | } |
||
421 |