Passed
Push — master ( c01df0...b2280f )
by Cesar
10:38 queued 06:42
created

ConfigV2::execute()   C

Complexity

Conditions 11
Paths 264

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 49
rs 5.6833
c 0
b 0
f 0
cc 11
nc 264
nop 0

How to fix   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
namespace Pagantis\Pagantis\Controller\Payment;
3
4
use Magento\Framework\App\Action\Action;
5
use Magento\Framework\App\ResourceConnection;
6
use Magento\Framework\App\CsrfAwareActionInterface;
7
use Magento\Framework\App\RequestInterface;
8
use Magento\Framework\App\Request\InvalidRequestException;
9
10
class ConfigV2 extends Action implements CsrfAwareActionInterface
11
{
12
    /** Config tablename */
13
    const CONFIG_TABLE = 'Pagantis_config';
14
15
    /** @var ResourceConnection $dbObject */
16
    protected $dbObject;
17
18
    /**
19
     * Variable which contains extra configuration.
20
     * @var array $defaultConfigs
21
     */
22
    public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing',
23
                                   'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE',
24
                                   'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE',
25
                                   'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
26
                                   'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
27
                                   'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
28
                                   'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
29
                                   'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER',
30
                                   'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default',
31
                                   'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default',
32
                                   'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
33
                                   'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
34
                                   'PAGANTIS_URL_OK'=>'',
35
                                   'PAGANTIS_URL_KO'=>'',
36
                                   'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!'
37
    );
38
39
    /**
40
     * Log constructor.
41
     *
42
     * @param \Magento\Framework\App\Action\Context $context
43
     * @param \Pagantis\Pagantis\Helper\Config      $pagantisConfig
44
     * @param ResourceConnection                    $dbObject
45
     */
46
    public function __construct(
47
        \Magento\Framework\App\Action\Context $context,
48
        \Pagantis\Pagantis\Helper\Config $pagantisConfig,
49
        ResourceConnection $dbObject
50
    ) {
51
        $this->config = $pagantisConfig->getConfig();
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
        $this->dbObject = $dbObject;
53
54
        // CsrfAwareAction Magento2.3 compatibility
55
        if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) {
56
            $request = $this->getRequest();
57
            if ($request instanceof HttpRequest && $request->isPost() && empty($request->getParam('form_key'))) {
0 ignored issues
show
Bug introduced by
The type Pagantis\Pagantis\Controller\Payment\HttpRequest was not found. Did you mean HttpRequest? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
The method isPost() does not exist on Magento\Framework\App\RequestInterface. It seems like you code against a sub-type of Magento\Framework\App\RequestInterface such as Magento\Framework\Webapi\Request or Magento\Framework\App\Request\Http. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
            if ($request instanceof HttpRequest && $request->/** @scrutinizer ignore-call */ isPost() && empty($request->getParam('form_key'))) {
Loading history...
58
                $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
59
                $request->setParam('form_key', $formKey->getFormKey());
0 ignored issues
show
Bug introduced by
The method setParam() does not exist on Magento\Framework\App\RequestInterface. Did you maybe mean setParams()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                $request->/** @scrutinizer ignore-call */ 
60
                          setParam('form_key', $formKey->getFormKey());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            }
61
        }
62
63
        return parent::__construct($context);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::__construct($context) targeting Magento\Framework\App\Action\Action::__construct() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
    }
65
66
    /**
67
     * Main function
68
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
69
     */
70
    public function execute()
71
    {
72
        try {
73
            $response = array('status'=>null);
74
            $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE);
75
            $secretKey = $this->getRequest()->getParam('secret');
76
            $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null;
77
78
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
79
            $dbConnection = $this->dbObject->getConnection();
80
            if ($privateKey != $secretKey) {
81
                $response['status'] = 401;
82
                $response['result'] = 'Unauthorized';
83
            } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
84
                if (count($_POST)) {
85
                    foreach ($_POST as $config => $value) {
86
                        if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
87
                            $dbConnection->update(
88
                                $tableName,
89
                                array('value' => $value),
90
                                "config='$config'"
91
                            );
92
                        } else {
93
                            $response['status'] = 400;
94
                            $response['result'] = 'Bad request';
95
                        }
96
                    }
97
                } else {
98
                    $response['status'] = 422;
99
                    $response['result'] = 'Empty data';
100
                }
101
            }
102
103
            $formattedResult = array();
104
            if ($response['status']==null) {
105
                $dbResult = $dbConnection->fetchAll("select * from $tableName");
106
                foreach ($dbResult as $value) {
107
                    $formattedResult[$value['config']] = $value['value'];
108
                }
109
                $response['result'] = $formattedResult;
110
            }
111
            $result = json_encode($response['result']);
112
            header("HTTP/1.1 ".$response['status'], true, $response['status']);
113
            header('Content-Type: application/json', true);
114
            header('Content-Length: '.strlen($result));
115
            echo($result);
116
            exit();
0 ignored issues
show
Best Practice introduced by
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...
117
        } catch (\Exception $e) {
118
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
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...
119
        }
120
    }
121
122
    /**
123
     * @param RequestInterface $request
124
     *
125
     * @return InvalidRequestException|null
126
     */
127
    public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
128
    {
129
        return null;
130
    }
131
132
    /**
133
     * @param RequestInterface $request
134
     *
135
     * @return bool|null
136
     */
137
    public function validateForCsrf(RequestInterface $request): ?bool
138
    {
139
        return true;
140
    }
141
}
142