Completed
Push — master ( 1137b9...97c77e )
by
unknown
20s queued 11s
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
use Magento\Framework\DB\Ddl\Table;
7
8
class Log extends Action
9
{
10
    /** Concurrency tablename */
11
    const LOGS_TABLE = 'pmt_logs';
12
13
    /** @var mixed $config */
14
    protected $config;
15
16
    /** @var ResourceConnection $dbObject */
17
    protected $dbObject;
18
19
    /**
20
     * Log constructor.
21
     *
22
     * @param \Magento\Framework\App\Action\Context $context
23
     * @param \DigitalOrigin\Pmt\Helper\Config      $pmtConfig
24
     * @param ResourceConnection                    $dbObject
25
     */
26
    public function __construct(
27
        \Magento\Framework\App\Action\Context $context,
28
        \DigitalOrigin\Pmt\Helper\Config $pmtConfig,
29
        ResourceConnection $dbObject
30
    ) {
31
        $this->config = $pmtConfig->getConfig();
32
        $this->dbObject = $dbObject;
33
        return parent::__construct($context);
0 ignored issues
show
Bug introduced by pgarces
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...
34
    }
35
36
    /**
37
     * Main function
38
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
39
     */
40
    public function execute()
41
    {
42
        try {
43
            $response = array();
44
            $secretKey = $this->getRequest()->getParam('secret');
45
            $privateKey = isset($this->config['secret_key']) ? $this->config['secret_key'] : null;
46
47
            if ($secretKey!='' && $privateKey!='') {
48
                $this->checkDbLogTable();
49
                /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
50
                $dbConnection = $this->dbObject->getConnection();
51
                $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
52
                $sql          = $dbConnection
53
                    ->select()
54
                    ->from($tableName, array('log', 'createdAt'));
0 ignored issues
show
Unused Code introduced by pgarces
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

54
                    ->/** @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...
55
56
                if ($dateFrom = $this->getRequest()->getParam('from')) {
57
                    $sql->where('createdAt > ?', $dateFrom);
58
                }
59
60
                if ($dateTo = $this->getRequest()->getParam('to')) {
61
                    $sql->where('createdAt < ?', $dateTo);
62
                }
63
64
                $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50;
65
                $sql->limit($limit);
66
                $sql->order('createdAt', 'desc');
0 ignored issues
show
Unused Code introduced by pgarces
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

66
                $sql->/** @scrutinizer ignore-call */ 
67
                      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...
67
68
                $results = $dbConnection->fetchAll($sql);
69
                if (isset($results) && $privateKey == $secretKey) {
70
                    foreach ($results as $key => $result) {
71
                        $response[$key]['timestamp'] = $result['createdAt'];
72
                        $response[$key]['log']       = json_decode($result['log']);
73
                    }
74
                } else {
75
                    $response['result'] = 'Error';
76
                }
77
78
                $response = json_encode($response);
79
                header("HTTP/1.1 200", true, 200);
80
                header('Content-Type: application/json', true);
81
                header('Content-Length: '.strlen($response));
82
                echo($response);
83
                exit();
0 ignored issues
show
Best Practice introduced by pgarces
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...
84
            }
85
        } catch (\Exception $e) {
86
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by pgarces
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
    /**
91
     * @return void|\Zend_Db_Statement_Interface
92
     * @throws \Zend_Db_Exception
93
     */
94
    private function checkDbLogTable()
95
    {
96
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
97
        $dbConnection = $this->dbObject->getConnection();
98
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
99
        if (!$dbConnection->isTableExists($tableName)) {
100
            $table = $dbConnection
101
                ->newTable($tableName)
102
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true))
103
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
104
                ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT));
105
            return $dbConnection->createTable($table);
106
        }
107
108
        return;
109
    }
110
}
111