Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

Consumerscore::canShowPaymentHintText()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

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