Completed
Pull Request — master (#17)
by Tim
03:38
created

ProductInventoryObserver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 161
Duplicated Lines 10.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 17
loc 161
c 0
b 0
f 0
ccs 0
cts 67
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 17 17 2
A process() 0 7 1
A prepareStockStatusAttributes() 0 21 2
A initializeStockStatus() 0 4 1
B prepareStockItemAttributes() 0 28 2
A initializeStockItem() 0 4 1
A persistStockItem() 0 4 1
A persistStockStatus() 0 4 1
A getHeaderStockMappings() 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
/**
4
 * TechDivision\Import\Product\Observers\ProductInventoryObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Observer that creates/updates the product's inventory.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductInventoryObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param array $row The row to handle
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
46
     */
47 View Code Duplication
    public function handle(array $row)
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...
48
    {
49
50
        // initialize the row
51
        $this->setRow($row);
52
53
        // query whether or not, we've found a new SKU => means we've found a new product
54
        if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) {
55
            return $this->getRow();
56
        }
57
58
        // process the functionality and return the row
59
        $this->process();
60
61
        // return the processed row
62
        return $this->getRow();
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return array The processed row
69
     */
70
    public function process()
71
    {
72
73
        // prepare, initialize and persist the stock status/item
74
        $this->persistStockStatus($this->initializeStockStatus($this->prepareStockStatusAttributes()));
75
        $this->persistStockItem($this->initializeStockItem($this->prepareStockItemAttributes()));
76
    }
77
78
    /**
79
     * Prepare the stock status attributes of the entity that has to be persisted.
80
     *
81
     * @return array The prepared stock status attributes
82
     */
83
    public function prepareStockStatusAttributes()
84
    {
85
86
        // load the ID of the product that has been created recently
87
        $lastEntityId = $this->getLastEntityId();
88
89
        // initialize the stock status data
90
        $websiteId =  $this->getValue(ColumnKeys::WEBSITE_ID);
91
        $qty = $this->castValueByBackendType('float', $this->getValue(ColumnKeys::QTY));
92
93
        // return the prepared stock status
94
        return $this->initializeEntity(
95
            array(
96
                MemberNames::PRODUCT_ID   => $lastEntityId,
97
                MemberNames::WEBSITE_ID   => $websiteId,
98
                MemberNames::STOCK_ID     => 1,
99
                MemberNames::STOCK_STATUS => $qty > 0 ? 1 : 0,
100
                MemberNames::QTY          => $qty
101
            )
102
        );
103
    }
104
105
    /**
106
     * Initialize the stock status with the passed attributes and returns an instance.
107
     *
108
     * @param array $attr The stock status attributes
109
     *
110
     * @return array The initialized stock status
111
     */
112
    public function initializeStockStatus(array $attr)
113
    {
114
        return $attr;
115
    }
116
117
    /**
118
     * Prepare the stock item attributes of the entity that has to be persisted.
119
     *
120
     * @return array The prepared stock status item
121
     */
122
    public function prepareStockItemAttributes()
123
    {
124
125
        // load the ID of the product that has been created recently
126
        $lastEntityId = $this->getLastEntityId();
127
128
        // initialize the stock status data
129
        $websiteId =  $this->getValue(ColumnKeys::WEBSITE_ID);
130
131
        // initialize the stock item with the basic data
132
        $stockItem = $this->initializeEntity(
133
            array(
134
                MemberNames::PRODUCT_ID  => $lastEntityId,
135
                MemberNames::WEBSITE_ID  => $websiteId,
136
                MemberNames::STOCK_ID    => 1
137
            )
138
        );
139
140
        // append the row values to the stock item
141
        $headerStockMappings = $this->getHeaderStockMappings();
142
        foreach ($headerStockMappings as $columnName => $header) {
143
            list ($headerName, $backendType) = $header;
144
            $stockItem[$columnName] = $this->castValueByBackendType($backendType, $this->getValue($headerName));
145
        }
146
147
        // return the prepared stock item
148
        return $stockItem;
149
    }
150
151
    /**
152
     * Initialize the stock item with the passed attributes and returns an instance.
153
     *
154
     * @param array $attr The stock item attributes
155
     *
156
     * @return array The initialized stock item
157
     */
158
    public function initializeStockItem(array $attr)
159
    {
160
        return $attr;
161
    }
162
163
    /**
164
     * Persist's the passed stock item data and return's the ID.
165
     *
166
     * @param array $stockItem The stock item data to persist
167
     *
168
     * @return void
169
     */
170
    public function persistStockItem($stockItem)
171
    {
172
        $this->getSubject()->persistStockItem($stockItem);
173
    }
174
175
    /**
176
     * Persist's the passed stock status data and return's the ID.
177
     *
178
     * @param array $stockStatus The stock status data to persist
179
     *
180
     * @return void
181
     */
182
    public function persistStockStatus($stockStatus)
183
    {
184
        $this->getSubject()->persistStockStatus($stockStatus);
185
    }
186
187
    /**
188
     * Return's the appings for the table column => CSV column header.
189
     *
190
     * @return array The header stock mappings
191
     */
192
    public function getHeaderStockMappings()
193
    {
194
        return $this->getSubject()->getHeaderStockMappings();
195
    }
196
}
197