Passed
Pull Request — master (#5)
by Cesar
07:23
created

Log::execute()   D

Complexity

Conditions 11
Paths 365

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 33
nc 365
nop 0
dl 0
loc 47
rs 4.4208
c 0
b 0
f 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
7
class Log extends Action
8
{
9
    /** Concurrency tablename */
10
    const LOGS_TABLE = 'pmt_logs';
11
12
    /** @var mixed $config */
13
    protected $config;
14
15
    /** @var ResourceConnection $dbObject */
16
    protected $dbObject;
17
18
    /**
19
     * Log constructor.
20
     *
21
     * @param \Magento\Framework\App\Action\Context $context
22
     * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig
23
     * @param ResourceConnection                    $dbObject
24
     */
25
    public function __construct(
26
        \Magento\Framework\App\Action\Context $context,
27
        \DigitalOrigin\Pmt\Helper\Config $pmtConfig,
28
        ResourceConnection $dbObject
29
    ) {
30
        $this->config = $pmtConfig->getConfig();
31
        $this->dbObject = $dbObject;
32
        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...
33
    }
34
35
    /**
36
     * Main function
37
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
38
     */
39
    public function execute()
40
    {
41
        try {
42
            $response = array();
43
            $secretKey = $this->getRequest()->getParam('secret');
44
            $privateKey = isset($this->config['secret_key']) ? $this->config['secret_key'] : null;
45
46
            if ($secretKey!='' && $privateKey!='') {
47
                $this->checkDbLogTable();
48
                /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
49
                $dbConnection = $this->dbObject->getConnection();
50
                $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
51
                $sql          = $dbConnection
52
                    ->select()
53
                    ->from($tableName, array('log', 'createdAt'));
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

53
                    ->/** @scrutinizer ignore-call */ from($tableName, array('log', 'createdAt'));

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...
54
55
                if ($dateFrom = $this->getRequest()->getParam('from')) {
56
                    $sql->where('createdAt > ?', $dateFrom);
57
                }
58
59
                if ($dateTo = $this->getRequest()->getParam('to')) {
60
                    $sql->where('createdAt < ?', $dateTo);
61
                }
62
63
                $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50;
64
                $sql->limit($limit);
65
                $sql->order('createdAt', 'desc');
0 ignored issues
show
Unused Code introduced by
The call to Magento\Framework\DB\Select::order() has too many arguments starting with 'desc'. ( Ignorable by Annotation )

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

65
                $sql->/** @scrutinizer ignore-call */ 
66
                      order('createdAt', 'desc');

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...
66
67
                $results = $dbConnection->fetchAll($sql);
68
                if (isset($results) && $privateKey == $secretKey) {
69
                    foreach ($results as $key => $result) {
70
                        $response[$key]['timestamp'] = $result['createdAt'];
71
                        $response[$key]['log']       = json_decode($result['log']);
72
                    }
73
                } else {
74
                    $response['result'] = 'Error';
75
                }
76
77
                $response = json_encode($response);
78
                header("HTTP/1.1 200", true, 200);
79
                header('Content-Type: application/json', true);
80
                header('Content-Length: '.strlen($response));
81
                echo($response);
82
                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...
83
            }
84
        } catch (\Exception $e) {
85
            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...
86
        }
87
    }
88
89
    /**
90
     * @return void|\Zend_Db_Statement_Interface
91
     * @throws \Zend_Db_Exception
92
     */
93
    private function checkDbLogTable()
94
    {
95
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
96
        $dbConnection = $this->dbObject->getConnection();
97
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
98
        if (!$dbConnection->isTableExists($tableName)) {
99
            $table = $dbConnection
100
                ->newTable($tableName)
101
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true))
0 ignored issues
show
Bug introduced by
The type DigitalOrigin\Pmt\Controller\Payment\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
102
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
103
                ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT));
104
            return $dbConnection->createTable($table);
105
        }
106
107
        return;
108
    }
109
}
110