|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Attribute\Set\Observers\AttributeSetObserver |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7 |
|
7
|
|
|
* |
|
8
|
|
|
* @author Tim Wagner <[email protected]> |
|
9
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT |
|
11
|
|
|
* @link https://github.com/techdivision/import-attribute-set |
|
12
|
|
|
* @link http://www.techdivision.com |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Attribute\Set\Observers; |
|
16
|
|
|
|
|
17
|
|
|
use TechDivision\Import\Dbal\Utils\EntityStatus; |
|
18
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
|
19
|
|
|
use TechDivision\Import\Observers\StateDetectorInterface; |
|
20
|
|
|
use TechDivision\Import\Observers\AttributeLoaderInterface; |
|
21
|
|
|
use TechDivision\Import\Observers\DynamicAttributeObserverInterface; |
|
22
|
|
|
use TechDivision\Import\Observers\EntityMergers\EntityMergerInterface; |
|
23
|
|
|
use TechDivision\Import\Attribute\Set\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Attribute\Set\Utils\MemberNames; |
|
25
|
|
|
use TechDivision\Import\Attribute\Set\Utils\EntityTypeCodes; |
|
26
|
|
|
use TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Observer that create's the EAV attribute set itself. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Tim Wagner <[email protected]> |
|
32
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
33
|
|
|
* @license https://opensource.org/licenses/MIT |
|
34
|
|
|
* @link https://github.com/techdivision/import-attribute-set |
|
35
|
|
|
* @link http://www.techdivision.com |
|
36
|
|
|
*/ |
|
37
|
|
|
class AttributeSetObserver extends AbstractAttributeSetObserver implements DynamicAttributeObserverInterface |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The attribute loader instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @var \TechDivision\Import\Observers\AttributeLoaderInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $attributeLoader; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The entity merger instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $entityMerger; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initialize the dedicated column. |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $columns = array(MemberNames::SORT_ORDER => array(ColumnKeys::SORT_ORDER, BackendTypeKeys::BACKEND_TYPE_INT)); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Initializes the observer with the passed subject instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface $attributeSetBunchProcessor The attribute set bunch processor instance |
|
65
|
|
|
* @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
|
66
|
|
|
* @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface|null $entityMerger The entity merger instance |
|
67
|
|
|
* @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct( |
|
70
|
|
|
AttributeSetBunchProcessorInterface $attributeSetBunchProcessor, |
|
71
|
|
|
AttributeLoaderInterface $attributeLoader = null, |
|
72
|
|
|
EntityMergerInterface $entityMerger = null, |
|
73
|
|
|
StateDetectorInterface $stateDetector = null |
|
74
|
|
|
) { |
|
75
|
|
|
|
|
76
|
|
|
// set the attribute loader |
|
77
|
|
|
$this->attributeLoader = $attributeLoader; |
|
78
|
|
|
$this->entityMerger = $entityMerger; |
|
79
|
|
|
|
|
80
|
|
|
// pass the processor to th eparend constructor |
|
81
|
|
|
parent::__construct($attributeSetBunchProcessor, $stateDetector); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Process the observer's business logic. |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function process() |
|
90
|
|
|
{ |
|
91
|
|
|
|
|
92
|
|
|
// load the entity type code and attribute set name |
|
93
|
|
|
$entityTypeCode = $this->getValue(ColumnKeys::ENTITY_TYPE_CODE); |
|
94
|
|
|
$attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME); |
|
95
|
|
|
|
|
96
|
|
|
// query whether or not we've found a line with a attribute group definition |
|
97
|
|
|
if ($entityTypeCode === null && $attributeSetName === null) { |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// query whether or not, we've found a new entity type code/attribute set name => means we've found a new attribute set |
|
102
|
|
|
if ($this->hasBeenProcessed($entityTypeCode, $attributeSetName)) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// prepare the attribue set values |
|
107
|
|
|
$attributeSet = $this->initializeAttribute($this->prepareAttributes()); |
|
108
|
|
|
|
|
109
|
|
|
// persist the values and set the new attribute set ID |
|
110
|
|
|
$attributeSet[MemberNames::ATTRIBUTE_SET_ID] = $this->persistAttributeSet($this->initializeAttribute($this->prepareDynamicAttributes())); |
|
111
|
|
|
|
|
112
|
|
|
// temporarily persist the attribute set for processing the attribute groups |
|
113
|
|
|
$this->setLastAttributeSet($attributeSet); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Merge's and return's the entity with the passed attributes and set's the |
|
118
|
|
|
* passed status. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $entity The entity to merge the attributes into |
|
121
|
|
|
* @param array $attr The attributes to be merged |
|
122
|
|
|
* @param string|null $changeSetName The change set name to use |
|
123
|
|
|
* |
|
124
|
|
|
* @return array The merged entity |
|
125
|
|
|
* @todo https://github.com/techdivision/import/issues/179 |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
|
128
|
|
|
{ |
|
129
|
|
|
return array_merge( |
|
130
|
|
|
$entity, |
|
131
|
|
|
$this->entityMerger ? $this->entityMerger->merge($this, $entity, $attr) : $attr, |
|
132
|
|
|
array(EntityStatus::MEMBER_NAME => $this->detectState($entity, $attr, $changeSetName)) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Appends the dynamic to the static attributes for the EAV attribute |
|
138
|
|
|
* and returns them. |
|
139
|
|
|
* |
|
140
|
|
|
* @return array The array with all available attributes |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function prepareDynamicAttributes() |
|
143
|
|
|
{ |
|
144
|
|
|
return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
149
|
|
|
* |
|
150
|
|
|
* @return array The prepared attributes |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function prepareAttributes() |
|
153
|
|
|
{ |
|
154
|
|
|
|
|
155
|
|
|
// map the entity type code to the ID |
|
156
|
|
|
$entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE)); |
|
157
|
|
|
$entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID]; |
|
158
|
|
|
|
|
159
|
|
|
// load the attribute set names from the column |
|
160
|
|
|
$attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME); |
|
161
|
|
|
|
|
162
|
|
|
// return the prepared product |
|
163
|
|
|
return $this->initializeEntity( |
|
164
|
|
|
$this->loadRawEntity( |
|
165
|
|
|
array( |
|
166
|
|
|
MemberNames::ENTITY_TYPE_ID => $entityTypeId, |
|
167
|
|
|
MemberNames::ATTRIBUTE_SET_NAME => $attributeSetName |
|
168
|
|
|
) |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $data An array with data that will be used to initialize the raw entity with |
|
177
|
|
|
* |
|
178
|
|
|
* @return array The initialized entity |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function loadRawEntity(array $data = array()) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_SET, $data); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $attr The attribute attributes |
|
189
|
|
|
* |
|
190
|
|
|
* @return array The initialized attribute |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function initializeAttribute(array $attr) |
|
193
|
|
|
{ |
|
194
|
|
|
return $attr; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Return's the entity type for the passed code. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $entityTypeCode The entity type code |
|
201
|
|
|
* |
|
202
|
|
|
* @return array The requested entity type |
|
203
|
|
|
* @throws \Exception Is thrown, if the entity type with the passed code is not available |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function getEntityType($entityTypeCode) |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->getSubject()->getEntityType($entityTypeCode); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Persist the passed attribute set. |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $attributeSet The attribute set to persist |
|
214
|
|
|
* |
|
215
|
|
|
* @return string The ID of the persisted attribute set |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function persistAttributeSet(array $attributeSet) |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->getAttributeSetBunchProcessor()->persistAttributeSet($attributeSet); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|