| Total Complexity | 57 |
| Total Lines | 556 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Simulator 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 Simulator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Simulator extends Template |
||
| 19 | { |
||
| 20 | const PROMOTIONS_CATEGORY = 'pagantis-promotion-product'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $enabled; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $enabled_4x; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $enabled_12x; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $publicKey; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $publicKey_4x; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $productSimulator; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $promotionProductExtra; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var float |
||
| 59 | */ |
||
| 60 | protected $minAmount; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var float |
||
| 64 | */ |
||
| 65 | protected $maxAmount; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var float |
||
| 69 | */ |
||
| 70 | protected $minAmount4x; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var float |
||
| 74 | */ |
||
| 75 | protected $maxAmount4x; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Product |
||
| 79 | */ |
||
| 80 | protected $product; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $minInstallments; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $priceSelector; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $quantitySelector; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var String |
||
| 99 | */ |
||
| 100 | protected $positionSelector; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var Registry |
||
| 104 | */ |
||
| 105 | protected $registry; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Config |
||
|
|
|||
| 109 | */ |
||
| 110 | protected $extraConfig; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var String |
||
| 114 | */ |
||
| 115 | protected $simulatorType; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var String |
||
| 119 | */ |
||
| 120 | protected $store; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var Boolean |
||
| 124 | */ |
||
| 125 | protected $promoted; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var String |
||
| 129 | */ |
||
| 130 | protected $promotedMessage; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var String |
||
| 134 | */ |
||
| 135 | protected $thousandSeparator; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var String |
||
| 139 | */ |
||
| 140 | protected $decimalSeparator; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var String |
||
| 144 | */ |
||
| 145 | protected $destinationSim; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Simulator constructor. |
||
| 149 | * |
||
| 150 | * @param Context $context |
||
| 151 | * @param Registry $registry |
||
| 152 | * @param ExtraConfig $extraConfig |
||
| 153 | * @param Resolver $store |
||
| 154 | * @param array $data |
||
| 155 | */ |
||
| 156 | public function __construct( |
||
| 157 | Context $context, |
||
| 158 | Registry $registry, |
||
| 159 | ExtraConfig $extraConfig, |
||
| 160 | Resolver $store, |
||
| 161 | array $data = [] |
||
| 162 | ) { |
||
| 163 | parent::__construct($context, $data); |
||
| 164 | $this->registry = $registry; |
||
| 165 | $this->store = $store; |
||
| 166 | /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ |
||
| 167 | $scopeConfig = $this->_scopeConfig; |
||
| 168 | $config = $scopeConfig->getValue('payment/pagantis'); |
||
| 169 | |||
| 170 | $this->enabled = $config['active']; |
||
| 171 | $this->enabled_12x = $config['active_12x']; |
||
| 172 | $this->enabled_4x = $config['active_4x']; |
||
| 173 | $this->publicKey = isset($config['pagantis_public_key']) ? $config['pagantis_public_key'] : ''; |
||
| 174 | $this->publicKey_4x = isset($config['pagantis_public_key_4x']) ? $config['pagantis_public_key_4x'] : ''; |
||
| 175 | $this->productSimulator = $config['product_simulator']; |
||
| 176 | |||
| 177 | $this->extraConfig = $extraConfig->getExtraConfig(); |
||
| 178 | $this->minAmount = $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT']; |
||
| 179 | $this->maxAmount = $this->extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT']; |
||
| 180 | $this->minAmount4x = $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT_4x']; |
||
| 181 | $this->maxAmount4x = $this->extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT_4x']; |
||
| 182 | $this->minInstallments = $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS']; |
||
| 183 | $this->priceSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']; |
||
| 184 | $this->quantitySelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']; |
||
| 185 | $this->positionSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']; |
||
| 186 | $this->simulatorType = $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE']; |
||
| 187 | $this->promotedMessage = $this->extraConfig['PAGANTIS_PROMOTION_EXTRA']; |
||
| 188 | $this->thousandSeparator = $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR']; |
||
| 189 | $this->decimalSeparator = $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR']; |
||
| 190 | $this->destinationSim = $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SITUATION']; |
||
| 191 | |||
| 192 | $this->promoted = $this->isProductInPromotion(); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getSimulatorMessage() |
||
| 199 | { |
||
| 200 | return sprintf(__("or 4 installments of %s€, without fees, with "), $this->getFinalPrice4x()); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getLocale() |
||
| 207 | { |
||
| 208 | return strstr($this->store->getLocale(), '_', true); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getCountry() |
||
| 215 | { |
||
| 216 | return strstr($this->store->getLocale(), '_', true); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param $locale |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function getAllowedCountry($locale) |
||
| 225 | { |
||
| 226 | $locale = strtolower($locale); |
||
| 227 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
| 228 | return (in_array(strtolower($locale), $allowedCountries)); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Product |
||
| 233 | */ |
||
| 234 | protected function getProduct() |
||
| 235 | { |
||
| 236 | if (is_null($this->product)) { |
||
| 237 | $this->product = $this->registry->registry('product'); |
||
| 238 | } |
||
| 239 | |||
| 240 | return $this->product; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function isEnabled12x() |
||
| 247 | { |
||
| 248 | return ($this->enabled && $this->enabled_12x); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function isEnabled4x() |
||
| 255 | { |
||
| 256 | return ($this->enabled && $this->enabled_4x); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getPublicKey() |
||
| 263 | { |
||
| 264 | return $this->publicKey; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getPublicKey4x() |
||
| 271 | { |
||
| 272 | return $this->publicKey_4x; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getPromotionProductExtra() |
||
| 279 | { |
||
| 280 | return $this->promotionProductExtra; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isProductInPromotion() |
||
| 287 | { |
||
| 288 | try { |
||
| 289 | return ($this->getProduct()->getData('pagantis_promoted') === '1') ? 'true' : 'false'; |
||
| 290 | } catch (\Exception $exception) { |
||
| 291 | return 'false'; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return float |
||
| 297 | */ |
||
| 298 | public function getFinalPrice() |
||
| 299 | { |
||
| 300 | return $this->getProduct()->getFinalPrice(); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return float |
||
| 305 | */ |
||
| 306 | public function getFinalPrice4x() |
||
| 307 | { |
||
| 308 | return number_format($this->getProduct()->getFinalPrice()/4, 2); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array|false|string |
||
| 313 | */ |
||
| 314 | public function getProductSimulator() |
||
| 315 | { |
||
| 316 | return $this->productSimulator; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param int $productSimulator |
||
| 321 | */ |
||
| 322 | public function setProductSimulator($productSimulator) |
||
| 323 | { |
||
| 324 | $this->productSimulator = $productSimulator; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return float |
||
| 329 | */ |
||
| 330 | public function getMinAmount() |
||
| 331 | { |
||
| 332 | return $this->minAmount; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param float $minAmount |
||
| 337 | */ |
||
| 338 | public function setMinAmount($minAmount) |
||
| 339 | { |
||
| 340 | $this->minAmount = $minAmount; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return float |
||
| 345 | */ |
||
| 346 | public function getMaxAmount() |
||
| 347 | { |
||
| 348 | return $this->maxAmount; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param float $maxAmount |
||
| 353 | */ |
||
| 354 | public function setMaxAmount($maxAmount) |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return int |
||
| 361 | */ |
||
| 362 | public function getMinInstallments() |
||
| 363 | { |
||
| 364 | return $this->minInstallments; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param int $minInstallments |
||
| 369 | */ |
||
| 370 | public function setMinInstallments($minInstallments) |
||
| 371 | { |
||
| 372 | $this->minInstallments = $minInstallments; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function getPriceSelector() |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $priceSelector |
||
| 385 | */ |
||
| 386 | public function setPriceSelector($priceSelector) |
||
| 387 | { |
||
| 388 | $this->priceSelector = $priceSelector; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getQuantitySelector() |
||
| 395 | { |
||
| 396 | return $this->quantitySelector; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $quantitySelector |
||
| 401 | */ |
||
| 402 | public function setQuantitySelector($quantitySelector) |
||
| 403 | { |
||
| 404 | $this->quantitySelector = $quantitySelector; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return String |
||
| 409 | */ |
||
| 410 | public function getPositionSelector() |
||
| 411 | { |
||
| 412 | return $this->positionSelector; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param String $positionSelector |
||
| 417 | */ |
||
| 418 | public function setPositionSelector($positionSelector) |
||
| 421 | } |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * @return String |
||
| 426 | */ |
||
| 427 | public function getSimulatorType() |
||
| 428 | { |
||
| 429 | return $this->simulatorType; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param String $simulatorType |
||
| 434 | */ |
||
| 435 | public function setSimulatorType($simulatorType) |
||
| 436 | { |
||
| 437 | $this->simulatorType = $simulatorType; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return Boolean |
||
| 442 | */ |
||
| 443 | public function getPromoted() |
||
| 444 | { |
||
| 445 | return $this->promoted; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param Boolean $promoted |
||
| 450 | */ |
||
| 451 | public function setPromoted($promoted) |
||
| 452 | { |
||
| 453 | $this->promoted = $promoted; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return String |
||
| 458 | */ |
||
| 459 | public function getPromotedMessage() |
||
| 460 | { |
||
| 461 | return $this->promotedMessage; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param String $promotedMessage |
||
| 466 | */ |
||
| 467 | public function setPromotedMessage($promotedMessage) |
||
| 468 | { |
||
| 469 | $this->promotedMessage = $promotedMessage; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return String |
||
| 474 | */ |
||
| 475 | public function getThousandSeparator() |
||
| 476 | { |
||
| 477 | return $this->thousandSeparator; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param String $thousandSeparator |
||
| 482 | */ |
||
| 483 | public function setThousandSeparator($thousandSeparator) |
||
| 484 | { |
||
| 485 | $this->thousandSeparator = $thousandSeparator; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return String |
||
| 490 | */ |
||
| 491 | public function getDecimalSeparator() |
||
| 492 | { |
||
| 493 | return $this->decimalSeparator; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param String $decimalSeparator |
||
| 498 | */ |
||
| 499 | public function setDecimalSeparator($decimalSeparator) |
||
| 500 | { |
||
| 501 | $this->decimalSeparator = $decimalSeparator; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return float |
||
| 506 | */ |
||
| 507 | public function getMinAmount4x() |
||
| 508 | { |
||
| 509 | return $this->minAmount4x; |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param float $minAmount4x |
||
| 514 | */ |
||
| 515 | public function setMinAmount4x($minAmount4x) |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return float |
||
| 522 | */ |
||
| 523 | public function getMaxAmount4x() |
||
| 524 | { |
||
| 525 | return $this->maxAmount4x; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param float $maxAmount4x |
||
| 530 | */ |
||
| 531 | public function setMaxAmount4x($maxAmount4x) |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | public function checkValidAmount() |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return bool |
||
| 550 | */ |
||
| 551 | public function checkValidAmount4x() |
||
| 552 | { |
||
| 553 | $maxAmount = $this->getMaxAmount4x(); |
||
| 554 | $minAmount = $this->getMinAmount4x(); |
||
| 555 | $totalPrice = (string) floor($this->getFinalPrice()); |
||
| 556 | |||
| 557 | return ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0')); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return String |
||
| 562 | */ |
||
| 563 | public function getDestinationSim() |
||
| 564 | { |
||
| 565 | return $this->destinationSim; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param String $destinationSim |
||
| 570 | */ |
||
| 571 | public function setDestinationSim($destinationSim) |
||
| 574 | } |
||
| 575 | } |
||
| 576 |
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