AbstractProductCommand::_sendProductData()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
3
namespace Richdynamix\PersonalisedProducts\Console\Command;
4
5
use \Symfony\Component\Console\Command\Command;
6
use \Magento\Catalog\Model\ProductFactory;
7
use \Magento\Framework\App\State as AppState;
8
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient\Client;
9
use \Symfony\Component\Config\Definition\Exception\Exception;
10
use \Richdynamix\PersonalisedProducts\Model\Export;
11
use \Richdynamix\PersonalisedProducts\Model\ExportFactory;
12
use \Richdynamix\PersonalisedProducts\Logger\PersonalisedProductsLogger;
13
14
/**
15
 * Class AbstractProductCommand
16
 *
17
 * @category  Richdynamix
18
 * @package   PersonalisedProducts
19
 * @author    Steven Richardson ([email protected]) @mage_gizmo
20
 */
21
abstract class AbstractProductCommand extends Command
22
{
23
24
    /**
25
     * Product visibility value
26
     */
27
    const CATALOG_SEARCH_VISIBILITY = 4;
28
29
    /**
30
     * @var ProductFactory
31
     */
32
    private $_productFactory;
33
34
    /**
35
     * @var Client
36
     */
37
    private $_eventClient;
38
39
    /**
40
     * @var Export
41
     */
42
    private $_export;
43
44
    /**
45
     * @var ExportFactory
46
     */
47
    private $_exportFactory;
48
49
    /**
50
     * @var PersonalisedProductsLogger
51
     */
52
    private $_logger;
53
54
    /**
55
     * AbstractProductCommand constructor.
56
     * @param ProductFactory $productFactory
57
     * @param Client $eventClient
58
     * @param AppState $appState
59
     */
60
    public function __construct(
61
        ProductFactory $productFactory,
62
        Client $eventClient,
63
        Export $export,
64
        ExportFactory $exportFactory,
65
        AppState $appState,
66
        PersonalisedProductsLogger $logger
67
    ) {
68
        $this->_productFactory = $productFactory;
69
        $this->_eventClient = $eventClient;
70
        $this->_export = $export;
71
        $this->_exportFactory = $exportFactory;
72
        $this->_logger = $logger;
73
        try {
74
            $appState->setAreaCode('adminhtml');
75
        } catch (\Exception $e) {
76
            $this->_logger->addCritical($e->getMessage());
77
        };
78
        parent::__construct();
79
    }
80
81
    /**
82
     * Send product data to PredictionIO
83
     *
84
     * @param $collection
85
     * @return int
86
     */
87
    protected function _sendProductData($collection)
88
    {
89
        $collectionCount = count($collection);
90
        $sentProductCount = 0;
91
        foreach ($collection as $productId) {
92
            $sentProduct = $this->_sendToPredictionIO($productId);
93
            $exportItem = $this->_export->saveProductForExport($productId);
94
            $this->_setProductExported($exportItem->getId());
95
96
            if ($sentProduct) {
97
                ++$sentProductCount;
98
            }
99
        }
100
101
        if ($collectionCount != $sentProductCount) {
102
            throw new Exception(
103
                'There was a problem sending the product data, check the log file for more information'
104
            );
105
        }
106
107
        return $sentProductCount;
108
109
    }
110
111
    /**
112
     * Get product collection
113
     *
114
     * @return array
115
     */
116
    protected function _getProductCollection()
117
    {
118
        $product = $this->_productFactory->create();
119
        $collection = $product->getCollection()
120
            ->addAttributeToFilter('visibility', self::CATALOG_SEARCH_VISIBILITY);
121
122
        return $collection->getAllIds();
123
    }
124
125
    /**
126
     * Get category collection for each product
127
     *
128
     * @param $productId
129
     * @return array
130
     */
131
    private function _getProductCategoryCollection($productId)
132
    {
133
        $product = $this->_productFactory->create();
134
        $product->load($productId);
135
136
        return $product->getCategoryIds();
137
    }
138
139
140
    /**
141
     * Send the new product to PredictionIO
142
     *
143
     * @param $productId
144
     * @return bool
145
     */
146
    private function _sendToPredictionIO($productId)
147
    {
148
        return $this->_eventClient->saveProductData(
149
            $productId,
150
            $this->_getProductCategoryCollection($productId)
151
        );
152
    }
153
154
    /**
155
     * Mark product as exported in DB
156
     *
157
     * @param $exportId
158
     */
159 View Code Duplication
    private function _setProductExported($exportId)
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...
160
    {
161
        $export = $this->_exportFactory->create()->load($exportId);
162
        $export->setData('is_exported', '1');
163
        try {
164
            $export->save();
165
        } catch (\Exception $e) {
166
            $this->_logger->addCritical($e->getMessage());
167
        }
168
    }
169
170
}
171