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