Consumerscore::isCreditratingNeeded()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 2
dl 0
loc 18
rs 9.6111
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\Quote\Api\Data\AddressInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\Data\AddressInterface 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\Store\Model\ScopeInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\ScopeInterface 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
32
/**
33
 * Helper class for everything that has to do with the consumerscore request
34
 */
35
class Consumerscore extends \Payone\Core\Helper\Base
36
{
37
    const CONFIG_KEY_CONSUMERSCORE_SAMPLE_COUNTER = 'payone_consumerscore_sample_counter';
38
39
    /**
40
     * Config writer resource
41
     *
42
     * @var \Magento\Framework\App\Config\Storage\WriterInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Co...Storage\WriterInterface 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...
43
     */
44
    protected $configWriter;
45
46
    /**
47
     * PAYONE database helper
48
     *
49
     * @var \Payone\Core\Helper\Database
50
     */
51
    protected $databaseHelper;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param \Magento\Framework\App\Helper\Context                 $context
57
     * @param \Magento\Store\Model\StoreManagerInterface            $storeManager
58
     * @param \Payone\Core\Helper\Shop                              $shopHelper
59
     * @param \Magento\Framework\App\State                          $state
60
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
61
     * @param \Payone\Core\Helper\Database                          $databaseHelper
62
     */
63
    public function __construct(
64
        \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...
65
        \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...
66
        \Payone\Core\Helper\Shop $shopHelper,
67
        \Magento\Framework\App\State $state,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\State 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...
68
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
69
        \Payone\Core\Helper\Database $databaseHelper
70
    ) {
71
        parent::__construct($context, $storeManager, $shopHelper, $state);
72
        $this->configWriter = $configWriter;
73
        $this->databaseHelper = $databaseHelper;
74
    }
75
76
    /**
77
     * Retrieve the creditrating sample counter from config
78
     *
79
     * @return int
80
     */
81
    public function getConsumerscoreSampleCounter()
82
    {
83
        $iCounter = $this->databaseHelper->getConfigParamWithoutCache(
84
            self::CONFIG_KEY_CONSUMERSCORE_SAMPLE_COUNTER,
85
            'creditrating',
86
            'payone_protect'
87
        );
88
        if (empty($iCounter) || !is_numeric($iCounter)) {
89
            $iCounter = 0;
90
        }
91
        return $iCounter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $iCounter also could return the type string which is incompatible with the documented return type integer.
Loading history...
92
    }
93
94
    /**
95
     * Store new value for creditrating sample counter in config
96
     *
97
     * @param  $iCount
98
     * @return true
99
     */
100
    public function setConsumerscoreSampleCounter($iCount)
101
    {
102
        $this->configWriter->save(
103
            'payone_protect/creditrating/'.self::CONFIG_KEY_CONSUMERSCORE_SAMPLE_COUNTER,
104
            $iCount,
105
            ScopeInterface::SCOPE_STORES,
106
            $this->storeManager->getStore()->getId()
107
        );
108
        return true;
109
    }
110
111
    /**
112
     * Increment creditrating sample counter in config
113
     *
114
     * @return int Returns the new counter value
115
     */
116
    public function incrementConsumerscoreSampleCounter()
117
    {
118
        $iCounter = $this->getConsumerscoreSampleCounter(); // get current sample counter
119
120
        $iCounter++;
121
        $this->setConsumerscoreSampleCounter($iCounter); // set current sample counter
122
        return $iCounter;
123
    }
124
125
    /**
126
     * Determine if a consumerscore sample has to be taken
127
     *
128
     * @return bool
129
     */
130
    public function isSampleNeeded()
131
    {
132
        $iFrequency = $this->getConfigParam('sample_mode_frequency', 'creditrating', 'payone_protect');
133
        if ((bool)$this->getConfigParam('sample_mode_enabled', 'creditrating', 'payone_protect') && !empty($iFrequency)) {
134
            $iCounter = $this->getConsumerscoreSampleCounter(); // get current sample counter
135
            if ($iCounter % $iFrequency !== 0) {
136
                return false;
137
            }
138
        }
139
        return true;
140
    }
141
142
    /**
143
     * Return if the consumerscore hint text has to be shown on payment selection
144
     *
145
     * @return bool
146
     */
147
    public function canShowPaymentHintText()
148
    {
149
        if ((bool)$this->getConfigParam('enabled', 'creditrating', 'payone_protect')
150
            && (bool)$this->getConfigParam('payment_hint_enabled', 'creditrating', 'payone_protect')
151
            && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') {
152
            return true;
153
        }
154
        return false;
155
    }
156
157
    /**
158
     * Return if the consumerscore agreement message has to be shown on payment selection
159
     *
160
     * @return bool
161
     */
162
    public function canShowAgreementMessage()
163
    {
164
        if ((bool)$this->getConfigParam('enabled', 'creditrating', 'payone_protect')
165
            && (bool)$this->getConfigParam('agreement_enabled', 'creditrating', 'payone_protect')
166
            && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') {
167
            return true;
168
        }
169
        return false;
170
    }
171
172
    /**
173
     * Get worst score
174
     *
175
     * @param  array $aScores
176
     * @return string
177
     */
178
    public function getWorstScore($aScores)
179
    {
180
        if (array_search('R', $aScores) !== false) { // is there a red score existing?
181
            return 'R'; // return red as worst score
182
        }
183
184
        if (array_search('Y', $aScores) !== false) { // is there a yellow score existing?
185
            return 'Y'; // return yellow as worst score
186
        }
187
        return 'G'; // return green
188
    }
189
190
    /**
191
     * Get the allowed methods for the score and transform it into an array
192
     *
193
     * @param  string $sScore
194
     * @return array
195
     */
196
    public function getAllowedMethodsForScore($sScore)
197
    {
198
        $sMethods = '';
199
        if ($sScore == 'Y') {
200
            $sMethods = $this->getConfigParam('allow_payment_methods_yellow', 'creditrating', 'payone_protect');
201
        } elseif ($sScore == 'R') {
202
            $sMethods = $this->getConfigParam('allow_payment_methods_red', 'creditrating', 'payone_protect');
203
        }
204
205
        $aMethods = [];
206
        if (!empty($sScore) && !empty($sMethods)) {
207
            $aMethods = explode(',', $sMethods); // config comes as a concatinated string
208
        }
209
        return $aMethods;
210
    }
211
212
    /**
213
     * Copy the status of old creditrating checks to the new addresses
214
     * when the lifetime of the old check was still active
215
     *
216
     * @param  AddressInterface $oAddress
217
     * @return void
218
     */
219
    public function copyOldStatusToNewAddress(AddressInterface $oAddress)
220
    {
221
        $sOldStatus = $this->databaseHelper->getOldAddressStatus($oAddress); // get old score from db
222
        if (!empty($sOldStatus)) {
223
            $oAddress->setPayoneProtectScore($sOldStatus)->save(); // add score to quote address object
224
        }
225
    }
226
227
    /**
228
     * Determine if the given quote total needs a consumerscore check
229
     *
230
     * @param double $dTotal
231
     * @return bool
232
     */
233
    public function isCheckNeededForPrice($dTotal)
234
    {
235
        $dMin = $this->getConfigParam('min_order_total', 'creditrating', 'payone_protect');
236
        $dMax = $this->getConfigParam('max_order_total', 'creditrating', 'payone_protect');
237
        if (is_numeric($dMin) && is_numeric($dMax) && ($dTotal < $dMin || $dTotal > $dMax)) {
238
            return false;
239
        }
240
        return true;
241
    }
242
243
    /**
244
     * Base checks if a creditrating check is needed
245
     *
246
     * @param string $sIntegrationEvent
247
     * @param double $dGrandTotal
248
     * @return bool
249
     */
250
    public function isCreditratingNeeded($sIntegrationEvent, $dGrandTotal)
251
    {
252
        if ((bool)$this->getConfigParam('enabled', 'creditrating', 'payone_protect') === false) {
253
            return false;
254
        }
255
256
        if ($this->getConfigParam('integration_event', 'creditrating', 'payone_protect') != $sIntegrationEvent) {
257
            return false;
258
        }
259
260
        if ($this->isCheckNeededForPrice($dGrandTotal) === false) {
261
            return false;
262
        }
263
264
        if ($this->isSampleNeeded() === false) {
265
            return false;
266
        }
267
        return true;
268
    }
269
270
    /**
271
     * Reads consumerscore enabled methods from database and splits them to an array
272
     *
273
     * @return array
274
     */
275
    public function getConsumerscoreEnabledMethods()
276
    {
277
        $sEnabledMethods = $this->getConfigParam('enabled_for_payment_methods', 'creditrating', 'payone_protect');
278
        if (!empty($sEnabledMethods)) {
279
            return explode(',', $sEnabledMethods);
280
        }
281
        return [];
282
    }
283
}
284