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

Config::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\DB\Ddl\Table;
7
use Magento\Framework\App\CsrfAwareActionInterface;
8
use Magento\Framework\App\RequestInterface;
9
use Magento\Framework\App\Request\InvalidRequestException;
10
11
class Config extends Action
12
{
13
    /** Config tablename */
14
    const CONFIG_TABLE = 'Pagantis_config';
15
16
    /** @var ResourceConnection $dbObject */
17
    protected $dbObject;
18
19
    /** @var mixed $config */
20
    protected $config;
21
22
    /**
23
     * Variable which contains extra configuration.
24
     * @var array $defaultConfigs
25
     */
26
    public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing',
27
                                   'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE',
28
                                   'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE',
29
                                   'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
30
                                   'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
31
                                   'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
32
                                   'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
33
                                   'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER',
34
                                   'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default',
35
                                   'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default',
36
                                   'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
37
                                   'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
38
                                   'PAGANTIS_URL_OK'=>'',
39
                                   'PAGANTIS_URL_KO'=>'',
40
                                   'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!'
41
    );
42
43
    /**
44
     * Log constructor.
45
     *
46
     * @param \Magento\Framework\App\Action\Context $context
47
     * @param \Pagantis\Pagantis\Helper\Config      $pagantisConfig
48
     * @param ResourceConnection                    $dbObject
49
     */
50
    public function __construct(
51
        \Magento\Framework\App\Action\Context $context,
52
        \Pagantis\Pagantis\Helper\Config $pagantisConfig,
53
        ResourceConnection $dbObject
54
    ) {
55
        $this->config = $pagantisConfig->getConfig();
56
        $this->dbObject = $dbObject;
57
58
        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...
59
    }
60
61
    /**
62
     * Main function
63
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
64
     */
65
    public function execute()
66
    {
67
        try {
68
            $response = array('status'=>null);
69
            $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE);
70
            $secretKey = $this->getRequest()->getParam('secret');
71
            $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null;
72
73
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
74
            $dbConnection = $this->dbObject->getConnection();
75
            if ($privateKey != $secretKey) {
76
                $response['status'] = 401;
77
                $response['result'] = 'Unauthorized';
78
            } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
79
                if (count($_POST)) {
80
                    foreach ($_POST as $config => $value) {
81
                        if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
82
                            $dbConnection->update(
83
                                $tableName,
84
                                array('value' => $value),
85
                                "config='$config'"
86
                            );
87
                        } else {
88
                            $response['status'] = 400;
89
                            $response['result'] = 'Bad request';
90
                        }
91
                    }
92
                } else {
93
                    $response['status'] = 422;
94
                    $response['result'] = 'Empty data';
95
                }
96
            }
97
98
            $formattedResult = array();
99
            if ($response['status']==null) {
100
                $dbResult = $dbConnection->fetchAll("select * from $tableName");
101
                foreach ($dbResult as $value) {
102
                    $formattedResult[$value['config']] = $value['value'];
103
                }
104
                $response['result'] = $formattedResult;
105
            }
106
            $result = json_encode($response['result']);
107
            header("HTTP/1.1 ".$response['status'], true, $response['status']);
108
            header('Content-Type: application/json', true);
109
            header('Content-Length: '.strlen($result));
110
            echo($result);
111
            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...
112
        } catch (\Exception $e) {
113
            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...
114
        }
115
    }
116
117
    /**
118
     * @param RequestInterface $request
119
     *
120
     * @return InvalidRequestException|null
121
     */
122
    public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

122
    public function createCsrfValidationException(/** @scrutinizer ignore-unused */ RequestInterface $request): ?InvalidRequestException

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        return null;
125
    }
126
127
    /**
128
     * @param RequestInterface $request
129
     *
130
     * @return bool|null
131
     */
132
    public function validateForCsrf(RequestInterface $request): ?bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

132
    public function validateForCsrf(/** @scrutinizer ignore-unused */ RequestInterface $request): ?bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    {
134
        return true;
135
    }
136
}
137