| Total Complexity | 40 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Consumerscore 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 Consumerscore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 \Payone\Core\Helper\Shop $shopHelper |
||
| 59 | * @param \Magento\Framework\App\State $state |
||
| 60 | * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter |
||
| 61 | * @param \Payone\Core\Helper\Database $databaseHelper |
||
| 62 | */ |
||
| 63 | public function __construct( |
||
| 64 | \Magento\Framework\App\Helper\Context $context, |
||
| 65 | \Magento\Store\Model\StoreManagerInterface $storeManager, |
||
| 66 | \Payone\Core\Helper\Shop $shopHelper, |
||
| 67 | \Magento\Framework\App\State $state, |
||
| 68 | \Magento\Framework\App\Config\Storage\WriterInterface $configWriter, |
||
| 69 | \Payone\Core\Helper\Database $databaseHelper |
||
| 70 | ) { |
||
| 71 | parent::__construct($context, $storeManager, $shopHelper, $state); |
||
| 72 | $this->configWriter = $configWriter; |
||
| 73 | $this->databaseHelper = $databaseHelper; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Retrieve the creditrating sample counter from config |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | public function getConsumerscoreSampleCounter() |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Store new value for creditrating sample counter in config |
||
| 96 | * |
||
| 97 | * @param $iCount |
||
| 98 | * @return true |
||
| 99 | */ |
||
| 100 | public function setConsumerscoreSampleCounter($iCount) |
||
| 101 | { |
||
| 102 | $this->configWriter->save( |
||
| 103 | 'payone_protect/creditrating/'.self::CONFIG_KEY_CONSUMERSCORE_SAMPLE_COUNTER, |
||
| 104 | $iCount, |
||
| 105 | ScopeInterface::SCOPE_STORES, |
||
| 106 | $this->storeManager->getStore()->getId() |
||
| 107 | ); |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Increment creditrating sample counter in config |
||
| 113 | * |
||
| 114 | * @return int Returns the new counter value |
||
| 115 | */ |
||
| 116 | public function incrementConsumerscoreSampleCounter() |
||
| 117 | { |
||
| 118 | $iCounter = $this->getConsumerscoreSampleCounter(); // get current sample counter |
||
| 119 | |||
| 120 | $iCounter++; |
||
| 121 | $this->setConsumerscoreSampleCounter($iCounter); // set current sample counter |
||
| 122 | return $iCounter; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Determine if a consumerscore sample has to be taken |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isSampleNeeded() |
||
| 131 | { |
||
| 132 | $iFrequency = $this->getConfigParam('sample_mode_frequency', 'creditrating', 'payone_protect'); |
||
| 133 | if ((bool)$this->getConfigParam('sample_mode_enabled', 'creditrating', 'payone_protect') && !empty($iFrequency)) { |
||
| 134 | $iCounter = $this->getConsumerscoreSampleCounter(); // get current sample counter |
||
| 135 | if ($iCounter % $iFrequency !== 0) { |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | return true; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return if the consumerscore hint text has to be shown on payment selection |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function canShowPaymentHintText() |
||
| 148 | { |
||
| 149 | if ((bool)$this->getConfigParam('enabled', 'creditrating', 'payone_protect') |
||
| 150 | && (bool)$this->getConfigParam('payment_hint_enabled', 'creditrating', 'payone_protect') |
||
| 151 | && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') { |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return if the consumerscore agreement message has to be shown on payment selection |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function canShowAgreementMessage() |
||
| 163 | { |
||
| 164 | if ((bool)$this->getConfigParam('enabled', 'creditrating', 'payone_protect') |
||
| 165 | && (bool)$this->getConfigParam('agreement_enabled', 'creditrating', 'payone_protect') |
||
| 166 | && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') { |
||
| 167 | return true; |
||
| 168 | } |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get worst score |
||
| 174 | * |
||
| 175 | * @param array $aScores |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function getWorstScore($aScores) |
||
| 179 | { |
||
| 180 | if (array_search('R', $aScores) !== false) { // is there a red score existing? |
||
| 181 | return 'R'; // return red as worst score |
||
| 182 | } |
||
| 183 | |||
| 184 | if (array_search('Y', $aScores) !== false) { // is there a yellow score existing? |
||
| 185 | return 'Y'; // return yellow as worst score |
||
| 186 | } |
||
| 187 | return 'G'; // return green |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the allowed methods for the score and transform it into an array |
||
| 192 | * |
||
| 193 | * @param string $sScore |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function getAllowedMethodsForScore($sScore) |
||
| 197 | { |
||
| 198 | $sMethods = ''; |
||
| 199 | if ($sScore == 'Y') { |
||
| 200 | $sMethods = $this->getConfigParam('allow_payment_methods_yellow', 'creditrating', 'payone_protect'); |
||
| 201 | } elseif ($sScore == 'R') { |
||
| 202 | $sMethods = $this->getConfigParam('allow_payment_methods_red', 'creditrating', 'payone_protect'); |
||
| 203 | } |
||
| 204 | |||
| 205 | $aMethods = []; |
||
| 206 | if (!empty($sScore) && !empty($sMethods)) { |
||
| 207 | $aMethods = explode(',', $sMethods); // config comes as a concatinated string |
||
| 208 | } |
||
| 209 | return $aMethods; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Copy the status of old creditrating checks to the new addresses |
||
| 214 | * when the lifetime of the old check was still active |
||
| 215 | * |
||
| 216 | * @param AddressInterface $oAddress |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function copyOldStatusToNewAddress(AddressInterface $oAddress) |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Determine if the given quote total needs a consumerscore check |
||
| 229 | * |
||
| 230 | * @param double $dTotal |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isCheckNeededForPrice($dTotal) |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Base checks if a creditrating check is needed |
||
| 245 | * |
||
| 246 | * @param string $sIntegrationEvent |
||
| 247 | * @param double $dGrandTotal |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isCreditratingNeeded($sIntegrationEvent, $dGrandTotal) |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Reads consumerscore enabled methods from database and splits them to an array |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getConsumerscoreEnabledMethods() |
||
| 284 |
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