1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\AttributeObserver |
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-attribute |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Attribute\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
24
|
|
|
use TechDivision\Import\Attribute\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
27
|
|
|
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Observer that create's the EAV attribute itself. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/techdivision/import-attribute |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class AttributeObserver extends AbstractAttributeImportObserver |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The attribute processor instance. |
43
|
|
|
* |
44
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
45
|
|
|
*/ |
46
|
|
|
protected $attributeBunchProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initializes the observer with the passed subject instance. |
50
|
|
|
* |
51
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject instance |
52
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
SubjectInterface $subject, |
56
|
|
|
AttributeBunchProcessorInterface $attributeBunchProcessor |
57
|
|
|
) { |
58
|
|
|
|
59
|
|
|
// pass the subject through to the parend observer |
60
|
|
|
parent::__construct($subject); |
61
|
|
|
|
62
|
|
|
// initialize the attribute bunch processor |
63
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Process the observer's business logic. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function process() |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// query whether or not, we've found a new attribute code => means we've found a new attribute |
75
|
|
|
if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// prepare the attribue values |
80
|
|
|
$attribute = $this->initializeAttribute($this->prepareAttributes()); |
81
|
|
|
|
82
|
|
|
// insert the entity and set the entity ID |
83
|
|
|
$this->setLastAttributeId($this->persistAttribute($attribute)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
88
|
|
|
* |
89
|
|
|
* @return array The prepared attributes |
90
|
|
|
*/ |
91
|
|
|
protected function prepareAttributes() |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
// map the entity type code to the ID |
95
|
|
|
$entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE)); |
96
|
|
|
$entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID]; |
97
|
|
|
|
98
|
|
|
// load the data from the row |
99
|
|
|
$attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE); |
100
|
|
|
$attributeModel = $this->getValue(ColumnKeys::ATTRIBUTE_MODEL); |
101
|
|
|
$backendModel = $this->getValue(ColumnKeys::BACKEND_MODEL); |
102
|
|
|
$backendType = $this->getValue(ColumnKeys::BACKEND_TYPE, BackendTypeKeys::BACKEND_TYPE_STATIC); |
103
|
|
|
$backendTable = $this->getValue(ColumnKeys::BACKEND_TABLE); |
104
|
|
|
$frontendModel = $this->getValue(ColumnKeys::FRONTEND_MODEL); |
105
|
|
|
$frontendInput = $this->getValue(ColumnKeys::FRONTEND_INPUT); |
106
|
|
|
$frontendLabel = $this->getValue(ColumnKeys::FRONTEND_LABEL); |
107
|
|
|
$frontendClass = $this->getValue(ColumnKeys::FRONTEND_CLASS); |
108
|
|
|
$sourceModel = $this->getValue(ColumnKeys::SOURCE_MODEL); |
109
|
|
|
$isRequired = $this->getValue(ColumnKeys::IS_REQUIRED, 0); |
110
|
|
|
$isUserDefined = $this->getValue(ColumnKeys::IS_USER_DEFINED, 1); |
111
|
|
|
$defaultValue = $this->getValue(ColumnKeys::DEFAULT_VALUE); |
112
|
|
|
$isUnique = $this->getValue(ColumnKeys::IS_UNIQUE, 0); |
113
|
|
|
$note = $this->getValue(ColumnKeys::NOTE); |
114
|
|
|
|
115
|
|
|
// return the prepared product |
116
|
|
|
return $this->initializeEntity( |
117
|
|
|
array( |
118
|
|
|
MemberNames::ENTITY_TYPE_ID => $entityTypeId, |
119
|
|
|
MemberNames::ATTRIBUTE_CODE => $attributeCode, |
120
|
|
|
MemberNames::ATTRIBUTE_MODEL => $attributeModel, |
121
|
|
|
MemberNames::BACKEND_MODEL => $backendModel, |
122
|
|
|
MemberNames::BACKEND_TYPE => $backendType, |
123
|
|
|
MemberNames::BACKEND_TABLE => $backendTable, |
124
|
|
|
MemberNames::FRONTEND_MODEL => $frontendModel, |
125
|
|
|
MemberNames::FRONTEND_INPUT => $frontendInput, |
126
|
|
|
MemberNames::FRONTEND_LABEL => $frontendLabel, |
127
|
|
|
MemberNames::FRONTEND_CLASS => $frontendClass, |
128
|
|
|
MemberNames::SOURCE_MODEL => $sourceModel, |
129
|
|
|
MemberNames::IS_REQUIRED => $isRequired, |
130
|
|
|
MemberNames::IS_USER_DEFINED => $isUserDefined, |
131
|
|
|
MemberNames::DEFAULT_VALUE => $defaultValue, |
132
|
|
|
MemberNames::IS_UNIQUE => $isUnique, |
133
|
|
|
MemberNames::NOTE => $note |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
140
|
|
|
* |
141
|
|
|
* @param array $attr The attribute attributes |
142
|
|
|
* |
143
|
|
|
* @return array The initialized attribute |
144
|
|
|
*/ |
145
|
|
|
protected function initializeAttribute(array $attr) |
146
|
|
|
{ |
147
|
|
|
return $attr; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return's the attribute bunch processor instance. |
152
|
|
|
* |
153
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
154
|
|
|
*/ |
155
|
|
|
protected function getAttributeBunchProcessor() |
156
|
|
|
{ |
157
|
|
|
return $this->attributeBunchProcessor; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Persist the passed attribute. |
162
|
|
|
* |
163
|
|
|
* @param array $attribute The attribute to persist |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
protected function persistAttribute(array $attribute) |
168
|
|
|
{ |
169
|
|
|
return $this->getAttributeBunchProcessor()->persistAttribute($attribute); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return's the entity type for the passed code. |
174
|
|
|
* |
175
|
|
|
* @param string $entityTypeCode The entity type code |
176
|
|
|
* |
177
|
|
|
* @return array The requested entity type |
178
|
|
|
* @throws \Exception Is thrown, if the entity type with the passed code is not available |
179
|
|
|
*/ |
180
|
|
|
protected function getEntityType($entityTypeCode) |
181
|
|
|
{ |
182
|
|
|
return $this->getSubject()->getEntityType($entityTypeCode); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|