Passed
Pull Request — master (#16)
by
unknown
03:49
created

Config::__construct()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 3
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
use Magento\Framework\App\CsrfAwareActionInterface;
8
use Magento\Framework\App\RequestInterface;
9
use Magento\Framework\App\Request\InvalidRequestException;
10
11
class Config extends Action implements CsrfAwareActionInterface
12
{
13
    /** Config tablename */
14
    const CONFIG_TABLE = 'pmt_config';
15
16
    /** @var ResourceConnection $dbObject */
17
    protected $dbObject;
18
19
    /**
20
     * Variable which contains extra configuration.
21
     * @var array $defaultConfigs
22
     */
23
    public $defaultConfigs = array('PMT_TITLE'=>'Instant Financing',
24
                                   'PMT_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE',
25
                                   'PMT_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE',
26
                                   'PMT_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
27
                                   'PMT_SIMULATOR_START_INSTALLMENTS'=>3,
28
                                   'PMT_SIMULATOR_MAX_INSTALLMENTS'=>12,
29
                                   'PMT_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
30
                                   'PMT_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER',
31
                                   'PMT_SIMULATOR_CSS_PRICE_SELECTOR'=>'default',
32
                                   'PMT_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default',
33
                                   'PMT_FORM_DISPLAY_TYPE'=>0,
34
                                   'PMT_DISPLAY_MIN_AMOUNT'=>1,
35
                                   'PMT_URL_OK'=>'',
36
                                   'PMT_URL_KO'=>'',
37
                                   'PMT_TITLE_EXTRA' => 'Paga hasta en 12 cómodas cuotas con Paga+Tarde. Solicitud totalmente 
38
                            online y sin papeleos,¡y la respuesta es inmediata!'
39
    );
40
41
    /**
42
     * Log constructor.
43
     *
44
     * @param \Magento\Framework\App\Action\Context $context
45
     * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig
46
     * @param ResourceConnection                    $dbObject
47
     */
48
    public function __construct(
49
        \Magento\Framework\App\Action\Context $context,
50
        \DigitalOrigin\Pmt\Helper\Config $pmtConfig,
51
        ResourceConnection $dbObject
52
    ) {
53
        $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...
54
        $this->dbObject = $dbObject;
55
56
        // CsrfAwareAction Magento2.3 compatibility
57
        if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) {
58
            $request = $this->getRequest();
59
            if ($request instanceof HttpRequest && $request->isPost() && empty($request->getParam('form_key'))) {
0 ignored issues
show
Bug introduced by
The type DigitalOrigin\Pmt\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

59
            if ($request instanceof HttpRequest && $request->/** @scrutinizer ignore-call */ isPost() && empty($request->getParam('form_key'))) {
Loading history...
60
                $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
61
                $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

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

129
    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...
130
    {
131
        return null;
132
    }
133
134
    /**
135
     * @param RequestInterface $request
136
     *
137
     * @return bool|null
138
     */
139
    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

139
    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...
140
    {
141
        return true;
142
    }
143
}
144