Consumerscore::getAllowedMethodsForScore()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.2
cc 4
eloc 10
nc 6
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\Quote\Api\Data\AddressInterface;
30
use Magento\Store\Model\ScopeInterface;
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
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,
64
        \Magento\Store\Model\StoreManagerInterface $storeManager,
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;
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_STORE,
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 View Code Duplication
    public function canShowPaymentHintText()
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...
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 View Code Duplication
    public function canShowAgreementMessage()
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...
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