Passed
Push — master ( ed26d0...23c688 )
by Florian
46s
created

ApiLog::getParamValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
     * Fields that need to be masked before written in to the API log
45
     *
46
     * @var array
47
     */
48
    protected $aMaskFields = [
49
        'ip',
50
    ];
51
52
    /**
53
     * Class constructor
54
     *
55
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
56
     * @param \Payone\Core\Helper\Shop $shopHelper
57
     * @param string $connectionName
58
     */
59
    public function __construct(
60
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
61
        \Payone\Core\Helper\Shop $shopHelper,
62
        $connectionName = null
63
    ) {
64
        parent::__construct($context, $connectionName);
65
        $this->shopHelper = $shopHelper;
66
    }
67
68
    /**
69
     * Initialize connection and table
70
     *
71
     * @return void
72
     */
73
    protected function _construct()
74
    {
75
        $this->_init('payone_protocol_api', 'id');
76
    }
77
78
    /**
79
     * Get value from array at given key or empty string if not set
80
     *
81
     * @param  array  $aRequest
82
     * @param  string $sField
83
     * @return string
84
     */
85
    protected function getParamValue($aRequest, $sField)
86
    {
87
        if (isset($aRequest[$sField])) {
88
            return $aRequest[$sField];
89
        }
90
        return '';
91
    }
92
93
    /**
94
     * Mask a given value with Xs
95
     *
96
     * @param  string $sValue
97
     * @return string
98
     */
99
    protected function maskValue($sValue)
100
    {
101
        for ($i = 0; $i < strlen($sValue); $i++) {
102
            $sValue[$i] = 'x';
103
        }
104
        return $sValue;
105
    }
106
107
    /**
108
     * Mask certain fields in the request array
109
     *
110
     * @param  array $aRequest
111
     * @return array
112
     */
113
    protected function maskParameters($aRequest)
114
    {
115
        foreach ($this->aMaskFields as $sKey) {
116
            if (isset($aRequest[$sKey])) {
117
                $aRequest[$sKey] = $this->maskValue($aRequest[$sKey]);
118
            }
119
        }
120
        return $aRequest;
121
    }
122
123
    /**
124
     * Save Api-log entry to database
125
     *
126
     * @param  Base   $oRequest
127
     * @param  array  $aResponse
128
     * @param  string $sStatus
129
     * @return $this
130
     */
131
    public function addApiLogEntry(Base $oRequest, $aResponse, $sStatus = '')
132
    {
133
        $aRequest = $oRequest->getParameters();
134
        $aRequest = $this->maskParameters($aRequest);
135
        $iTxid = '';
136
        if (isset($aResponse['txid'])) {
137
            $iTxid = $aResponse['txid'];
138
        }
139
140
        $this->getConnection()->insert(
141
            $this->getMainTable(),
142
            [
143
                'order_id' => $oRequest->getOrderId(),
144
                'store_id' => $this->shopHelper->getStoreId(),
145
                'refnr' => $this->getParamValue($aRequest, 'reference'),
146
                'txid' => $iTxid,
147
                'requesttype' => $this->getParamValue($aRequest, 'request'),
148
                'responsestatus' => $sStatus,
149
                'mid' => $this->getParamValue($aRequest, 'mid'),
150
                'aid' => $this->getParamValue($aRequest, 'aid'),
151
                'portalid' => $this->getParamValue($aRequest, 'portalid'),
152
                'raw_request' => serialize($aRequest),
153
                'raw_response' => serialize($aResponse),
154
            ]
155
        );
156
        return $this;
157
    }
158
}
159