Passed
Pull Request — master (#16)
by
unknown
06:23 queued 02:44
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
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
     * Log constructor.
18
     *
19
     * @param \Magento\Framework\App\Action\Context $context
20
     * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig
21
     * @param ResourceConnection                    $dbObject
22
     */
23
    public function __construct(
24
        \Magento\Framework\App\Action\Context $context,
25
        \DigitalOrigin\Pmt\Helper\Config $pmtConfig,
26
        ResourceConnection $dbObject
27
    ) {
28
        $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...
29
        $this->dbObject = $dbObject;
30
        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...
31
    }
32
33
    /**
34
     * Main function
35
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
36
     */
37
    public function execute()
38
    {
39
        try {
40
            $response = array('status'=>null);
41
            $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE);
42
            $secretKey = $this->getRequest()->getParam('secret');
43
            $privateKey = isset($this->config['pmt_private_key']) ? $this->config['pmt_private_key'] : null;
44
45
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
46
            $dbConnection = $this->dbObject->getConnection();
47
            if ($privateKey != $secretKey) {
48
                $response['status'] = 401;
49
                $response['result'] = 'Unauthorized';
50
            } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
51
                if (count($_POST)) {
52
                    foreach ($_POST as $config => $value) {
53
                        if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
0 ignored issues
show
Bug Best Practice introduced by
The property defaultConfigs does not exist on DigitalOrigin\Pmt\Controller\Payment\Config. Did you maybe forget to declare it?
Loading history...
54
                            $dbConnection->update(
55
                                $tableName,
56
                                array('value' => $value),
57
                                "config='$$config'"
58
                            );
59
                        } else {
60
                            $response['status'] = 400;
61
                            $response['result'] = 'Bad request';
62
                        }
63
                    }
64
                } else {
65
                    $response['status'] = 422;
66
                    $response['result'] = 'Empty data';
67
                }
68
            }
69
70
            if ($response['status']==null) {
71
                $dbResult = $dbConnection
72
                    ->select()
73
                    ->from($tableName, array('config', 'value'));
0 ignored issues
show
Unused Code introduced by
The call to Magento\Framework\DB\Select::from() has too many arguments starting with $tableName. ( Ignorable by Annotation )

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

73
                    ->/** @scrutinizer ignore-call */ from($tableName, array('config', 'value'));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
                foreach ($dbResult as $value) {
75
                    $formattedResult[$value['config']] = $value['value'];
76
                }
77
                $response['result'] = $formattedResult;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $formattedResult seems to be defined by a foreach iteration on line 74. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
78
            }
79
            $result = json_encode($response['result']);
80
            header("HTTP/1.1 ".$response['status'], true, $response['status']);
81
            header('Content-Type: application/json', true);
82
            header('Content-Length: '.strlen($result));
83
            echo($result);
84
            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...
85
        } catch (\Exception $e) {
86
            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...
87
        }
88
    }
89
}
90