Completed
Push — master ( 08c5b9...8b6449 )
by pablo
15s queued 11s
created

Simulator   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 450
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
c 1
b 0
f 0
dl 0
loc 450
rs 8.96
wmc 43

37 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriceSelector() 0 3 1
A setMaxAmount() 0 3 1
A setPositionSelector() 0 3 1
A getCountry() 0 3 1
A getDecimalSeparator() 0 3 1
A getProduct() 0 7 2
A __construct() 0 32 2
A getMinInstallments() 0 3 1
A getPromoted() 0 3 1
A setPromotedMessage() 0 3 1
A getSeparator() 0 3 1
A getLocale() 0 3 1
A getThousandSeparator() 0 3 1
A setPromoted() 0 3 1
A getMaxAmount() 0 3 1
A setProductSimulator() 0 3 1
A getAllowedCountry() 0 5 1
A setDecimalSeparator() 0 3 1
A getProductSimulator() 0 3 1
A getPromotionProductExtra() 0 3 1
A isProductInPromotion() 0 6 3
A getPublicKey() 0 3 1
A isEnabled() 0 3 1
A getFinalPrice() 0 3 1
A getPromotedMessage() 0 3 1
A getQuantitySelector() 0 3 1
A setThousandSeparator() 0 3 1
A getSimulatorType() 0 3 1
A getMinAmount() 0 3 1
A setSimulatorType() 0 3 1
A setQuantitySelector() 0 3 1
A setSeparator() 0 3 1
A setPriceSelector() 0 3 1
A setMinAmount() 0 3 1
A getPositionSelector() 0 3 1
A setMinInstallments() 0 3 1
A checkValidAmount() 0 7 3

How to fix   Complexity   

Complex Class

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
2
3
namespace Pagantis\Pagantis\Block\Product;
4
5
use Magento\Backend\Block\Template\Context;
6
use Magento\Catalog\Model\Category;
7
use Magento\Catalog\Model\Product;
8
use Magento\Framework\Registry;
9
use Magento\Framework\View\Element\Template;
10
use Pagantis\Pagantis\Helper\ExtraConfig;
11
use Magento\Framework\Locale\Resolver;
12
use Zend\Db\Sql\Ddl\Column\Boolean;
13
14
/**
15
 * Class Simulator
16
 * @package Pagantis\Pagantis\Block\Product
17
 */
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
0 ignored issues
show
Bug introduced by pgarces
The type Pagantis\Pagantis\Block\Product\Config was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by pgarces
It seems like $store of type Magento\Framework\Locale\Resolver is incompatible with the declared type string of property $store.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by pgarces
It seems like $extraConfig->getExtraConfig() of type array or array is incompatible with the declared type Pagantis\Pagantis\Block\Product\Config of property $extraConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by pgarces
It seems like $this->isProductInPromotion() of type boolean is incompatible with the declared type Zend\Db\Sql\Ddl\Column\Boolean of property $promoted.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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');
0 ignored issues
show
Deprecated Code introduced by cromero
The function Magento\Framework\Registry::registry() has been deprecated: 102.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

199
            $this->product = /** @scrutinizer ignore-deprecated */ $this->registry->registry('product');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by pgarces
The expression return $this->getProduct... '1' ? 'true' : 'false' returns the type string which is incompatible with the documented return type boolean.
Loading history...
236
        } catch (\Exception $exception) {
237
            return 'false';
0 ignored issues
show
Bug Best Practice introduced by pgarces
The expression return 'false' returns the type string which is incompatible with the documented return type boolean.
Loading history...
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()
317
    {
318
        return $this->priceSelector;
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)
357
    {
358
        $this->positionSelector = $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()
430
    {
431
        return $this->decimalSeparator;
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()
446
    {
447
        return $this->separator;
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