Consumerscore   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 243
rs 9.36
c 0
b 0
f 0
wmc 38

13 Methods

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