ApiLog   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A _construct() 0 4 1
A getParamValue() 0 7 2
B addApiLogEntry() 0 26 2
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Model\ResourceModel;
28
29
use Payone\Core\Model\Api\Request\Base;
30
31
/**
32
 * ApiLog resource model
33
 */
34
class ApiLog extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
35
{
36
    /**
37
     * PAYONE shop helper
38
     *
39
     * @var \Payone\Core\Helper\Shop
40
     */
41
    protected $shopHelper;
42
43
    /**
44
     * Class constructor
45
     *
46
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
47
     * @param \Payone\Core\Helper\Shop $shopHelper
48
     * @param string $connectionName
49
     */
50
    public function __construct(
51
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
52
        \Payone\Core\Helper\Shop $shopHelper,
53
        $connectionName = null
54
    ) {
55
        parent::__construct($context, $connectionName);
56
        $this->shopHelper = $shopHelper;
57
    }
58
59
    /**
60
     * Initialize connection and table
61
     *
62
     * @return void
63
     */
64
    protected function _construct()
65
    {
66
        $this->_init('payone_protocol_api', 'id');
67
    }
68
69
    /**
70
     * Get value from array at given key or empty string if not set
71
     *
72
     * @param  array  $aRequest
73
     * @param  string $sField
74
     * @return string
75
     */
76
    protected function getParamValue($aRequest, $sField)
77
    {
78
        if (isset($aRequest[$sField])) {
79
            return $aRequest[$sField];
80
        }
81
        return '';
82
    }
83
84
    /**
85
     * Save Api-log entry to database
86
     *
87
     * @param  Base   $oRequest
88
     * @param  array  $aResponse
89
     * @param  string $sStatus
90
     * @return $this
91
     */
92
    public function addApiLogEntry(Base $oRequest, $aResponse, $sStatus = '')
93
    {
94
        $aRequest = $oRequest->getParameters();
95
        $iTxid = '';
96
        if (isset($aResponse['txid'])) {
97
            $iTxid = $aResponse['txid'];
98
        }
99
100
        $this->getConnection()->insert(
101
            $this->getMainTable(),
102
            [
103
                'order_id' => $oRequest->getOrderId(),
104
                'store_id' => $this->shopHelper->getStoreId(),
105
                'refnr' => $this->getParamValue($aRequest, 'reference'),
106
                'txid' => $iTxid,
107
                'requesttype' => $this->getParamValue($aRequest, 'request'),
108
                'responsestatus' => $sStatus,
109
                'mid' => $this->getParamValue($aRequest, 'mid'),
110
                'aid' => $this->getParamValue($aRequest, 'aid'),
111
                'portalid' => $this->getParamValue($aRequest, 'portalid'),
112
                'raw_request' => serialize($aRequest),
113
                'raw_response' => serialize($aResponse),
114
            ]
115
        );
116
        return $this;
117
    }
118
}
119