Passed
Push — master ( c01df0...b2280f )
by Cesar
10:38 queued 06:42
created

LogV2   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A checkDbLogTable() 0 25 2
D execute() 0 47 11
A validateForCsrf() 0 3 1
A createCsrfValidationException() 0 3 1
1
<?php
2
namespace Pagantis\Pagantis\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 LogV2 extends Action implements CsrfAwareActionInterface
12
{
13
    /** Concurrency tablename */
14
    const LOGS_TABLE = 'Pagantis_logs';
15
16
    /** @var mixed $config */
17
    protected $config;
18
19
    /** @var ResourceConnection $dbObject */
20
    protected $dbObject;
21
22
    /**
23
     * Log constructor.
24
     *
25
     * @param \Magento\Framework\App\Action\Context $context
26
     * @param \Pagantis\Pagantis\Helper\Config      $pagantisConfig
27
     * @param ResourceConnection                    $dbObject
28
     */
29
    public function __construct(
30
        \Magento\Framework\App\Action\Context $context,
31
        \Pagantis\Pagantis\Helper\Config $pagantisConfig,
32
        ResourceConnection $dbObject
33
    ) {
34
        $this->config = $pagantisConfig->getConfig();
35
        $this->dbObject = $dbObject;
36
37
38
        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...
39
    }
40
41
    /**
42
     * Main function
43
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
44
     */
45
    public function execute()
46
    {
47
        try {
48
            $response = array();
49
            $secretKey = $this->getRequest()->getParam('secret');
50
            $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null;
51
52
            if ($secretKey!='' && $privateKey!='') {
53
                $this->checkDbLogTable();
54
                /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
55
                $dbConnection = $this->dbObject->getConnection();
56
                $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
57
                $sql          = $dbConnection
58
                    ->select()
59
                    ->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

59
                    ->/** @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...
60
61
                if ($dateFrom = $this->getRequest()->getParam('from')) {
62
                    $sql->where('createdAt > ?', $dateFrom);
63
                }
64
65
                if ($dateTo = $this->getRequest()->getParam('to')) {
66
                    $sql->where('createdAt < ?', $dateTo);
67
                }
68
69
                $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50;
70
                $sql->limit($limit);
71
                $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

71
                $sql->/** @scrutinizer ignore-call */ 
72
                      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...
72
73
                $results = $dbConnection->fetchAll($sql);
74
                if (isset($results) && $privateKey == $secretKey) {
75
                    foreach ($results as $key => $result) {
76
                        $response[$key]['timestamp'] = $result['createdAt'];
77
                        $response[$key]['log']       = json_decode($result['log']);
78
                    }
79
                } else {
80
                    $response['result'] = 'Error';
81
                }
82
83
                $response = json_encode($response);
84
                header("HTTP/1.1 200", true, 200);
85
                header('Content-Type: application/json', true);
86
                header('Content-Length: '.strlen($response));
87
                echo($response);
88
                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...
89
            }
90
        } catch (\Exception $e) {
91
            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...
92
        }
93
    }
94
95
    /**
96
     * @return void|\Zend_Db_Statement_Interface
97
     * @throws \Zend_Db_Exception
98
     */
99
    private function checkDbLogTable()
100
    {
101
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
102
        $dbConnection = $this->dbObject->getConnection();
103
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
104
        if (!$dbConnection->isTableExists($tableName)) {
105
            $table = $dbConnection
106
                ->newTable($tableName)
107
                ->addColumn(
108
                    'id',
109
                    Table::TYPE_SMALLINT,
110
                    null,
111
                    array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)
112
                )
113
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
114
                ->addColumn(
115
                    'createdAt',
116
                    Table::TYPE_TIMESTAMP,
117
                    null,
118
                    array('nullable'=>false,
119
                          'default'=>Table::TIMESTAMP_INIT)
120
                );
121
            return $dbConnection->createTable($table);
122
        }
123
        return;
124
    }
125
126
    /**
127
     * @param RequestInterface $request
128
     *
129
     * @return InvalidRequestException|null
130
     */
131
    public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
132
    {
133
        return null;
134
    }
135
136
    /**
137
     * @param RequestInterface $request
138
     *
139
     * @return bool|null
140
     */
141
    public function validateForCsrf(RequestInterface $request): ?bool
142
    {
143
        return true;
144
    }
145
}
146