TargetCrosssell::getItemCollection()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 6
nop 0
1
<?php
2
3
namespace Richdynamix\PersonalisedProducts\Block\Checkout\Cart;
4
5
use \Magento\Catalog\Block\Product\Context;
6
use \Magento\TargetRule\Model\ResourceModel\Index;
7
use \Magento\TargetRule\Helper\Data;
8
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
9
use \Magento\Catalog\Model\Product\Visibility;
10
use \Magento\CatalogInventory\Helper\Stock;
11
use \Magento\Checkout\Model\Session;
12
use \Magento\Catalog\Model\Product\LinkFactory;
13
use \Magento\TargetRule\Model\IndexFactory;
14
use \Magento\Catalog\Model\ProductTypes\ConfigInterface;
15
use \Magento\Catalog\Api\ProductRepositoryInterface;
16
use \Richdynamix\PersonalisedProducts\Helper\Config;
17
use \Magento\TargetRule\Block\Checkout\Cart\Crosssell;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Richdynamix\Personalised...Checkout\Cart\Crosssell.

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...
18
use \Richdynamix\PersonalisedProducts\Model\Frontend\Checkout\Cart\Crosssell as CrosssellModel;
19
use \Magento\Catalog\Model\ProductFactory as ProductFactory;
20
use \Magento\Framework\Module\Manager as Manager;
21
22
/**
23
 * Class TargetCrosssell
24
 *
25
 * @category  Richdynamix
26
 * @package   PersonalisedProducts
27
 * @author    Steven Richardson ([email protected]) @mage_gizmo
28
 */
29
class TargetCrosssell extends Crosssell
30
{
31
    /**
32
     * @var Config
33
     */
34
    private $_config;
35
    /**
36
     * @var CrosssellModel
37
     */
38
    private $_crosssell;
39
    /**
40
     * @var ProductFactory
41
     */
42
    private $_productFactory;
43
    /**
44
     * @var Manager
45
     */
46
    private $_moduleManager;
47
48
    /**
49
     * TargetCrosssell constructor.
50
     * @param Context $context
51
     * @param Index $index
52
     * @param Data $targetRuleData
53
     * @param CollectionFactory $productCollectionFactory
54
     * @param Visibility $visibility
55
     * @param Stock $stockHelper
56
     * @param Session $session
57
     * @param LinkFactory $productLinkFactory
58
     * @param IndexFactory $indexFactory
59
     * @param ConfigInterface $productTypeConfig
60
     * @param ProductRepositoryInterface $productRepository
61
     * @param Config $config
62
     * @param CrosssellModel $crosssell
63
     * @param ProductFactory $productFactory
64
     * @param Manager $moduleManager
65
     * @param array $data
66
     */
67
    public function __construct(
68
        Context $context,
69
        Index $index,
70
        Data $targetRuleData,
71
        CollectionFactory $productCollectionFactory,
72
        Visibility $visibility,
73
        Stock $stockHelper,
74
        Session $session,
75
        LinkFactory $productLinkFactory,
76
        IndexFactory $indexFactory,
77
        ConfigInterface $productTypeConfig,
78
        ProductRepositoryInterface $productRepository,
79
        Config $config,
80
        CrosssellModel $crosssell,
81
        ProductFactory $productFactory,
82
        Manager $moduleManager,
83
        array $data = []
84
    ) {
85
        $this->_config = $config;
86
        $this->_crosssell = $crosssell;
87
        $this->_productFactory = $productFactory;
88
        $this->_moduleManager = $moduleManager;
89
        parent::__construct(
90
            $context,
91
            $index,
92
            $targetRuleData,
93
            $productCollectionFactory,
94
            $visibility,
95
            $stockHelper,
96
            $session,
97
            $productLinkFactory,
98
            $indexFactory,
99
            $productTypeConfig,
100
            $productRepository,
101
            $data
102
        );
103
    }
104
105
    /**
106
     * Get the crossell items for the basket page
107
     * Rewrites the target rules for Enterprise edition
108
     *
109
     * @return array
110
     */
111
    public function getItemCollection()
112
    {
113
        if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::COMPLEMENTARY_REPLACE_RULES)) {
114
            return parent::getItemCollection();
115
        }
116
117
        $personalisedIds = $this->_crosssell->getProductCollection();
118
        if (!$personalisedIds) {
119
            return parent::getItemCollection();
120
        }
121
122
        $collection = $this->_crosssell->getPersonalisedProductCollection($personalisedIds);
123
124
        if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
125
            $this->_addProductAttributesAndPrices($collection);
126
        }
127
128
        $this->_items = [];
129
        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...
130
            $this->_items[] = $item;
131
        }
132
133
        return $this->_items;
134
    }
135
}
136