Completed
Push — master ( 1137b9...97c77e )
by
unknown
20s queued 11s
created

Iframe::insertLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace DigitalOrigin\Pmt\Controller\Payment;
3
4
use Magento\Framework\App\Action\Action;
5
use Magento\Framework\View\Element\BlockInterface;
6
use Magento\Framework\App\ResourceConnection;
7
use Magento\Framework\DB\Ddl\Table;
8
9
class Iframe extends Action
10
{
11
    /** Concurrency tablename */
12
    const LOGS_TABLE = 'pmt_logs';
13
14
    /**
15
     * @var \Magento\Framework\View\Result\PageFactory
16
     */
17
    protected $_pageFactory;
18
19
    /** @var ResourceConnection $dbObject */
20
    protected $dbObject;
21
22
    public function __construct(
23
        \Magento\Framework\App\Action\Context $context,
24
        \Magento\Framework\View\Result\PageFactory $pageFactory,
25
        ResourceConnection $dbObject,
26
        \DigitalOrigin\Pmt\Helper\Config $pmtconfig
27
    ) {
28
        $this->_pageFactory = $pageFactory;
29
        $this->dbObject = $dbObject;
30
        $this->config = $pmtconfig->getConfig();
0 ignored issues
show
Bug Best Practice introduced by pgarces
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        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...
32
    }
33
34
    /**
35
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|\Magento\Framework\View\Result\Page
36
     */
37
    public function execute()
38
    {
39
        try {
40
            $resultPage = $this->_pageFactory->create();
41
            $orderId = $this->getRequest()->getParam('orderId');
42
            if ($orderId == '') {
43
                throw new \Exception('Empty orderId');
44
            }
45
46
            if ($this->config['public_key'] == '' || $this->config['secret_key'] == '') {
47
                throw new \Exception('Public and Secret Key not found');
48
            }
49
50
            $orderClient = new \PagaMasTarde\OrdersApiClient\Client(
51
                $this->config['public_key'],
52
                $this->config['secret_key']
53
            );
54
55
            $order = $orderClient->getOrder($orderId);
56
57
            /** @var \DigitalOrigin\Pmt\Block\Payment\Iframe $block */
58
            $block = $resultPage->getLayout()->getBlock('paylater_payment_iframe');
59
            $block
60
                ->setEmail($order->getUser()->getEmail())
61
                ->setOrderUrl($order->getActionUrls()->getForm())
62
                ->setCheckoutUrl($order->getConfiguration()->getUrls()->getCancel())
63
            ;
64
65
            return $resultPage;
66
        } catch (\Exception $exception) {
67
            $this->insertLog($exception);
68
            die($exception->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...
69
        }
70
    }
71
72
    private function checkDbLogTable()
73
    {
74
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
75
        $dbConnection = $this->dbObject->getConnection();
76
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
77
        if (!$dbConnection->isTableExists($tableName)) {
78
            $table = $dbConnection
79
                ->newTable($tableName)
80
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true))
81
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
82
                ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>TIMESTAMP_INIT));
0 ignored issues
show
Bug introduced by pgarces
The constant DigitalOrigin\Pmt\Contro...\Payment\TIMESTAMP_INIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
83
            return $dbConnection->createTable($table);
84
        }
85
86
        return;
87
    }
88
89
    private function insertLog($exceptionMessage)
90
    {
91
        if ($exceptionMessage instanceof \Exception) {
92
            $this->checkDbLogTable();
93
            $logObject          = new \stdClass();
94
            $logObject->message = $exceptionMessage->getMessage();
95
            $logObject->code    = $exceptionMessage->getCode();
96
            $logObject->line    = $exceptionMessage->getLine();
97
            $logObject->file    = $exceptionMessage->getFile();
98
            $logObject->trace   = $exceptionMessage->getTraceAsString();
99
100
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
101
            $dbConnection = $this->dbObject->getConnection();
102
            $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
103
            $dbConnection->insert($tableName, array('log' => json_encode($logObject)));
104
        }
105
    }
106
}
107