Issues (112)

Controller/Payment/Log.php (5 issues)

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
8
class Log extends Action
9
{
10
    /** Concurrency tablename */
11
    const LOGS_TABLE = 'Pagantis_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 \Pagantis\Pagantis\Helper\Config      $pagantisConfig
24
     * @param ResourceConnection                    $dbObject
25
     */
26
    public function __construct(
27
        \Magento\Framework\App\Action\Context $context,
28
        \Pagantis\Pagantis\Helper\Config $pagantisConfig,
29
        ResourceConnection $dbObject
30
    ) {
31
        $this->config = $pagantisConfig->getConfig();
32
        $this->dbObject = $dbObject;
33
        return parent::__construct($context);
0 ignored issues
show
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['pagantis_private_key']) ? $this->config['pagantis_private_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
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
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
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
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(
103
                    'id',
104
                    Table::TYPE_SMALLINT,
105
                    null,
106
                    array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)
107
                )
108
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
109
                ->addColumn(
110
                    'createdAt',
111
                    Table::TYPE_TIMESTAMP,
112
                    null,
113
                    array('nullable'=>false,
114
                          'default'=>Table::TIMESTAMP_INIT)
115
                );
116
            return $dbConnection->createTable($table);
117
        }
118
        return;
119
    }
120
}
121