1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Observers\ProductObserver |
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 create's the product itself. |
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 ProductObserver 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
|
1 |
|
public function handle(array $row) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
// initialize the row |
51
|
1 |
|
$this->setRow($row); |
52
|
|
|
|
53
|
|
|
// process the functionality and return the row |
54
|
1 |
|
$this->process(); |
55
|
|
|
|
56
|
|
|
// return the processed row |
57
|
1 |
|
return $this->getRow(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Process the observer's business logic. |
62
|
|
|
* |
63
|
|
|
* @return array The processed row |
64
|
|
|
*/ |
65
|
1 |
|
protected function process() |
66
|
|
|
{ |
67
|
|
|
|
68
|
|
|
// query whether or not, we've found a new SKU => means we've found a new product |
69
|
1 |
|
if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) { |
70
|
|
|
return $this->getRow(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// prepare the static entity values |
74
|
1 |
|
$product = $this->initializeProduct($this->prepareAttributes()); |
75
|
|
|
|
76
|
|
|
// insert the entity and set the entity ID |
77
|
1 |
|
$this->setLastEntityId($this->persistProduct($product)); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
82
|
|
|
* |
83
|
|
|
* @return array The prepared attributes |
84
|
|
|
*/ |
85
|
1 |
|
protected function prepareAttributes() |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
// prepare the date format for the created at/updated at dates |
89
|
1 |
|
$createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
90
|
1 |
|
$updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
91
|
|
|
|
92
|
|
|
// load the product's attribute set ID |
93
|
1 |
|
$attributeSet = $this->getAttributeSetByAttributeSetName($this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE)); |
94
|
1 |
|
$attributeSetId = $attributeSet[MemberNames::ATTRIBUTE_SET_ID]; |
95
|
1 |
|
$this->setAttributeSet($attributeSet); |
96
|
|
|
|
97
|
|
|
// initialize the product values |
98
|
1 |
|
$sku = $this->getValue(ColumnKeys::SKU); |
99
|
1 |
|
$productType = $this->getValue(ColumnKeys::PRODUCT_TYPE); |
100
|
|
|
|
101
|
|
|
// return the prepared product |
102
|
1 |
|
return $this->initializeEntity( |
103
|
|
|
array( |
104
|
1 |
|
MemberNames::SKU => $sku, |
105
|
1 |
|
MemberNames::CREATED_AT => $createdAt, |
106
|
1 |
|
MemberNames::UPDATED_AT => $updatedAt, |
107
|
1 |
|
MemberNames::HAS_OPTIONS => 0, |
108
|
1 |
|
MemberNames::REQUIRED_OPTIONS => 0, |
109
|
1 |
|
MemberNames::TYPE_ID => $productType, |
110
|
1 |
|
MemberNames::ATTRIBUTE_SET_ID => $attributeSetId |
111
|
1 |
|
) |
112
|
1 |
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Initialize the product with the passed attributes and returns an instance. |
117
|
|
|
* |
118
|
|
|
* @param array $attr The product attributes |
119
|
|
|
* |
120
|
|
|
* @return array The initialized product |
121
|
|
|
*/ |
122
|
|
|
protected function initializeProduct(array $attr) |
123
|
|
|
{ |
124
|
|
|
return $attr; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Persist's the passed product data and return's the ID. |
129
|
|
|
* |
130
|
|
|
* @param array $product The product data to persist |
131
|
|
|
* |
132
|
|
|
* @return string The ID of the persisted entity |
133
|
|
|
*/ |
134
|
1 |
|
protected function persistProduct($product) |
135
|
|
|
{ |
136
|
1 |
|
return $this->getSubject()->persistProduct($product); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set's the attribute set of the product that has to be created. |
141
|
|
|
* |
142
|
|
|
* @param array $attributeSet The attribute set |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
1 |
|
protected function setAttributeSet(array $attributeSet) |
147
|
|
|
{ |
148
|
1 |
|
$this->getSubject()->setAttributeSet($attributeSet); |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return's the attribute set of the product that has to be created. |
153
|
|
|
* |
154
|
|
|
* @return array The attribute set |
155
|
|
|
*/ |
156
|
|
|
protected function getAttributeSet() |
157
|
|
|
{ |
158
|
|
|
$this->getSubject()->getAttributeSet(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return's the attribute set with the passed attribute set name. |
163
|
|
|
* |
164
|
|
|
* @param string $attributeSetName The name of the requested attribute set |
165
|
|
|
* |
166
|
|
|
* @return array The attribute set data |
167
|
|
|
*/ |
168
|
1 |
|
protected function getAttributeSetByAttributeSetName($attributeSetName) |
169
|
|
|
{ |
170
|
1 |
|
return $this->getSubject()->getAttributeSetByAttributeSetName($attributeSetName); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set's the ID of the product that has been created recently. |
175
|
|
|
* |
176
|
|
|
* @param string $lastEntityId The entity ID |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
1 |
|
protected function setLastEntityId($lastEntityId) |
181
|
|
|
{ |
182
|
1 |
|
$this->getSubject()->setLastEntityId($lastEntityId); |
183
|
1 |
|
} |
184
|
|
|
} |
185
|
|
|
|