Completed
Push — master ( fb9075...7e65e5 )
by Tim
9s
created

ProductInventoryObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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
     * Process the observer's business logic.
41
     *
42
     * @return array The processed row
43
     */
44
    protected function process()
45
    {
46
47
        // query whether or not, we've found a new SKU => means we've found a new product
48
        if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) {
49
            return;
50
        }
51
52
        // prepare, initialize and persist the stock status/item
53
        $this->persistStockStatus($this->initializeStockStatus($this->prepareStockStatusAttributes()));
54
        $this->persistStockItem($this->initializeStockItem($this->prepareStockItemAttributes()));
55
    }
56
57
    /**
58
     * Prepare the stock status attributes of the entity that has to be persisted.
59
     *
60
     * @return array The prepared stock status attributes
61
     */
62
    protected function prepareStockStatusAttributes()
63
    {
64
65
        // load the ID of the product that has been created recently
66
        $lastEntityId = $this->getLastEntityId();
67
68
        // initialize the stock status data
69
        $websiteId =  $this->getValue(ColumnKeys::WEBSITE_ID);
70
        $qty = $this->castValueByBackendType('float', $this->getValue(ColumnKeys::QTY));
71
72
        // return the prepared stock status
73
        return $this->initializeEntity(
74
            array(
75
                MemberNames::PRODUCT_ID   => $lastEntityId,
76
                MemberNames::WEBSITE_ID   => $websiteId,
77
                MemberNames::STOCK_ID     => 1,
78
                MemberNames::STOCK_STATUS => $qty > 0 ? 1 : 0,
79
                MemberNames::QTY          => $qty
80
            )
81
        );
82
    }
83
84
    /**
85
     * Initialize the stock status with the passed attributes and returns an instance.
86
     *
87
     * @param array $attr The stock status attributes
88
     *
89
     * @return array The initialized stock status
90
     */
91
    protected function initializeStockStatus(array $attr)
92
    {
93
        return $attr;
94
    }
95
96
    /**
97
     * Prepare the stock item attributes of the entity that has to be persisted.
98
     *
99
     * @return array The prepared stock status item
100
     */
101
    protected function prepareStockItemAttributes()
102
    {
103
104
        // load the ID of the product that has been created recently
105
        $lastEntityId = $this->getLastEntityId();
106
107
        // initialize the stock status data
108
        $websiteId =  $this->getValue(ColumnKeys::WEBSITE_ID);
109
110
        // initialize the stock item with the basic data
111
        $stockItem = $this->initializeEntity(
112
            array(
113
                MemberNames::PRODUCT_ID  => $lastEntityId,
114
                MemberNames::WEBSITE_ID  => $websiteId,
115
                MemberNames::STOCK_ID    => 1
116
            )
117
        );
118
119
        // append the row values to the stock item
120
        $headerStockMappings = $this->getHeaderStockMappings();
121
        foreach ($headerStockMappings as $columnName => $header) {
122
            list ($headerName, $backendType) = $header;
123
            $stockItem[$columnName] = $this->castValueByBackendType($backendType, $this->getValue($headerName));
124
        }
125
126
        // return the prepared stock item
127
        return $stockItem;
128
    }
129
130
    /**
131
     * Initialize the stock item with the passed attributes and returns an instance.
132
     *
133
     * @param array $attr The stock item attributes
134
     *
135
     * @return array The initialized stock item
136
     */
137
    protected function initializeStockItem(array $attr)
138
    {
139
        return $attr;
140
    }
141
142
    /**
143
     * Persist's the passed stock item data and return's the ID.
144
     *
145
     * @param array $stockItem The stock item data to persist
146
     *
147
     * @return void
148
     */
149
    protected function persistStockItem($stockItem)
150
    {
151
        $this->getSubject()->persistStockItem($stockItem);
152
    }
153
154
    /**
155
     * Persist's the passed stock status data and return's the ID.
156
     *
157
     * @param array $stockStatus The stock status data to persist
158
     *
159
     * @return void
160
     */
161
    protected function persistStockStatus($stockStatus)
162
    {
163
        $this->getSubject()->persistStockStatus($stockStatus);
164
    }
165
166
    /**
167
     * Return's the appings for the table column => CSV column header.
168
     *
169
     * @return array The header stock mappings
170
     */
171
    protected function getHeaderStockMappings()
172
    {
173
        return $this->getSubject()->getHeaderStockMappings();
174
    }
175
}
176