Toolkit::getInvoiceAppendix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
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;
30
use Magento\Sales\Model\Order as SalesOrder;
31
use Payone\Core\Model\Methods\PayoneMethod;
32
33
/**
34
 * Toolkit class for methods that dont fit in a certain drawer
35
 */
36
class Toolkit extends \Payone\Core\Helper\Base
37
{
38
    /**
39
     * PAYONE payment helper
40
     *
41
     * @var \Payone\Core\Helper\Payment
42
     */
43
    protected $paymentHelper;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param \Magento\Framework\App\Helper\Context      $context
49
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
50
     * @param \Payone\Core\Helper\Payment                $paymentHelper
51
     * @param \Payone\Core\Helper\Shop                   $shopHelper
52
     */
53
    public function __construct(
54
        \Magento\Framework\App\Helper\Context $context,
55
        \Magento\Store\Model\StoreManagerInterface $storeManager,
56
        \Payone\Core\Helper\Payment $paymentHelper,
57
        \Payone\Core\Helper\Shop $shopHelper
58
    ) {
59
        parent::__construct($context, $storeManager, $shopHelper);
60
        $this->paymentHelper = $paymentHelper;
61
    }
62
63
    /**
64
     * Get security keys for all payment types for the given store code
65
     *
66
     * @param  string $sStoreCode
67
     * @return array
68
     */
69
    protected function getAllPayoneSecurityKeysByStoreCode($sStoreCode)
70
    {
71
        $aKeys = [];
72
        foreach ($this->paymentHelper->getAvailablePaymentTypes() as $sPaymentCode) {
73
            $iUseGlobal = $this->getConfigParam('use_global', $sPaymentCode, 'payone_payment', $sStoreCode);
74
            if ($iUseGlobal == '0') {
75
                $aKeys[] = $this->getConfigParam('key', $sPaymentCode, 'payone_payment', $sStoreCode);
76
            }
77
        }
78
        return $aKeys;
79
    }
80
81
    /**
82
     * Get the configured security keys for all available stores
83
     * and payment types - since every payment-type can have its own
84
     *
85
     * @return array
86
     */
87
    public function getAllPayoneSecurityKeys()
88
    {
89
        $aKeys = $this->getConfigParamAllStores('key');
90
        $aShopIds = $this->storeManager->getStores(false, true);
91
        foreach ($aShopIds as $sStoreCode => $oStore) {
92
            $aKeys = array_merge($aKeys, $this->getAllPayoneSecurityKeysByStoreCode($sStoreCode));
93
        }
94
        return array_unique($aKeys);
95
    }
96
97
    /**
98
     * Check wheither the given key is configured in the shop and thus valid
99
     *
100
     * @param  string $sKey
101
     * @return bool
102
     */
103
    public function isKeyValid($sKey)
104
    {
105
        $aKeyValues = $this->getAllPayoneSecurityKeys();
106
        foreach ($aKeyValues as $sConfigKey) {
107
            if (md5($sConfigKey) == $sKey) {
108
                return true;
109
            }
110
        }
111
        return false;
112
    }
113
114
    /**
115
     * Replace substitutes in a given text with the given replacements
116
     *
117
     * @param  string   $sText
118
     * @param  string   $aSubstitutionArray
119
     * @param  int|bool $iMaxLength
120
     * @return string
121
     */
122
    public function handleSubstituteReplacement($sText, $aSubstitutionArray, $iMaxLength = false)
123
    {
124
        if (!empty($sText)) {
125
            $sText = str_replace(array_keys($aSubstitutionArray), array_values($aSubstitutionArray), $sText);
126
            if ($iMaxLength !== false && strlen($sText) > $iMaxLength) {
127
                $sText = substr($sText, 0, $iMaxLength); // shorten text if too long
128
            }
129
            return $sText;
130
        }
131
        return '';
132
    }
133
134
    /**
135
     * Get substituted invoice appendix text
136
     *
137
     * @param  SalesOrder $oOrder
138
     * @return string
139
     */
140 View Code Duplication
    public function getInvoiceAppendix(SalesOrder $oOrder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $sText = $this->getConfigParam('invoice_appendix', 'invoicing'); // get invoice appendix from config
143
        $aSubstitutionArray = [
144
            '{{order_increment_id}}' => $oOrder->getIncrementId(),
145
            '{{customer_id}}' => $oOrder->getCustomerId(),
146
        ];
147
        $sInvoiceAppendix = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, 255);
0 ignored issues
show
Documentation introduced by
$aSubstitutionArray is of type array<string,?,{"{{order..."{{customer_id}}":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
        return $sInvoiceAppendix;
149
    }
150
151
    /**
152
     * Returns narrative text for authorization request
153
     *
154
     * @param  SalesOrder   $oOrder
155
     * @param  PayoneMethod $oPayment
156
     * @return string
157
     */
158 View Code Duplication
    public function getNarrativeText(SalesOrder $oOrder, PayoneMethod $oPayment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        $sText = $this->getConfigParam('narrative_text', $oPayment->getCode(), 'payone_payment'); // get narrative text for payment from config
161
        $aSubstitutionArray = [
162
            '{{order_increment_id}}' => $oOrder->getIncrementId(),
163
        ];
164
        $sNarrativeText = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, $oPayment->getNarrativeTextMaxLength());
0 ignored issues
show
Documentation introduced by
$aSubstitutionArray is of type array<string,?,{"{{order_increment_id}}":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
        return $sNarrativeText;
166
    }
167
168
    /**
169
     * Format a price to the XX.YY format
170
     *
171
     * @param  double $dPrice    price of any sort
172
     * @param  int    $iDecimals number of digits behind the decimal point
173
     * @return string
174
     */
175
    public function formatNumber($dPrice, $iDecimals = 2)
176
    {
177
        return number_format($dPrice, $iDecimals, '.', '');
178
    }
179
180
    /**
181
     * Checks if given string is utf8 encoded
182
     *
183
     * @param  string $sString
184
     * @return bool
185
     */
186
    public function isUTF8($sString)
187
    {
188
        return $sString === mb_convert_encoding(mb_convert_encoding($sString, "UTF-32", "UTF-8"), "UTF-8", "UTF-32");
189
    }
190
191
    /**
192
     * Return data from data-object
193
     * Needed because of different ways to read from it for different magento versions
194
     *
195
     * @param  DataObject $oData
196
     * @param  string     $sKey
197
     * @return string|null
198
     */
199
    public function getAdditionalDataEntry(DataObject $oData, $sKey)
200
    {
201
        // The way to read the form-parameters changed with version 2.0.6
202
        if (version_compare($this->shopHelper->getMagentoVersion(), '2.0.6', '>=')) { // Magento 2.0.6 and above
203
            $aAdditionalData = $oData->getAdditionalData();
204
            if (isset($aAdditionalData[$sKey])) {
205
                return $aAdditionalData[$sKey];
206
            }
207
            return null;
208
        }
209
        // everything below 2.0.6
210
        return $oData->getData($sKey);
211
    }
212
}
213