Cron   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 159
Duplicated Lines 8.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 13
loc 159
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A export() 0 22 3
A _getProductsForExport() 0 18 2
A _setExportedProducts() 0 4 1
A _getExportedProducts() 0 4 1
A _updateDatabase() 13 13 3
A _getItemModel() 0 7 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
namespace Richdynamix\PersonalisedProducts\Model;
3
4
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient\Client;
5
use \Richdynamix\PersonalisedProducts\Model\ResourceModel\Export\Collection as ExportCollection;
6
use \Richdynamix\PersonalisedProducts\Model\ResourceModel\Export\CollectionFactory;
7
use \Richdynamix\PersonalisedProducts\Model\ExportFactory;
8
use \Richdynamix\PersonalisedProducts\Api\Data\ExportInterface;
9
use \Richdynamix\PersonalisedProducts\Logger\PersonalisedProductsLogger;
10
11
/**
12
 * Class Cron
13
 *
14
 * @category  Richdynamix
15
 * @package   PersonalisedProducts
16
 * @author    Steven Richardson ([email protected]) @mage_gizmo
17
 */
18
class Cron
19
{
20
    /**
21
     * @var array
22
     */
23
    private $_exportedProducts = [];
24
25
    /**
26
     * @var Client
27
     */
28
    private $_eventClient;
29
30
    /**
31
     * @var ExportCollection
32
     */
33
    private $_exportCollection;
34
35
    /**
36
     * @var CollectionFactory
37
     */
38
    protected $_ecFactory;
39
40
    /**
41
     * @var \Richdynamix\PersonalisedProducts\Model\ExportFactory
42
     */
43
    private $_exportFactory;
44
45
    /**
46
     * @var PersonalisedProductsLogger
47
     */
48
    private $_logger;
49
50
    /**
51
     * Cron constructor.
52
     * @param Client $client
53
     * @param ExportCollection $exportCollection
54
     * @param CollectionFactory $ecFactory
55
     * @param PersonalisedProductsLogger $logger
56
     */
57
    public function __construct(
58
        Client $client,
59
        ExportCollection $exportCollection,
60
        CollectionFactory $ecFactory,
61
        ExportFactory $_exportFactory,
62
        PersonalisedProductsLogger $logger
63
    ) {
64
        $this->_eventClient = $client;
65
        $this->_exportCollection = $exportCollection;
66
        $this->_ecFactory = $ecFactory;
67
        $this->_exportFactory = $_exportFactory;
68
        $this->_logger = $logger;
69
    }
70
71
    /**
72
     * Method called on the cron to do the export
73
     *
74
     * @return bool
75
     */
76
    public function export()
77
    {
78
        $productExport = $this->_getProductsForExport();
79
        $productCount = count($productExport);
80
        $exportCount = 0;
81
82
        foreach ($productExport as $productId => $categories) {
83
            try {
84
                $this->_eventClient->saveProductData($productId, $categories);
85
                $this->_setExportedProducts($productId);
86
                ++$exportCount;
87
            } catch (\Exception $e) {
88
                $this->_logger->addCritical("Product ID - $productId failed to export: " . $e);
89
                return false;
90
            }
91
        }
92
93
        $this->_updateDatabase();
94
95
        $this->_logger->addInfo("Successfully exported " . $exportCount . " out " . $productCount . " products ");
96
        return true;
97
    }
98
99
    /**
100
     * Get product and category collections for export
101
     *
102
     * @return array
103
     */
104
    private function _getProductsForExport()
105
    {
106
        $productExport = $this->_ecFactory
107
            ->create()
108
            ->addFieldToFilter('is_exported', '0')
109
            ->addOrder(
110
                ExportInterface::CREATION_TIME,
111
                ExportCollection::SORT_ORDER_DESC
112
            );
113
114
        $products = [];
115
        foreach ($productExport as $export) {
116
            $products[$export->getProductId()] = $export->getCategoryIds();
117
        }
118
119
        return $products;
120
121
    }
122
123
    /**
124
     * Set a list of product successfully exported
125
     *
126
     * @param $productId
127
     */
128
    private function _setExportedProducts($productId)
129
    {
130
        $this->_exportedProducts[] = $productId;
131
    }
132
133
    /**
134
     * Getter for the exportedProducts to be updated
135
     *
136
     * @return array
137
     */
138
    private function _getExportedProducts()
139
    {
140
        return $this->_exportedProducts;
141
    }
142
143
    /**
144
     * Update the database row with the `is_exported` as 1
145
     */
146 View Code Duplication
    private function _updateDatabase()
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...
147
    {
148
        foreach ($this->_getExportedProducts() as $productId) {
149
            $model = $this->_getItemModel($productId);
150
            $model->setData('is_exported', '1');
0 ignored issues
show
Bug introduced by
The method setData() does not seem to exist on object<Richdynamix\Perso...sedProducts\Model\Cron>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
            try {
152
                $model->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<Richdynamix\Perso...sedProducts\Model\Cron>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
            } catch (\Exception $e) {
154
                $this->_logger->addCritical($e->getMessage());
155
            }
156
157
        }
158
    }
159
160
    /**
161
     * Load the item model by using product ID as identifier.
162
     *
163
     * //todo move this into a proper loadByProductId method on the model.
164
     *
165
     * @param $productId
166
     * @return $this
167
     */
168
    private function _getItemModel($productId)
169
    {
170
        $item = $this->_exportFactory->create()->getCollection()
171
            ->addFieldToFilter('product_id', $productId)->getFirstItem();
172
173
        return $this->_exportFactory->create()->load($item->getId());
174
    }
175
176
}
177