Completed
Push — master ( 6c4da9...442164 )
by Florian
14s queued 11s
created

MethodList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
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\Model\Plugins;
28
29
use Magento\Payment\Model\MethodList as OrigMethodList;
30
use Magento\Payment\Model\MethodInterface;
31
use Magento\Quote\Api\Data\AddressInterface;
32
use Payone\Core\Model\Source\CreditratingIntegrationEvent as Event;
33
use Magento\Quote\Model\Quote;
34
35
/**
36
 * Plugin for Magentos MethodList class
37
 */
38
class MethodList
39
{
40
    /**
41
     * PAYONE consumerscore request model
42
     *
43
     * @var \Payone\Core\Model\Api\Request\Consumerscore
44
     */
45
    protected $consumerscore;
46
47
    /**
48
     * Consumerscore helper
49
     *
50
     * @var \Payone\Core\Helper\Consumerscore
51
     */
52
    protected $consumerscoreHelper;
53
54
    /**
55
     * Checkout session
56
     *
57
     * @var \Magento\Checkout\Model\Session
58
     */
59
    protected $checkoutSession;
60
61
    /**
62
     * Payment ban entity
63
     *
64
     * @var \Payone\Core\Model\ResourceModel\PaymentBan
65
     */
66
    protected $paymentBan;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param \Payone\Core\Model\Api\Request\Consumerscore $consumerscore
72
     * @param \Payone\Core\Helper\Consumerscore            $consumerscoreHelper
73
     * @param \Magento\Checkout\Model\Session              $checkoutSession
74
     * @param \Payone\Core\Model\ResourceModel\PaymentBan  $paymentBan
75
     */
76
    public function __construct(
77
        \Payone\Core\Model\Api\Request\Consumerscore $consumerscore,
78
        \Payone\Core\Helper\Consumerscore $consumerscoreHelper,
79
        \Magento\Checkout\Model\Session $checkoutSession,
80
        \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan
81
    ) {
82
        $this->consumerscore = $consumerscore;
83
        $this->consumerscoreHelper = $consumerscoreHelper;
84
        $this->checkoutSession = $checkoutSession;
85
        $this->paymentBan = $paymentBan;
86
    }
87
88
    /**
89
     * Filter methods by the worst score
90
     *
91
     * @param  MethodInterface[] $aPaymentMethods
92
     * @param  string            $sWorstScore
93
     * @return MethodInterface[]
94
     */
95
    protected function filterMethodsByScore($aPaymentMethods, $sWorstScore)
96
    {
97
        $aRedMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('R');
98
        $aYellowMethods = array_merge($aRedMethods, $this->consumerscoreHelper->getAllowedMethodsForScore('Y'));
99
100
        $aReturnMethods = [];
101
        foreach ($aPaymentMethods as $oMethod) {
102
            if ($sWorstScore == 'Y' && array_search($oMethod->getCode(), $aYellowMethods) !== false) {
103
                $aReturnMethods[] = $oMethod;
104
            }
105
106
            if ($sWorstScore == 'R' && array_search($oMethod->getCode(), $aRedMethods) !== false) {
107
                $aReturnMethods[] = $oMethod;
108
            }
109
        }
110
        return $aReturnMethods;
111
    }
112
113
    /**
114
     * Execute a consumerscore request to PAYONE or load an old score if its lifetime is still active
115
     *
116
     * @param  AddressInterface $oShipping
117
     * @return string
118
     */
119
    protected function getScoreByCreditrating(AddressInterface $oShipping)
120
    {
121
        $aResponse = $this->consumerscore->sendRequest($oShipping);
122
        if ($aResponse === true) {// creditrating not executed because of a previous check
123
            $this->consumerscoreHelper->copyOldStatusToNewAddress($oShipping);
124
        }
125
126
        if (isset($aResponse['score'])) {
127
            $oShipping->setPayoneProtectScore($aResponse['score'])->save();
128
        }
129
130
        $sScore = $oShipping->getPayoneProtectScore();
131
        return $sScore;
132
    }
133
134
    /**
135
     * Get parameter from config
136
     *
137
     * @param  string $sParam
138
     * @param  bool   $blIsAddresscheck
139
     * @return string
140
     */
141 View Code Duplication
    protected function getConfigParam($sParam, $blIsAddresscheck = false)
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...
142
    {
143
        $sGroup = 'creditrating';
144
        if ($blIsAddresscheck === true) {
145
            $sGroup = 'address_check';
146
        }
147
        return $this->consumerscoreHelper->getConfigParam($sParam, $sGroup, 'payone_protect');
148
    }
149
150
    /**
151
     * Get quote object from session
152
     *
153
     * @return Quote
154
     */
155
    protected function getQuote()
156
    {
157
        return $this->checkoutSession->getQuote();
158
    }
159
160
    /**
161
     * Return banned payment methods for the current user
162
     *
163
     * @param  Quote $oQuote
164
     * @return array
165
     */
166
    protected function getBannedPaymentMethods(Quote $oQuote)
167
    {
168
        $aBans = [];
169
        if (!empty($oQuote->getCustomerId())) {
170
            $aBans = $this->paymentBan->getPaymentBans($oQuote->getCustomerId());
171
        } else { // guest checkout
172
            $aSessionBans = $this->checkoutSession->getPayonePaymentBans();
173
            if (!empty($aSessionBans)) {
174
                $aBans = $aSessionBans;
175
            }
176
        }
177
        return $aBans;
178
    }
179
180
    /**
181
     * Remove banned paymenttypes
182
     *
183
     * @param  array $aPaymentMethods
184
     * @param  Quote $oQuote
185
     * @return array
186
     */
187
    protected function removeBannedPaymentMethods($aPaymentMethods, Quote $oQuote)
188
    {
189
        $aBannedMethos = $this->getBannedPaymentMethods($oQuote);
190
        for($i = 0; $i < count($aPaymentMethods); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
191
            $sCode = $aPaymentMethods[$i]->getCode();
192
            if (array_key_exists($sCode, $aBannedMethos) !== false) {
193
                $iBannedUntil = strtotime($aBannedMethos[$sCode]);
194
                if ($iBannedUntil > time()) {
195
                    unset($aPaymentMethods[$i]);
196
                }
197
            }
198
        }
199
        return $aPaymentMethods;
200
    }
201
202
    /**
203
     *
204
     * @param  OrigMethodList    $subject
205
     * @param  MethodInterface[] $aPaymentMethods
206
     * @return MethodInterface[]
207
     */
208
    public function afterGetAvailableMethods(OrigMethodList $subject, $aPaymentMethods)
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
        $oQuote = $this->getQuote();
211
        $oShipping = $oQuote->getShippingAddress();
212
213
        $aScores = [];
214
        if ($this->getConfigParam('enabled', true)) {// is addresscheck active
215
            $aScores[] = $oShipping->getPayoneAddresscheckScore();
216
        }
217
218
        $dTotal = $oQuote->getGrandTotal();
219
        if ($this->consumerscoreHelper->isCreditratingNeeded(Event::BEFORE_PAYMENT, $dTotal) === true) {
220
            $aScores[] = $this->getScoreByCreditrating($oShipping);
221
        }
222
223
        $sScore = $this->consumerscoreHelper->getWorstScore($aScores);
224
        if ($sScore != 'G') { // no need to filter
225
            $aPaymentMethods = $this->filterMethodsByScore($aPaymentMethods, $sScore);
226
        }
227
228
        $aPaymentMethods = $this->removeBannedPaymentMethods($aPaymentMethods, $oQuote);
229
230
        return $aPaymentMethods;
231
    }
232
}
233