Total Complexity | 43 |
Total Lines | 450 |
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 string |
||
29 | */ |
||
30 | protected $publicKey; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $productSimulator; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $promotionProductExtra; |
||
41 | |||
42 | /** |
||
43 | * @var float |
||
44 | */ |
||
45 | protected $minAmount; |
||
46 | |||
47 | /** |
||
48 | * @var float |
||
49 | */ |
||
50 | protected $maxAmount; |
||
51 | |||
52 | /** |
||
53 | * @var Product |
||
54 | */ |
||
55 | protected $product; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $minInstallments; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $priceSelector; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $quantitySelector; |
||
71 | |||
72 | /** |
||
73 | * @var String |
||
74 | */ |
||
75 | protected $positionSelector; |
||
76 | |||
77 | /** |
||
78 | * @var Registry |
||
79 | */ |
||
80 | protected $registry; |
||
81 | |||
82 | /** |
||
83 | * @var Config |
||
|
|||
84 | */ |
||
85 | protected $extraConfig; |
||
86 | |||
87 | /** |
||
88 | * @var String |
||
89 | */ |
||
90 | protected $simulatorType; |
||
91 | |||
92 | /** |
||
93 | * @var String |
||
94 | */ |
||
95 | protected $store; |
||
96 | |||
97 | /** |
||
98 | * @var Boolean |
||
99 | */ |
||
100 | protected $promoted; |
||
101 | |||
102 | /** |
||
103 | * @var String |
||
104 | */ |
||
105 | protected $promotedMessage; |
||
106 | |||
107 | /** |
||
108 | * @var String |
||
109 | */ |
||
110 | protected $thousandSeparator; |
||
111 | |||
112 | /** |
||
113 | * @var String |
||
114 | */ |
||
115 | protected $decimalSeparator; |
||
116 | |||
117 | /** |
||
118 | * @var String |
||
119 | */ |
||
120 | protected $separator; |
||
121 | |||
122 | /** |
||
123 | * Simulator constructor. |
||
124 | * |
||
125 | * @param Context $context |
||
126 | * @param Registry $registry |
||
127 | * @param ExtraConfig $extraConfig |
||
128 | * @param Resolver $store |
||
129 | * @param array $data |
||
130 | */ |
||
131 | public function __construct( |
||
132 | Context $context, |
||
133 | Registry $registry, |
||
134 | ExtraConfig $extraConfig, |
||
135 | Resolver $store, |
||
136 | array $data = [] |
||
137 | ) { |
||
138 | parent::__construct($context, $data); |
||
139 | $this->registry = $registry; |
||
140 | $this->store = $store; |
||
141 | /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ |
||
142 | $scopeConfig = $this->_scopeConfig; |
||
143 | $config = $scopeConfig->getValue('payment/pagantis'); |
||
144 | |||
145 | $this->enabled = $config['active']; |
||
146 | $this->publicKey = isset($config['pagantis_public_key']) ? $config['pagantis_public_key'] : ''; |
||
147 | $this->productSimulator = $config['product_simulator']; |
||
148 | $this->extraConfig = $extraConfig->getExtraConfig(); |
||
149 | |||
150 | $this->minAmount = $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT']; |
||
151 | $this->maxAmount = $this->extraConfig['PAGANTIS_DISPLAY_MAX_AMOUNT']; |
||
152 | $this->minInstallments = $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS']; |
||
153 | $this->priceSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']; |
||
154 | $this->quantitySelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']; |
||
155 | $this->positionSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']; |
||
156 | $this->simulatorType = $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE']; |
||
157 | $this->promotedMessage = $this->extraConfig['PAGANTIS_PROMOTION_EXTRA']; |
||
158 | $this->thousandSeparator = $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR']; |
||
159 | $this->decimalSeparator = $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR']; |
||
160 | $this->separator = __('ó'); |
||
161 | |||
162 | $this->promoted = $this->isProductInPromotion(); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getLocale() |
||
169 | { |
||
170 | return strstr($this->store->getLocale(), '_', true); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getCountry() |
||
177 | { |
||
178 | return strstr($this->store->getLocale(), '_', true); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param $locale |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function getAllowedCountry($locale) |
||
187 | { |
||
188 | $locale = strtolower($locale); |
||
189 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
190 | return (in_array(strtolower($locale), $allowedCountries)); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return Product |
||
195 | */ |
||
196 | protected function getProduct() |
||
197 | { |
||
198 | if (is_null($this->product)) { |
||
199 | $this->product = $this->registry->registry('product'); |
||
200 | } |
||
201 | |||
202 | return $this->product; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isEnabled() |
||
209 | { |
||
210 | return $this->enabled; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getPublicKey() |
||
217 | { |
||
218 | return $this->publicKey; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getPromotionProductExtra() |
||
225 | { |
||
226 | return $this->promotionProductExtra; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function isProductInPromotion() |
||
233 | { |
||
234 | try { |
||
235 | return ($this->getProduct()->getData('pagantis_promoted') === '1') ? 'true' : 'false'; |
||
236 | } catch (\Exception $exception) { |
||
237 | return 'false'; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return float |
||
243 | */ |
||
244 | public function getFinalPrice() |
||
245 | { |
||
246 | return $this->getProduct()->getFinalPrice(); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return array|false|string |
||
251 | */ |
||
252 | public function getProductSimulator() |
||
253 | { |
||
254 | return $this->productSimulator; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param int $productSimulator |
||
259 | */ |
||
260 | public function setProductSimulator($productSimulator) |
||
261 | { |
||
262 | $this->productSimulator = $productSimulator; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return float |
||
267 | */ |
||
268 | public function getMinAmount() |
||
269 | { |
||
270 | return $this->minAmount; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param float $minAmount |
||
275 | */ |
||
276 | public function setMinAmount($minAmount) |
||
277 | { |
||
278 | $this->minAmount = $minAmount; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @return float |
||
283 | */ |
||
284 | public function getMaxAmount() |
||
285 | { |
||
286 | return $this->maxAmount; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param float $maxAmount |
||
291 | */ |
||
292 | public function setMaxAmount($maxAmount) |
||
293 | { |
||
294 | $this->maxAmount = $maxAmount; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @return int |
||
299 | */ |
||
300 | public function getMinInstallments() |
||
301 | { |
||
302 | return $this->minInstallments; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param int $minInstallments |
||
307 | */ |
||
308 | public function setMinInstallments($minInstallments) |
||
309 | { |
||
310 | $this->minInstallments = $minInstallments; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getPriceSelector() |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param string $priceSelector |
||
323 | */ |
||
324 | public function setPriceSelector($priceSelector) |
||
325 | { |
||
326 | $this->priceSelector = $priceSelector; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getQuantitySelector() |
||
333 | { |
||
334 | return $this->quantitySelector; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param string $quantitySelector |
||
339 | */ |
||
340 | public function setQuantitySelector($quantitySelector) |
||
341 | { |
||
342 | $this->quantitySelector = $quantitySelector; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return String |
||
347 | */ |
||
348 | public function getPositionSelector() |
||
349 | { |
||
350 | return $this->positionSelector; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @param String $positionSelector |
||
355 | */ |
||
356 | public function setPositionSelector($positionSelector) |
||
359 | } |
||
360 | |||
361 | |||
362 | /** |
||
363 | * @return String |
||
364 | */ |
||
365 | public function getSimulatorType() |
||
366 | { |
||
367 | return $this->simulatorType; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param String $simulatorType |
||
372 | */ |
||
373 | public function setSimulatorType($simulatorType) |
||
374 | { |
||
375 | $this->simulatorType = $simulatorType; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @return Boolean |
||
380 | */ |
||
381 | public function getPromoted() |
||
382 | { |
||
383 | return $this->promoted; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param Boolean $promoted |
||
388 | */ |
||
389 | public function setPromoted($promoted) |
||
390 | { |
||
391 | $this->promoted = $promoted; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @return String |
||
396 | */ |
||
397 | public function getPromotedMessage() |
||
398 | { |
||
399 | return $this->promotedMessage; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @param String $promotedMessage |
||
404 | */ |
||
405 | public function setPromotedMessage($promotedMessage) |
||
406 | { |
||
407 | $this->promotedMessage = $promotedMessage; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @return String |
||
412 | */ |
||
413 | public function getThousandSeparator() |
||
414 | { |
||
415 | return $this->thousandSeparator; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param String $thousandSeparator |
||
420 | */ |
||
421 | public function setThousandSeparator($thousandSeparator) |
||
422 | { |
||
423 | $this->thousandSeparator = $thousandSeparator; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @return String |
||
428 | */ |
||
429 | public function getDecimalSeparator() |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param String $decimalSeparator |
||
436 | */ |
||
437 | public function setDecimalSeparator($decimalSeparator) |
||
438 | { |
||
439 | $this->decimalSeparator = $decimalSeparator; |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @return String |
||
444 | */ |
||
445 | public function getSeparator() |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @param String $separator |
||
452 | */ |
||
453 | public function setSeparator($separator) |
||
454 | { |
||
455 | $this->separator = $separator; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function checkValidAmount() |
||
462 | { |
||
463 | $maxAmount = $this->getMaxAmount(); |
||
464 | $minAmount = $this->getMinAmount(); |
||
465 | $totalPrice = (string) floor($this->getFinalPrice()); |
||
466 | |||
467 | return ($totalPrice>=$minAmount && ($totalPrice<=$maxAmount||$maxAmount=='0')); |
||
468 | } |
||
469 | } |
||
470 |
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