Completed
Push — develop ( f7dfad...117a7f )
by Steven
04:48
created

Crosssell   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 5.41 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 13
c 6
b 0
f 3
lcom 2
cbo 1
dl 8
loc 148
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getProductCollection() 0 11 2
A _getPredictedProducts() 0 9 2
A _getProductIds() 0 10 3
A getPersonalisedProductCollection() 8 8 1
A _getCartProductIds() 0 13 3
A getQuote() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Richdynamix\PersonalisedProducts\Model\Frontend\Checkout\Cart;
4
5
use Magento\Catalog\Model\Product\Visibility;
6
use \Magento\Catalog\Model\ProductFactory as ProductFactory;
7
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EngineClient\Complementary;
8
use \Magento\Customer\Model\Session as CustomerSession;
9
use \Magento\Checkout\Model\Session;
10
11
/**
12
 * Class Crosssell
13
 *
14
 * @category  Richdynamix
15
 * @package   PersonalisedProducts
16
 * @author    Steven Richardson ([email protected]) @mage_gizmo
17
 */
18
class Crosssell
19
{
20
    /**
21
     * @var Complementary
22
     */
23
    private $_complementaryEngine;
24
25
    /**
26
     * @var CustomerSession
27
     */
28
    private $_customerSession;
29
30
    /**
31
     * @var array
32
     */
33
    private $_basketProducts;
34
35
    /**
36
     * @var ProductFactory
37
     */
38
    private $_productFactory;
39
40
    /**
41
     * @var array
42
     */
43
    protected $_products;
44
45
    /**
46
     * @var Session
47
     */
48
    protected $_checkoutSession;
49
50
    /**
51
     * Crosssell constructor.
52
     * @param Complementary $complementaryEngine
53
     * @param CustomerSession $customerSession
54
     * @param ProductFactory $productFactory
55
     * @param Visibility $visibility
56
     * @param Session $session
57
     */
58
    public function __construct(
59
        Complementary $complementaryEngine,
60
        CustomerSession $customerSession,
61
        ProductFactory $productFactory,
62
        Visibility $visibility,
0 ignored issues
show
Unused Code introduced by
The parameter $visibility is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
        Session $session
64
    ) {
65
        $this->_complementaryEngine = $complementaryEngine;
66
        $this->_customerSession = $customerSession;
67
        $this->_productFactory = $productFactory;
68
        $this->_checkoutSession = $session;
69
    }
70
71
    /**
72
     * Query the PredictionIO engine for product data
73
     *
74
     * @return array|bool
75
     */
76
    public function getProductCollection()
77
    {
78
        $this->_basketProducts = $this->_getCartProductIds();
79
        $products = $this->_complementaryEngine->sendQuery($this->_basketProducts);
80
81
        if ($products['rules']) {
82
            return $this->_getPredictedProducts($products['rules']);
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Loop over each of the rules in the returned data from PredictionIO
90
     *
91
     * @param array $items
92
     * @return array
93
     */
94
    private function _getPredictedProducts(array $items)
95
    {
96
        $productIds = [];
97
        foreach ($items as $item) {
98
            $this->_getProductIds($item['itemScores'], $productIds);
99
        }
100
101
        return $productIds;
102
    }
103
104
    /**
105
     * Build product ID collection array from PredictionIO engine data
106
     *
107
     * @param array $items
108
     * @param $productIds
109
     * @return $this
110
     */
111
    private function _getProductIds(array $items, &$productIds)
112
    {
113
        foreach ($items as $item) {
114
            if (!in_array($item['item'], $this->_basketProducts)) {
115
                $productIds[] = $item['item'];
116
            }
117
        }
118
119
        return $this;
120
    }
121
122
    /**
123
     * Get a new product collection from prediction IO result set
124
     *
125
     * @param array $personalisedIds
126
     * @return $this
127
     */
128 View Code Duplication
    public function getPersonalisedProductCollection(array $personalisedIds)
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...
129
    {
130
        $collection = $this->_productFactory->create()->getCollection()
131
            ->addAttributeToFilter('entity_id', ['in', $personalisedIds])
132
            ->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH)
133
            ->addAttributeToFilter('status', array('eq' => 1));
134
        return $collection;
135
    }
136
137
    /**
138
     * Get all product ids in the cart
139
     *
140
     * @return array
141
     */
142
    private function _getCartProductIds()
143
    {
144
        if ($this->_products === null) {
145
            $this->_products = [];
146
            foreach ($this->getQuote()->getAllItems() as $quoteItem) {
147
                /* @var $quoteItem \Magento\Quote\Model\Quote\Item */
148
                $product = $quoteItem->getProduct();
149
                $this->_products[] = $product->getEntityId();
150
            }
151
        }
152
153
        return $this->_products;
154
    }
155
156
    /**
157
     * Get the quote from the session
158
     *
159
     * @return \Magento\Quote\Model\Quote
160
     */
161
    public function getQuote()
162
    {
163
        return $this->_checkoutSession->getQuote();
164
    }
165
}
166