Passed
Push — master ( 470652...80150a )
by Hannes
02:48 queued 10s
created

Toolkit::maskIban()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 1
dl 0
loc 15
rs 8.8333
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\Helper;
28
29
use Magento\Framework\DataObject;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\DataObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Magento\Sales\Model\Order as SalesOrder;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Magento\Store\Model\Store;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\Store was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use Payone\Core\Model\Methods\PayoneMethod;
33
use Magento\Framework\Exception\LocalizedException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\LocalizedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
35
/**
36
 * Toolkit class for methods that dont fit in a certain drawer
37
 */
38
class Toolkit extends \Payone\Core\Helper\Base
39
{
40
    /**
41
     * PAYONE payment helper
42
     *
43
     * @var \Payone\Core\Helper\Payment
44
     */
45
    protected $paymentHelper;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param \Magento\Framework\App\Helper\Context      $context
51
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
52
     * @param \Payone\Core\Helper\Payment                $paymentHelper
53
     * @param \Payone\Core\Helper\Shop                   $shopHelper
54
     */
55
    public function __construct(
56
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Helper\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
        \Payone\Core\Helper\Payment $paymentHelper,
59
        \Payone\Core\Helper\Shop $shopHelper
60
    ) {
61
        parent::__construct($context, $storeManager, $shopHelper);
62
        $this->paymentHelper = $paymentHelper;
63
    }
64
65
    /**
66
     * Get security keys for all payment types for the given store code
67
     *
68
     * @param  string $sStoreCode
69
     * @return array
70
     */
71
    protected function getAllPayoneSecurityKeysByStoreCode($sStoreCode)
72
    {
73
        $aKeys = [];
74
        foreach ($this->paymentHelper->getAvailablePaymentTypes() as $sPaymentCode) {
75
            $iUseGlobal = $this->getConfigParam('use_global', $sPaymentCode, 'payone_payment', $sStoreCode);
76
            if ($iUseGlobal == '0') {
77
                $aKeys[] = $this->getConfigParam('key', $sPaymentCode, 'payone_payment', $sStoreCode);
78
            }
79
        }
80
        return $aKeys;
81
    }
82
83
    /**
84
     * Get the configured security keys for all available stores
85
     * and payment types - since every payment-type can have its own
86
     *
87
     * @return array
88
     */
89
    public function getAllPayoneSecurityKeys()
90
    {
91
        $aKeys = $this->getConfigParamAllStores('key');
92
        $aShopIds = $this->storeManager->getStores(false, true);
93
        foreach ($aShopIds as $sStoreCode => $oStore) {
94
            $aKeys = array_merge($aKeys, $this->getAllPayoneSecurityKeysByStoreCode($sStoreCode));
95
        }
96
        return array_unique($aKeys);
97
    }
98
99
    /**
100
     * Check wheither the given key is configured in the shop and thus valid
101
     *
102
     * @param  string $sKey
103
     * @return bool
104
     */
105
    public function isKeyValid($sKey)
106
    {
107
        $aKeyValues = $this->getAllPayoneSecurityKeys();
108
        foreach ($aKeyValues as $sConfigKey) {
109
            if (md5($sConfigKey) == $sKey) {
110
                return true;
111
            }
112
        }
113
        return false;
114
    }
115
116
    /**
117
     * Replace substitutes in a given text with the given replacements
118
     *
119
     * @param  string   $sText
120
     * @param  string   $aSubstitutionArray
121
     * @param  int|bool $iMaxLength
122
     * @return string
123
     */
124
    public function handleSubstituteReplacement($sText, $aSubstitutionArray, $iMaxLength = false)
125
    {
126
        if (!empty($sText)) {
127
            $sText = str_replace(array_keys($aSubstitutionArray), array_values($aSubstitutionArray), $sText);
0 ignored issues
show
Bug introduced by
$aSubstitutionArray of type string is incompatible with the type array expected by parameter $input of array_values(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
            $sText = str_replace(array_keys($aSubstitutionArray), array_values(/** @scrutinizer ignore-type */ $aSubstitutionArray), $sText);
Loading history...
Bug introduced by
$aSubstitutionArray of type string is incompatible with the type array expected by parameter $input of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
            $sText = str_replace(array_keys(/** @scrutinizer ignore-type */ $aSubstitutionArray), array_values($aSubstitutionArray), $sText);
Loading history...
128
            if ($iMaxLength !== false && strlen($sText) > $iMaxLength) {
129
                $sText = substr($sText, 0, $iMaxLength); // shorten text if too long
0 ignored issues
show
Bug introduced by
It seems like $iMaxLength can also be of type true; however, parameter $length of substr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
                $sText = substr($sText, 0, /** @scrutinizer ignore-type */ $iMaxLength); // shorten text if too long
Loading history...
130
            }
131
            return $sText;
132
        }
133
        return '';
134
    }
135
136
    /**
137
     * Get substituted invoice appendix text
138
     *
139
     * @param  SalesOrder $oOrder
140
     * @return string
141
     */
142
    public function getInvoiceAppendix(SalesOrder $oOrder)
143
    {
144
        $sText = $this->getConfigParam('invoice_appendix', 'invoicing'); // get invoice appendix from config
145
        $aSubstitutionArray = [
146
            '{{order_increment_id}}' => $oOrder->getIncrementId(),
147
            '{{customer_id}}' => $oOrder->getCustomerId(),
148
        ];
149
        $sInvoiceAppendix = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, 255);
0 ignored issues
show
Bug introduced by
$aSubstitutionArray of type array is incompatible with the type string expected by parameter $aSubstitutionArray of Payone\Core\Helper\Toolk...SubstituteReplacement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
        $sInvoiceAppendix = $this->handleSubstituteReplacement($sText, /** @scrutinizer ignore-type */ $aSubstitutionArray, 255);
Loading history...
150
        return $sInvoiceAppendix;
151
    }
152
153
    /**
154
     * Returns narrative text for authorization request
155
     *
156
     * @param  SalesOrder   $oOrder
157
     * @param  PayoneMethod $oPayment
158
     * @return string
159
     */
160
    public function getNarrativeText(SalesOrder $oOrder, PayoneMethod $oPayment)
161
    {
162
        $sText = $this->getConfigParam('narrative_text', $oPayment->getCode(), 'payone_payment'); // get narrative text for payment from config
163
        $aSubstitutionArray = [
164
            '{{order_increment_id}}' => $oOrder->getIncrementId(),
165
        ];
166
        $sNarrativeText = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, $oPayment->getNarrativeTextMaxLength());
0 ignored issues
show
Bug introduced by
$aSubstitutionArray of type array is incompatible with the type string expected by parameter $aSubstitutionArray of Payone\Core\Helper\Toolk...SubstituteReplacement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
        $sNarrativeText = $this->handleSubstituteReplacement($sText, /** @scrutinizer ignore-type */ $aSubstitutionArray, $oPayment->getNarrativeTextMaxLength());
Loading history...
167
        return $sNarrativeText;
168
    }
169
170
    /**
171
     * Format a price to the XX.YY format
172
     *
173
     * @param  double $dPrice    price of any sort
174
     * @param  int    $iDecimals number of digits behind the decimal point
175
     * @return string
176
     */
177
    public function formatNumber($dPrice, $iDecimals = 2)
178
    {
179
        return number_format($dPrice, $iDecimals, '.', '');
180
    }
181
182
    /**
183
     * Masks IBAN
184
     *
185
     * @param string $sUnmasked
186
     * @return string
187
     */
188
    public function maskIban($sUnmasked)
189
    {
190
        $sMasked = '';
191
        for ($i = 0; $i < strlen($sUnmasked); $i++) {
192
            if ($i == 2 || ((strlen($sUnmasked) - 2) % 4 == 0 && (($i - 2) % 4 == 0))) {
193
                $sMasked .= ' ';
194
            }
195
196
            if ($i < 4 || $i >= (strlen($sUnmasked) - 4)) {
197
                $sMasked .= $sUnmasked[$i];
198
            } else {
199
                $sMasked .= 'x';
200
            }
201
        }
202
        return $sMasked;
203
    }
204
205
    /**
206
     * Checks if given string is utf8 encoded
207
     *
208
     * @param  string $sString
209
     * @return bool
210
     */
211
    public function isUTF8($sString)
212
    {
213
        return $sString === mb_convert_encoding(mb_convert_encoding($sString, "UTF-32", "UTF-8"), "UTF-8", "UTF-32");
214
    }
215
216
    /**
217
     * Return data from data-object
218
     * Needed because of different ways to read from it for different magento versions
219
     *
220
     * @param  DataObject $oData
221
     * @param  string     $sKey
222
     * @return string|null
223
     */
224
    public function getAdditionalDataEntry(DataObject $oData, $sKey)
225
    {
226
        // The way to read the form-parameters changed with version 2.0.6
227
        if (version_compare($this->shopHelper->getMagentoVersion(), '2.0.6', '>=')) { // Magento 2.0.6 and above
228
            $aAdditionalData = $oData->getAdditionalData();
229
            if (isset($aAdditionalData[$sKey])) {
230
                return $aAdditionalData[$sKey];
231
            }
232
            return null;
233
        }
234
        // everything below 2.0.6
235
        return $oData->getData($sKey);
236
    }
237
}
238