Crosssell::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 11

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\Checkout\Cart;
4
5
use \Magento\Catalog\Block\Product\Context as Context;
6
use \Richdynamix\PersonalisedProducts\Helper\Config as Config;
7
use \Richdynamix\PersonalisedProducts\Model\Frontend\Checkout\Cart\Crosssell as CrosssellModel;
8
use \Magento\Catalog\Model\ProductFactory as ProductFactory;
9
use \Magento\CatalogInventory\Helper\Stock as StockHelper;
10
use \Magento\Framework\Module\Manager as Manager;
11
use \Magento\Catalog\Model\Product\Visibility;
12
use \Magento\Checkout\Model\Session;
13
use \Magento\Catalog\Model\Product\LinkFactory;
14
use \Magento\Quote\Model\Quote\Item\RelatedProducts;
15
16
/**
17
 * Class Crosssel
18
 *
19
 * @category  Richdynamix
20
 * @package   PersonalisedProducts
21
 * @author    Steven Richardson ([email protected]) @mage_gizmo
22
 */
23
class Crosssell extends \Magento\Checkout\Block\Cart\Crosssell
24
{
25
    /**
26
     * @var Config
27
     */
28
    private $_config;
29
30
    /**
31
     * @var CrosssellModel
32
     */
33
    private $_crosssell;
34
35
    /**
36
     * @var Manager
37
     */
38
    private $_moduleManager;
39
40
    /**
41
     * Crosssell constructor.
42
     * @param Context $context
43
     * @param Config $config
44
     * @param CrosssellModel $crosssell
45
     * @param ProductFactory $productFactory
46
     * @param Manager $moduleManager
47
     * @param Visibility $productVisibility
48
     * @param Session $checkoutSession
49
     * @param LinkFactory $productLinkFactory
50
     * @param RelatedProducts $itemRelationsList
51
     * @param StockHelper $stockHelper
52
     * @param array $data
53
     */
54 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...
55
        Context $context,
56
        Config $config,
57
        CrosssellModel $crosssell,
58
        ProductFactory $productFactory,
59
        Manager $moduleManager,
60
        Visibility $productVisibility,
61
        Session $checkoutSession,
62
        LinkFactory $productLinkFactory,
63
        RelatedProducts $itemRelationsList,
64
        StockHelper $stockHelper,
65
        array $data = []
66
    ) {
67
        $this->_config = $config;
68
        $this->_crosssell = $crosssell;
69
        $this->_productFactory = $productFactory;
70
        $this->_moduleManager = $moduleManager;
71
        parent::__construct(
72
            $context,
73
            $checkoutSession,
74
            $productVisibility,
75
            $productLinkFactory,
76
            $itemRelationsList,
77
            $stockHelper,
78
            $data
79
        );
80
    }
81
82
    /**
83
     * Get the crossell items for the basket page
84
     *
85
     * @return array
86
     */
87
    public function getItems()
88
    {
89
        if (!$this->_config->isEnabled()) {
90
            return parent::getItems();
91
        }
92
93
        $personalisedIds = $this->_crosssell->getProductCollection();
94
        if (!$personalisedIds) {
95
            return parent::getItems();
96
        }
97
98
        $collection = $this->_crosssell->getPersonalisedProductCollection($personalisedIds);
99
        if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
100
            $this->_addProductAttributesAndPrices($collection);
101
        }
102
103
        $items = [];
104
        foreach ($collection as $item) {
0 ignored issues
show
Bug introduced by
The expression $collection of type object<Richdynamix\Perso...heckout\Cart\Crosssell> is not traversable.
Loading history...
105
            $items[] = $item;
106
        }
107
108
        return $items;
109
    }
110
}
111