PaymentMethodAvailable::execute()   F
last analyzed

Complexity

Conditions 24
Paths 4347

Size

Total Lines 52
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 24
eloc 45
nc 4347
nop 1
dl 0
loc 52
rs 0
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Pagantis\Pagantis\Observer;
4
5
use Magento\Framework\Event\ObserverInterface;
6
use Pagantis\Pagantis\Model\Ui\ConfigProvider;
7
8
/**
9
 * Class PaymentMethodAvailable
10
 * @package Pagantis\Pagantis\Observer
11
 */
12
class PaymentMethodAvailable implements ObserverInterface
13
{
14
    /**
15
     * @param \Magento\Framework\Event\Observer $observer
16
     */
17
    public function execute(\Magento\Framework\Event\Observer $observer)
18
    {
19
        try {
20
            if ($observer->getEvent()->getMethodInstance()->getCode()==ConfigProvider::CODE) {
21
                $checkResult = $observer->getEvent()->getResult();
22
                $totalPrice  = (string) floor($observer->getEvent()->getQuote()->getGrandTotal());
23
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
24
                $config        = $objectManager->create('Pagantis\Pagantis\Helper\Config')->getConfig();
25
                $extraConfig   = $objectManager->create('Pagantis\Pagantis\Helper\ExtraConfig')->getExtraConfig();
26
                $resolver      = $objectManager->create('Magento\Framework\Locale\Resolver');
27
                $locale = strstr($resolver->getLocale(), '_', true);
28
                $allowedCountries = unserialize($extraConfig['PAGANTIS_ALLOWED_COUNTRIES']);
29
                $availableCountry = (in_array(strtolower($locale), $allowedCountries));
30
                $maxAmount = $extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT'];
31
                $minAmount = $extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'];
32
                $validAmount = ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0'));
33
                $disabledPg = (!isset($config['pagantis_public_key']) || $config['pagantis_public_key'] == '' ||
34
                               !isset($config['pagantis_private_key']) || $config['pagantis_private_key'] == '' ||
35
                               !$availableCountry || !$validAmount || $config['active_12x']!=='1'
36
                               || $config['active']!=='1' );
37
                if ($disabledPg) {
38
                    $checkResult->setData('is_available', false);
39
                } else {
40
                    $checkResult->setData('is_available', true);
41
                }
42
            }
43
44
            if ($observer->getEvent()->getMethodInstance()->getCode()==ConfigProvider::CODE4X) {
45
                $checkResult = $observer->getEvent()->getResult();
46
                $totalPrice  = (string) floor($observer->getEvent()->getQuote()->getGrandTotal());
47
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
48
                $config        = $objectManager->create('Pagantis\Pagantis\Helper\Config')->getConfig();
49
                $extraConfig   = $objectManager->create('Pagantis\Pagantis\Helper\ExtraConfig')->getExtraConfig();
50
                $resolver      = $objectManager->create('Magento\Framework\Locale\Resolver');
51
                $locale = strstr($resolver->getLocale(), '_', true);
52
                $allowedCountries = unserialize($extraConfig['PAGANTIS_ALLOWED_COUNTRIES']);
53
                $availableCountry = (in_array(strtolower($locale), $allowedCountries));
54
                $maxAmount = $extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT_4x'];
55
                $minAmount = $extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT_4x'];
56
                $validAmount = ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0'));
57
                $disabledPg4x = (!isset($config['pagantis_public_key_4x']) || $config['pagantis_public_key_4x']=='' ||
58
                                 !isset($config['pagantis_private_key_4x']) || $config['pagantis_private_key_4x']=='' ||
59
                                 !$availableCountry || !$validAmount || $config['active_4x']!=='1'
60
                                 || $config['active']!=='1' );
61
                if ($disabledPg4x) {
62
                    $checkResult->setData('is_available', false);
63
                } else {
64
                    $checkResult->setData('is_available', true);
65
                }
66
            }
67
        } catch (\Exception $e) {
68
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by pgarces
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
69
        }
70
    }
71
}
72