TargetUpsell::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Richdynamix\PersonalisedProducts\Block\Product\ProductList;
4
5
use \Magento\TargetRule\Block\Catalog\Product\ProductList\Upsell;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Richdynamix\Personalised...duct\ProductList\Upsell.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use \Magento\Catalog\Block\Product\Context;
7
use \Magento\TargetRule\Model\ResourceModel\Index;
8
use \Magento\TargetRule\Helper\Data;
9
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
10
use \Magento\Catalog\Model\Product\Visibility;
11
use \Magento\TargetRule\Model\IndexFactory;
12
use \Magento\Framework\Module\Manager as Manager;
13
use \Magento\Checkout\Model\Cart;
14
use \Richdynamix\PersonalisedProducts\Helper\Config;
15
use \Richdynamix\PersonalisedProducts\Model\Frontend\Catalog\Product\ProductList\Upsell as UpsellModel;
16
17
/**
18
 * Rewrite target rule product upsell block in enterprise edition
19
 * to switch out product collection for one returned from PredictionIO
20
 *
21
 * @category  Richdynamix
22
 * @package   PersonalisedProducts
23
 * @author    Steven Richardson ([email protected]) @mage_gizmo
24
 */
25
class TargetUpsell extends Upsell
26
{
27
    /**
28
     * @var Cart
29
     */
30
    protected $_cart;
31
    /**
32
     * @var Config
33
     */
34
    protected $_config;
35
    /**
36
     * @var UpsellModel
37
     */
38
    protected $_upsell;
39
    /**
40
     * @var Manager
41
     */
42
    protected $_moduleManager;
43
44
    /**
45
     * @param Context $context
46
     * @param Index $index
47
     * @param Data $targetRuleData
48
     * @param CollectionFactory $productCollectionFactory
49
     * @param Visibility $visibility
50
     * @param IndexFactory $indexFactory
51
     * @param Cart $cart
52
     * @param Config $config
53
     * @param UpsellModel $upsell
54
     * @param Manager $moduleManager
55
     */
56 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
        Context $context,
58
        Index $index,
59
        Data $targetRuleData,
60
        CollectionFactory $productCollectionFactory,
61
        Visibility $visibility,
62
        IndexFactory $indexFactory,
63
        Cart $cart,
64
        Config $config,
65
        UpsellModel $upsell,
66
        Manager $moduleManager
67
    ) {
68
        $this->_cart = $cart;
69
        $this->_config = $config;
70
        $this->_upsell = $upsell;
71
        $this->_moduleManager = $moduleManager;
72
        parent::__construct(
73
            $context,
74
            $index,
75
            $targetRuleData,
76
            $productCollectionFactory,
77
            $visibility,
78
            $indexFactory,
79
            $cart
80
        );
81
    }
82
83
    /**
84
     * Rewrite parent getAllItems method to use PredictionIO results when available
85
     * Rewrites the target rules for Enterprise edition
86
     *
87
     * @return array
88
     */
89
    public function getAllItems()
90
    {
91
        if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::SIMILARITY_REPLACE_RULES)) {
92
            return parent::getAllItems();
93
        }
94
95
        $product = $this->_coreRegistry->registry('product');
96
        $categoryIds = $this->_upsell->getCategoryIds($product);
97
        $personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds);
98
99
        if (!$personalisedIds) {
100
            return parent::getAllItems();
101
        }
102
103
        $collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds);
104
105
        if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
106
            $this->_addProductAttributesAndPrices($collection);
107
        }
108
109
        $items = [];
110
        foreach ($collection as $product) {
0 ignored issues
show
Bug introduced by
The expression $collection of type object<Richdynamix\Perso...uct\ProductList\Upsell> is not traversable.
Loading history...
111
            $product->setDoNotUseCategoryId(true);
112
            $items[] = $product;
113
        }
114
115
        return $items;
116
    }
117
}
118