| Total Complexity | 43 |
| Total Lines | 262 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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\Proxy |
||
| 68 | */ |
||
| 69 | protected $checkoutSession; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor |
||
| 73 | * |
||
| 74 | * @param \Payone\Core\Model\Api\Request\Consumerscore $consumerscore |
||
| 75 | * @param \Payone\Core\Helper\Consumerscore $consumerscoreHelper |
||
| 76 | * @param \Payone\Core\Model\Risk\Addresscheck $addresscheck |
||
| 77 | * @param \Magento\Checkout\Model\Session\Proxy $checkoutSession |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 80 | \Payone\Core\Model\Api\Request\Consumerscore $consumerscore, |
||
| 81 | \Payone\Core\Helper\Consumerscore $consumerscoreHelper, |
||
| 82 | \Payone\Core\Model\Risk\Addresscheck $addresscheck, |
||
| 83 | \Magento\Checkout\Model\Session\Proxy $checkoutSession |
||
| 84 | ) { |
||
| 85 | $this->consumerscore = $consumerscore; |
||
| 86 | $this->consumerscoreHelper = $consumerscoreHelper; |
||
| 87 | $this->addresscheck = $addresscheck; |
||
| 88 | $this->checkoutSession = $checkoutSession; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get parameter from config |
||
| 93 | * |
||
| 94 | * @param string $sParam |
||
| 95 | * @param bool $blIsAddresscheck |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getConfigParam($sParam, $blIsAddresscheck = false) |
||
| 99 | { |
||
| 100 | $sGroup = 'creditrating'; |
||
| 101 | if ($blIsAddresscheck === true) { |
||
| 102 | $sGroup = 'address_check'; |
||
| 103 | } |
||
| 104 | return $this->consumerscoreHelper->getConfigParam($sParam, $sGroup, 'payone_protect'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Check if given payment methods was enabled for bonicheck in the configuration |
||
| 109 | * |
||
| 110 | * @param string $sPaymentCode |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | protected function isPaymentMethodEnabledForCheck($sPaymentCode) |
||
| 114 | { |
||
| 115 | $aPaymentTypesToCheck = $this->consumerscoreHelper->getConsumerscoreEnabledMethods(); |
||
| 116 | if (array_search($sPaymentCode, $aPaymentTypesToCheck) !== false) { |
||
| 117 | return true; |
||
| 118 | } |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Determine if creditrating is needed |
||
| 124 | * |
||
| 125 | * @param Quote $oQuote |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function isCreditratingNeeded(Quote $oQuote) |
||
| 129 | { |
||
| 130 | if (!$this->consumerscoreHelper->isCreditratingNeeded(Event::AFTER_PAYMENT, $oQuote->getGrandTotal())) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($this->isPaymentMethodEnabledForCheck($oQuote->getPayment()->getMethodInstance()->getCode()) === false) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($oQuote->getPayment()->getMethodInstance()->getInfoInstance()->getAdditionalInformation('payone_boni_agreement') === false) { // getAdditionalInformation() returns null if field not existing |
||
| 139 | return false; // agreement checkbox was not checked by the customer |
||
| 140 | } |
||
| 141 | |||
| 142 | return true; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param Quote $oQuote |
||
| 147 | */ |
||
| 148 | protected function isBonicheckAgreementActiveAndNotConfirmedByCustomer($oQuote) |
||
| 149 | { |
||
| 150 | if (!$this->consumerscoreHelper->canShowAgreementMessage() || // check if agreement is configured |
||
| 151 | !$this->isPaymentMethodEnabledForCheck($oQuote->getPayment()->getMethodInstance()->getCode()) || // check if selected payment methods is enabled for bonicheck |
||
| 152 | $oQuote->getPayment()->getMethodInstance()->getInfoInstance()->getAdditionalInformation('payone_boni_agreement') !== false // check if agreement was not confirmed |
||
| 153 | ) { |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Determine if the payment type can be used with this score |
||
| 161 | * |
||
| 162 | * @param Quote $oQuote |
||
| 163 | * @param string $sScore |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function isPaymentApplicableForScore(Quote $oQuote, $sScore) |
||
| 167 | { |
||
| 168 | if ($sScore == 'G') { |
||
| 169 | return true; |
||
| 170 | } |
||
| 171 | |||
| 172 | $sPaymentCode = $oQuote->getPayment()->getMethodInstance()->getCode(); |
||
| 173 | |||
| 174 | $aYellowMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('Y'); |
||
| 175 | $aRedMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('R'); |
||
| 176 | |||
| 177 | if ($sScore == 'Y' && (array_search($sPaymentCode, $aYellowMethods) !== false || |
||
| 178 | array_search($sPaymentCode, $aRedMethods) !== false)) { |
||
| 179 | return true; |
||
| 180 | } elseif ($sScore == 'R' && array_search($sPaymentCode, $aRedMethods) !== false) { |
||
| 181 | return true; |
||
| 182 | } |
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * |
||
| 188 | * @param array $aResponse |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function checkoutNeedsToBeStopped($aResponse) |
||
| 192 | { |
||
| 193 | if (!$aResponse || (isset($aResponse['status']) && $aResponse['status'] == 'ERROR' |
||
| 194 | && $this->getConfigParam('handle_response_error') == 'stop_checkout')) { |
||
| 195 | return true; |
||
| 196 | } |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Filter payment methods by the creditrating result if applicable |
||
| 202 | * |
||
| 203 | * @param AddressInterface $oBilling |
||
| 204 | * @return string |
||
| 205 | * @throws LocalizedException |
||
| 206 | */ |
||
| 207 | public function getScoreByCreditrating(AddressInterface $oBilling) |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get error message for when the creditrating failed because the score is insufficient |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getInsufficientScoreMessage() |
||
| 242 | { |
||
| 243 | $sErrorMsg = $this->getConfigParam('insufficient_score_message'); |
||
| 244 | if (empty($sErrorMsg)) { |
||
| 245 | $sErrorMsg = 'An error occured during the credit check.'; |
||
| 246 | } |
||
| 247 | return $sErrorMsg; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns allowed payment methods for the given score |
||
| 252 | * |
||
| 253 | * @param string $sScore |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function getPaymentWhitelist($sScore) |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Execute certain tasks after the payment is placed and thus the order is placed |
||
| 267 | * |
||
| 268 | * @param Observer $observer |
||
| 269 | * @return void |
||
| 270 | * @throws LocalizedException |
||
| 271 | */ |
||
| 272 | public function execute(Observer $observer) |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 |
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