Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

CheckedAddresses::getHashFromAddress()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 8.2222
cc 7
eloc 11
nc 7
nop 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 Magento\Quote\Api\Data\AddressInterface;
30
31
/**
32
 * CheckedAddresses resource model
33
 */
34
class CheckedAddresses extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
35
{
36
    /**
37
     * Shop helper object
38
     *
39
     * @var \Payone\Core\Helper\Shop
40
     */
41
    protected $shopHelper;
42
43
    /**
44
     * All parameters used for the address hash
45
     *
46
     * @var array
47
     */
48
    protected $aHashParams = [
49
        'firstname',
50
        'lastname',
51
        'company',
52
        'street',
53
        'zip',
54
        'city',
55
        'country',
56
        'state',
57
    ];
58
59
    /**
60
     * Class constructor
61
     *
62
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
63
     * @param \Payone\Core\Helper\Shop $shopHelper
64
     * @param string $connectionName
65
     */
66
    public function __construct(
67
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
68
        \Payone\Core\Helper\Shop $shopHelper,
69
        $connectionName = null
70
    ) {
71
        parent::__construct($context, $connectionName);
72
        $this->shopHelper = $shopHelper;
73
    }
74
75
    /**
76
     * Initialize connection and table
77
     *
78
     * @return void
79
     */
80
    protected function _construct()
81
    {
82
        $this->_init('payone_checked_addresses', 'address_hash');
83
    }
84
85
    /**
86
     * Get address array for hash creation
87
     *
88
     * @param  AddressInterface $oAddress
89
     * @return array
90
     */
91
    protected function getAddressArray(AddressInterface $oAddress)
92
    {
93
        return [
94
            'firstname' => $oAddress->getFirstname(),
95
            'lastname' => $oAddress->getLastname(),
96
            'company' => $oAddress->getCompany(),
97
            'street' => $oAddress->getStreet()[0],
98
            'zip' => $oAddress->getPostcode(),
99
            'city' => $oAddress->getCity(),
100
            'country' => $oAddress->getCountryId(),
101
            'state' => $oAddress->getRegionCode(),
102
        ];
103
    }
104
105
    /**
106
     * Generate a unique hash of an address
107
     *
108
     * @param  AddressInterface $oAddress
109
     * @param  array            $aResponse
110
     * @return string
111
     */
112
    protected function getHashFromAddress(AddressInterface $oAddress, $aResponse = false)
113
    {
114
        $aAddressArray = $this->getAddressArray($oAddress); // collect data from the address object
115
116
        $sAddress = '';
117
        foreach ($this->aHashParams as $sParamKey) {
118
            $sParamValue = isset($aAddressArray[$sParamKey]) ? $aAddressArray[$sParamKey] : false;
119
            if ($sParamValue) {
120
                if ($aResponse !== false && array_key_exists($sParamKey, $aResponse) !== false && $aResponse[$sParamKey] != $sParamValue) {
121
                    //take the corrected value from the address-check
122
                    $sParamValue = $aResponse[$sParamKey];
123
                }
124
                $sAddress .= $sParamValue;
125
            }
126
        }
127
        $sHash = md5($sAddress); // generate hash from address for identification
128
129
        return $sHash;
130
    }
131
132
    /**
133
     * Save Api-log entry to database
134
     *
135
     * @param  AddressInterface $oAddress
136
     * @param  array            $aResponse
137
     * @param  bool             $blIsBonicheck
138
     * @return $this
139
     */
140
    public function addCheckedAddress(AddressInterface $oAddress, $aResponse, $blIsBonicheck = false)
141
    {
142
        $sHash = $this->getHashFromAddress($oAddress, $aResponse); // generate hash from given address
143
        $this->getConnection()->insert(
144
            $this->getMainTable(),
145
            [
146
                'address_hash' => $sHash,
147
                'is_bonicheck' => $blIsBonicheck
148
            ]
149
        );
150
        return $this;
151
    }
152
153
    /**
154
     * Check and return if this exact address has been checked before
155
     *
156
     * @param  AddressInterface $oAddress
157
     * @param  bool             $blIsBonicheck
158
     * @return bool
159
     */
160
    public function wasAddressCheckedBefore(AddressInterface $oAddress, $blIsBonicheck = false)
161
    {
162
        $sHash = $this->getHashFromAddress($oAddress);
163
        $oDb = $this->getConnection();
164
        $sQuery = " SELECT
165
                        checkdate
166
                    FROM
167
                        {$this->getMainTable()}
168
                    WHERE
169
                        address_hash = :hash AND
170
                        is_bonicheck = :isBoni";
171
        $aParams = [
172
            'hash' => $sHash,
173
            'isBoni' => $blIsBonicheck
174
        ];
175
176
        $sGroup = 'address_check';
177
        if ($blIsBonicheck === true) {
178
            $sGroup = 'creditrating';
179
        }
180
181
        $sLifetime = $this->shopHelper->getConfigParam('result_lifetime', $sGroup, 'payone_protect');
182
        if (!empty($sLifetime) && is_numeric($sLifetime)) {
183
            $sQuery .= " AND checkdate > DATE_SUB(NOW(), INTERVAL :lifetime DAY)";
184
            $aParams['lifetime'] = $sLifetime;
185
        } else {
186
            return false; // no lifetime = check every time
187
        }
188
189
        $sDate = $oDb->fetchOne($sQuery, $aParams);
190
        if ($sDate != false) {
191
            return true;
192
        }
193
        return false;
194
    }
195
}
196