Total Complexity | 43 |
Total Lines | 272 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CheckoutSubmitBefore 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 CheckoutSubmitBefore, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class CheckoutSubmitBefore implements ObserverInterface |
||
42 | { |
||
43 | /** |
||
44 | * PAYONE consumerscore request model |
||
45 | * |
||
46 | * @var \Payone\Core\Model\Api\Request\Consumerscore |
||
47 | */ |
||
48 | protected $consumerscore; |
||
49 | |||
50 | /** |
||
51 | * Consumerscore helper |
||
52 | * |
||
53 | * @var \Payone\Core\Helper\Consumerscore |
||
54 | */ |
||
55 | protected $consumerscoreHelper; |
||
56 | |||
57 | /** |
||
58 | * Addresscheck management object |
||
59 | * |
||
60 | * @var \Payone\Core\Model\Risk\Addresscheck |
||
61 | */ |
||
62 | protected $addresscheck; |
||
63 | |||
64 | /** |
||
65 | * Checkout session object |
||
66 | * |
||
67 | * @var \Magento\Checkout\Model\Session |
||
68 | */ |
||
69 | protected $checkoutSession; |
||
70 | |||
71 | /** |
||
72 | * Payone customer helper |
||
73 | * |
||
74 | * @var \Payone\Core\Helper\Customer |
||
75 | */ |
||
76 | protected $customerHelper; |
||
77 | |||
78 | /** |
||
79 | * Constructor |
||
80 | * |
||
81 | * @param \Payone\Core\Model\Api\Request\Consumerscore $consumerscore |
||
82 | * @param \Payone\Core\Helper\Consumerscore $consumerscoreHelper |
||
83 | * @param \Payone\Core\Model\Risk\Addresscheck $addresscheck |
||
84 | * @param \Magento\Checkout\Model\Session $checkoutSession |
||
85 | * @param \Payone\Core\Helper\Customer $customerHelper |
||
86 | */ |
||
87 | public function __construct( |
||
88 | \Payone\Core\Model\Api\Request\Consumerscore $consumerscore, |
||
89 | \Payone\Core\Helper\Consumerscore $consumerscoreHelper, |
||
90 | \Payone\Core\Model\Risk\Addresscheck $addresscheck, |
||
91 | \Magento\Checkout\Model\Session $checkoutSession, |
||
92 | \Payone\Core\Helper\Customer $customerHelper |
||
93 | ) { |
||
94 | $this->consumerscore = $consumerscore; |
||
95 | $this->consumerscoreHelper = $consumerscoreHelper; |
||
96 | $this->addresscheck = $addresscheck; |
||
97 | $this->checkoutSession = $checkoutSession; |
||
98 | $this->customerHelper = $customerHelper; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get parameter from config |
||
103 | * |
||
104 | * @param string $sParam |
||
105 | * @param bool $blIsAddresscheck |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getConfigParam($sParam, $blIsAddresscheck = false) |
||
109 | { |
||
110 | $sGroup = 'creditrating'; |
||
111 | if ($blIsAddresscheck === true) { |
||
112 | $sGroup = 'address_check'; |
||
113 | } |
||
114 | return $this->consumerscoreHelper->getConfigParam($sParam, $sGroup, 'payone_protect'); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Check if given payment methods was enabled for bonicheck in the configuration |
||
119 | * |
||
120 | * @param string $sPaymentCode |
||
121 | * @return bool |
||
122 | */ |
||
123 | protected function isPaymentMethodEnabledForCheck($sPaymentCode) |
||
124 | { |
||
125 | $aPaymentTypesToCheck = $this->consumerscoreHelper->getConsumerscoreEnabledMethods(); |
||
126 | if (array_search($sPaymentCode, $aPaymentTypesToCheck) !== false) { |
||
127 | return true; |
||
128 | } |
||
129 | return false; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Determine if creditrating is needed |
||
134 | * |
||
135 | * @param Quote $oQuote |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function isCreditratingNeeded(Quote $oQuote) |
||
139 | { |
||
140 | if (!$this->consumerscoreHelper->isCreditratingNeeded(Event::AFTER_PAYMENT, $oQuote->getGrandTotal())) { |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | if ($this->isPaymentMethodEnabledForCheck($oQuote->getPayment()->getMethodInstance()->getCode()) === false) { |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | if ($oQuote->getPayment()->getMethodInstance()->getInfoInstance()->getAdditionalInformation('payone_boni_agreement') === false) { // getAdditionalInformation() returns null if field not existing |
||
149 | return false; // agreement checkbox was not checked by the customer |
||
150 | } |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param Quote $oQuote |
||
157 | */ |
||
158 | protected function isBonicheckAgreementActiveAndNotConfirmedByCustomer($oQuote) |
||
159 | { |
||
160 | if (!$this->consumerscoreHelper->canShowAgreementMessage() || // check if agreement is configured |
||
161 | !$this->isPaymentMethodEnabledForCheck($oQuote->getPayment()->getMethodInstance()->getCode()) || // check if selected payment methods is enabled for bonicheck |
||
162 | $oQuote->getPayment()->getMethodInstance()->getInfoInstance()->getAdditionalInformation('payone_boni_agreement') !== false // check if agreement was not confirmed |
||
163 | ) { |
||
164 | return false; |
||
165 | } |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Determine if the payment type can be used with this score |
||
171 | * |
||
172 | * @param Quote $oQuote |
||
173 | * @param string $sScore |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function isPaymentApplicableForScore(Quote $oQuote, $sScore) |
||
177 | { |
||
178 | if ($sScore == 'G') { |
||
179 | return true; |
||
180 | } |
||
181 | |||
182 | $sPaymentCode = $oQuote->getPayment()->getMethodInstance()->getCode(); |
||
183 | |||
184 | $aYellowMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('Y'); |
||
185 | $aRedMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('R'); |
||
186 | |||
187 | if ($sScore == 'Y' && (array_search($sPaymentCode, $aYellowMethods) !== false || |
||
188 | array_search($sPaymentCode, $aRedMethods) !== false)) { |
||
189 | return true; |
||
190 | } elseif ($sScore == 'R' && array_search($sPaymentCode, $aRedMethods) !== false) { |
||
191 | return true; |
||
192 | } |
||
193 | return false; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * |
||
198 | * @param array $aResponse |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function checkoutNeedsToBeStopped($aResponse) |
||
202 | { |
||
203 | if (!$aResponse || (isset($aResponse['status']) && $aResponse['status'] == 'ERROR' |
||
204 | && $this->getConfigParam('handle_response_error') == 'stop_checkout')) { |
||
205 | return true; |
||
206 | } |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Filter payment methods by the creditrating result if applicable |
||
212 | * |
||
213 | * @param AddressInterface $oBilling |
||
214 | * @return string |
||
215 | * @throws LocalizedException |
||
216 | */ |
||
217 | public function getScoreByCreditrating(AddressInterface $oBilling) |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Get error message for when the creditrating failed because the score is insufficient |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getInsufficientScoreMessage() |
||
252 | { |
||
253 | $sErrorMsg = $this->getConfigParam('insufficient_score_message'); |
||
254 | if (empty($sErrorMsg)) { |
||
255 | $sErrorMsg = 'An error occured during the credit check.'; |
||
256 | } |
||
257 | return $sErrorMsg; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Returns allowed payment methods for the given score |
||
262 | * |
||
263 | * @param string $sScore |
||
264 | * @return array |
||
265 | */ |
||
266 | protected function getPaymentWhitelist($sScore) |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Execute certain tasks after the payment is placed and thus the order is placed |
||
277 | * |
||
278 | * @param Observer $observer |
||
279 | * @return void |
||
280 | * @throws LocalizedException |
||
281 | */ |
||
282 | public function execute(Observer $observer) |
||
316 |
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