Total Complexity | 42 |
Total Lines | 281 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like MethodList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MethodList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class MethodList |
||
41 | { |
||
42 | /** |
||
43 | * PAYONE consumerscore request model |
||
44 | * |
||
45 | * @var \Payone\Core\Model\Api\Request\Consumerscore |
||
46 | */ |
||
47 | protected $consumerscore; |
||
48 | |||
49 | /** |
||
50 | * Consumerscore helper |
||
51 | * |
||
52 | * @var \Payone\Core\Helper\Consumerscore |
||
53 | */ |
||
54 | protected $consumerscoreHelper; |
||
55 | |||
56 | /** |
||
57 | * Checkout session |
||
58 | * |
||
59 | * @var \Magento\Checkout\Model\Session |
||
60 | */ |
||
61 | protected $checkoutSession; |
||
62 | |||
63 | /** |
||
64 | * Payment ban entity |
||
65 | * |
||
66 | * @var \Payone\Core\Model\ResourceModel\PaymentBan |
||
67 | */ |
||
68 | protected $paymentBan; |
||
69 | |||
70 | /** |
||
71 | * Addresscheck management object |
||
72 | * |
||
73 | * @var \Payone\Core\Model\Risk\Addresscheck |
||
74 | */ |
||
75 | protected $addresscheck; |
||
76 | |||
77 | /** |
||
78 | * Constructor |
||
79 | * |
||
80 | * @param \Payone\Core\Model\Api\Request\Consumerscore $consumerscore |
||
81 | * @param \Payone\Core\Helper\Consumerscore $consumerscoreHelper |
||
82 | * @param \Magento\Checkout\Model\Session $checkoutSession |
||
83 | * @param \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan |
||
84 | * @param \Payone\Core\Model\Risk\Addresscheck $addresscheck |
||
85 | */ |
||
86 | public function __construct( |
||
87 | \Payone\Core\Model\Api\Request\Consumerscore $consumerscore, |
||
88 | \Payone\Core\Helper\Consumerscore $consumerscoreHelper, |
||
89 | \Magento\Checkout\Model\Session $checkoutSession, |
||
90 | \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan, |
||
91 | \Payone\Core\Model\Risk\Addresscheck $addresscheck |
||
92 | ) { |
||
93 | $this->consumerscore = $consumerscore; |
||
94 | $this->consumerscoreHelper = $consumerscoreHelper; |
||
95 | $this->checkoutSession = $checkoutSession; |
||
96 | $this->paymentBan = $paymentBan; |
||
97 | $this->addresscheck = $addresscheck; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Filter methods by the worst score |
||
102 | * |
||
103 | * @param MethodInterface[] $aPaymentMethods |
||
104 | * @param string $sWorstScore |
||
105 | * @return MethodInterface[] |
||
106 | */ |
||
107 | protected function filterMethodsByScore($aPaymentMethods, $sWorstScore) |
||
108 | { |
||
109 | $aRedMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('R'); |
||
110 | $aYellowMethods = array_merge($aRedMethods, $this->consumerscoreHelper->getAllowedMethodsForScore('Y')); |
||
111 | |||
112 | $aReturnMethods = []; |
||
113 | foreach ($aPaymentMethods as $oMethod) { |
||
114 | if ($sWorstScore == 'Y' && array_search($oMethod->getCode(), $aYellowMethods) !== false) { |
||
115 | $aReturnMethods[] = $oMethod; |
||
116 | } |
||
117 | |||
118 | if ($sWorstScore == 'R' && array_search($oMethod->getCode(), $aRedMethods) !== false) { |
||
119 | $aReturnMethods[] = $oMethod; |
||
120 | } |
||
121 | } |
||
122 | return $aReturnMethods; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Execute a consumerscore request to PAYONE or load an old score if its lifetime is still active |
||
127 | * |
||
128 | * @param AddressInterface $oShipping |
||
129 | * @return string |
||
130 | */ |
||
131 | protected function getScoreByCreditrating(AddressInterface $oShipping) |
||
132 | { |
||
133 | $aResponse = $this->consumerscore->sendRequest($oShipping); |
||
134 | if ($aResponse === true) {// creditrating not executed because of a previous check |
||
135 | $this->consumerscoreHelper->copyOldStatusToNewAddress($oShipping); |
||
136 | } |
||
137 | |||
138 | if (isset($aResponse['score'])) { |
||
139 | $oShipping->setPayoneProtectScore($aResponse['score'])->save(); |
||
140 | } |
||
141 | |||
142 | $sScore = $oShipping->getPayoneProtectScore(); |
||
143 | if (isset($aResponse['personstatus']) && $aResponse['personstatus'] !== PersonStatus::NONE) { |
||
144 | $aMapping = $this->addresscheck->getPersonstatusMapping(); |
||
145 | if (array_key_exists($aResponse['personstatus'], $aMapping)) { |
||
146 | $sScore = $this->consumerscoreHelper->getWorstScore([$sScore, $aMapping[$aResponse['personstatus']]]); |
||
147 | } |
||
148 | } |
||
149 | return $sScore; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get parameter from config |
||
154 | * |
||
155 | * @param string $sParam |
||
156 | * @param bool $blIsAddresscheck |
||
157 | * @return string |
||
158 | */ |
||
159 | protected function getConfigParam($sParam, $blIsAddresscheck = false) |
||
160 | { |
||
161 | $sGroup = 'creditrating'; |
||
162 | if ($blIsAddresscheck === true) { |
||
163 | $sGroup = 'address_check'; |
||
164 | } |
||
165 | return $this->consumerscoreHelper->getConfigParam($sParam, $sGroup, 'payone_protect'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get quote object from session |
||
170 | * |
||
171 | * @return Quote |
||
172 | */ |
||
173 | protected function getQuote() |
||
174 | { |
||
175 | return $this->checkoutSession->getQuote(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Return banned payment methods for the current user |
||
180 | * |
||
181 | * @param Quote $oQuote |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function getBannedPaymentMethods(Quote $oQuote) |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Remove banned paymenttypes |
||
200 | * |
||
201 | * @param array $aPaymentMethods |
||
202 | * @param Quote $oQuote |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function removeBannedPaymentMethods($aPaymentMethods, Quote $oQuote) |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Remove paymenttypes not on the session whitelist |
||
222 | * |
||
223 | * @param array $aPaymentMethods |
||
224 | * @return array |
||
225 | */ |
||
226 | protected function removeNotWhitelistedPaymentMethods($aPaymentMethods) |
||
227 | { |
||
228 | $aWhitelist = $this->checkoutSession->getPayonePaymentWhitelist(); |
||
229 | if (!empty($aWhitelist)) { |
||
230 | $iCount = count($aPaymentMethods); |
||
231 | for($i = 0; $i < $iCount; $i++) { |
||
232 | if (array_search($aPaymentMethods[$i]->getCode(), $aWhitelist) === false) { |
||
233 | unset($aPaymentMethods[$i]); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | return $aPaymentMethods; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Remove Amazon Pay from payment method array |
||
242 | * |
||
243 | * @param array $aPaymentMethods |
||
244 | * @return array |
||
245 | */ |
||
246 | public function removeAmazonPay($aPaymentMethods) |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Removes Klarna base methode if there are no Klarna sub-types available |
||
258 | * |
||
259 | * @param array $aPaymentMethods |
||
260 | * @return array |
||
261 | */ |
||
262 | public function checkKlarnaMethods($aPaymentMethods) |
||
263 | { |
||
264 | $iKeyKlarna = false; |
||
265 | $blHasKlarnaSubtypes = false; |
||
266 | $aKlarnaSubtypes = [ |
||
267 | PayoneConfig::METHOD_KLARNA_INVOICE, |
||
268 | PayoneConfig::METHOD_KLARNA_DEBIT, |
||
269 | PayoneConfig::METHOD_KLARNA_INSTALLMENT |
||
270 | ]; |
||
271 | for($i = 0; $i < count($aPaymentMethods); $i++) { |
||
272 | if (isset($aPaymentMethods[$i])) { |
||
273 | if ($aPaymentMethods[$i]->getCode() == PayoneConfig::METHOD_KLARNA_BASE) { |
||
274 | $iKeyKlarna = $i; |
||
275 | } |
||
276 | if (in_array($aPaymentMethods[$i]->getCode(), $aKlarnaSubtypes) === true) { |
||
277 | $blHasKlarnaSubtypes = true; |
||
278 | break; |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | if ($iKeyKlarna !== false && $blHasKlarnaSubtypes === false) { |
||
284 | unset($aPaymentMethods[$iKeyKlarna]); |
||
285 | } |
||
286 | return $aPaymentMethods; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * |
||
291 | * @param OrigMethodList $subject |
||
292 | * @param MethodInterface[] $aPaymentMethods |
||
293 | * @return MethodInterface[] |
||
294 | */ |
||
295 | public function afterGetAvailableMethods(OrigMethodList $subject, $aPaymentMethods) |
||
321 | } |
||
322 | } |
||
323 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths