Passed
Pull Request — master (#16)
by
unknown
03:27
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 DigitalOrigin\Pmt\Controller\Payment;
3
4
use Magento\Framework\App\Action\Action;
5
use Magento\Framework\App\ResourceConnection;
6
use Magento\Framework\DB\Ddl\Table;
7
8
class Config extends Action
9
{
10
    /** Config tablename */
11
    const CONFIG_TABLE = 'pmt_config';
12
13
    /** @var ResourceConnection $dbObject */
14
    protected $dbObject;
15
16
    /**
17
     * Variable which contains extra configuration.
18
     * @var array $defaultConfigs
19
     */
20
    public $defaultConfigs = array('PMT_TITLE'=>'Instant Financing',
21
                                   'PMT_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE',
22
                                   'PMT_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE',
23
                                   'PMT_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
24
                                   'PMT_SIMULATOR_START_INSTALLMENTS'=>3,
25
                                   'PMT_SIMULATOR_MAX_INSTALLMENTS'=>12,
26
                                   'PMT_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
27
                                   'PMT_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER',
28
                                   'PMT_SIMULATOR_CSS_PRICE_SELECTOR'=>'default',
29
                                   'PMT_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default',
30
                                   'PMT_FORM_DISPLAY_TYPE'=>0,
31
                                   'PMT_DISPLAY_MIN_AMOUNT'=>1,
32
                                   'PMT_URL_OK'=>'',
33
                                   'PMT_URL_KO'=>'',
34
                                   'PMT_TITLE_EXTRA' => 'Paga hasta en 12 cómodas cuotas con Paga+Tarde. Solicitud totalmente 
35
                            online y sin papeleos,¡y la respuesta es inmediata!'
36
    );
37
38
    /**
39
     * Log constructor.
40
     *
41
     * @param \Magento\Framework\App\Action\Context $context
42
     * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig
43
     * @param ResourceConnection                    $dbObject
44
     */
45
    public function __construct(
46
        \Magento\Framework\App\Action\Context $context,
47
        \DigitalOrigin\Pmt\Helper\Config $pmtConfig,
48
        ResourceConnection $dbObject
49
    ) {
50
        $this->config = $pmtConfig->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...
51
        $this->dbObject = $dbObject;
52
        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...
53
    }
54
55
    /**
56
     * Main function
57
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
58
     */
59
    public function execute()
60
    {
61
        try {
62
            $response = array('status'=>null);
63
            $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE);
64
            $secretKey = $this->getRequest()->getParam('secret');
65
            $privateKey = isset($this->config['pmt_private_key']) ? $this->config['pmt_private_key'] : null;
66
67
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
68
            $dbConnection = $this->dbObject->getConnection();
69
            if ($privateKey != $secretKey) {
70
                $response['status'] = 401;
71
                $response['result'] = 'Unauthorized';
72
            } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
73
                if (count($_POST)) {
74
                    foreach ($_POST as $config => $value) {
75
                        if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
76
                            $dbConnection->update(
77
                                $tableName,
78
                                array('value' => $value),
79
                                "config='$config'"
80
                            );
81
                        } else {
82
                            $response['status'] = 400;
83
                            $response['result'] = 'Bad request';
84
                        }
85
                    }
86
                } else {
87
                    $response['status'] = 422;
88
                    $response['result'] = 'Empty data';
89
                }
90
            }
91
92
            $formattedResult = array();
93
            if ($response['status']==null) {
94
                $dbResult = $dbConnection->fetchAll("select * from $tableName");
95
                foreach ($dbResult as $value) {
96
                    $formattedResult[$value['config']] = $value['value'];
97
                }
98
                $response['result'] = $formattedResult;
99
            }
100
            $result = json_encode($response['result']);
101
            header("HTTP/1.1 ".$response['status'], true, $response['status']);
102
            header('Content-Type: application/json', true);
103
            header('Content-Length: '.strlen($result));
104
            echo($result);
105
            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...
106
        } catch (\Exception $e) {
107
            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...
108
        }
109
    }
110
}
111