1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Link\Observers\LinkAttributePositionObserver |
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-link |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Link\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Link\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Link\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Oberserver that provides functionality for the product link attribute position replace operation. |
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-link |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class LinkAttributePositionObserver extends AbstractProductImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The link attribute that has to be handled by this observer. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
const ATTRIBUTE_CODE = 'position'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The product link attribute ID to persist the the position attribute for. |
48
|
|
|
* |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
protected $productLinktAttributeId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Process the observer's business logic. |
55
|
|
|
* |
56
|
|
|
* @return array The processed row |
57
|
|
|
*/ |
58
|
|
|
protected function process() |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
// initialize the attribute code |
62
|
|
|
$attributeCode = LinkAttributePositionObserver::ATTRIBUTE_CODE; |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
// extract the link type code from the row |
66
|
|
|
$linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($this->getValue(ColumnKeys::LINK_TYPE_CODE)); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
// query whether or not, debug mode is enabled |
69
|
|
|
if ($this->isDebugMode()) { |
70
|
|
|
// log a warning and return immediately |
71
|
|
|
$this->getSystemLogger()->warning($e->getMessage()); |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// if we're NOT in debug mode, re-throw the exception |
76
|
|
|
throw $e; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// try to load the product link attribute |
80
|
|
|
if ($productLinkAttribute = $this->getProductLinkAttribute($linkTypeId, $attributeCode)) { |
81
|
|
|
$this->setProductLinkAttributeId($productLinkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_ID]); |
82
|
|
|
} else { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// prepare, initialize and persist the product link attribute int entity |
87
|
|
|
$productLink = $this->initializeProductLinkAttributeInt($this->prepareAttributes()); |
88
|
|
|
$this->persistProductLinkAttributeInt($productLink); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
93
|
|
|
* |
94
|
|
|
* @return array The prepared attributes |
95
|
|
|
*/ |
96
|
|
|
protected function prepareAttributes() |
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
// load the ID of the last link |
100
|
|
|
$linkId = $this->getLastLinkId(); |
101
|
|
|
|
102
|
|
|
// load the product link attribute ID |
103
|
|
|
$productLinkAttributeId = $this->getProductLinkAttributeId(); |
104
|
|
|
|
105
|
|
|
// load the position value |
106
|
|
|
$value = $this->getValue(ColumnKeys::LINK_POSITION); |
107
|
|
|
|
108
|
|
|
// initialize and return the entity |
109
|
|
|
return $this->initializeEntity( |
110
|
|
|
array( |
111
|
|
|
MemberNames::PRODUCT_LINK_ATTRIBUTE_ID => $productLinkAttributeId, |
112
|
|
|
MemberNames::LINK_ID => $linkId, |
113
|
|
|
MemberNames::VALUE => $value |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Temporary persist the product link attribute ID. |
120
|
|
|
* |
121
|
|
|
* @param integer $productLinkAttributeId The product link attribute ID |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected function setProductLinkAttributeId($productLinkAttributeId) |
126
|
|
|
{ |
127
|
|
|
$this->productLinktAttributeId = $productLinkAttributeId; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return's the temporary persisted product link attribute ID. |
132
|
|
|
* |
133
|
|
|
* @return integer The product link attribute ID |
134
|
|
|
*/ |
135
|
|
|
protected function getProductLinkAttributeId() |
136
|
|
|
{ |
137
|
|
|
return $this->productLinktAttributeId; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return's the link attribute for the passed link type ID and attribute code. |
142
|
|
|
* |
143
|
|
|
* @param integer $linkTypeId The link type |
144
|
|
|
* @param string $attributeCode The attribute code |
145
|
|
|
* |
146
|
|
|
* @return array The link attribute |
147
|
|
|
*/ |
148
|
|
|
protected function getProductLinkAttribute($linkTypeId, $attributeCode) |
149
|
|
|
{ |
150
|
|
|
return $this->getSubject()->getProductLinkAttribute($linkTypeId, $attributeCode); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Initialize the product link attribute with the passed attributes and returns an instance. |
155
|
|
|
* |
156
|
|
|
* @param array $attr The product link attribute |
157
|
|
|
* |
158
|
|
|
* @return array The initialized product link attribute |
159
|
|
|
*/ |
160
|
|
|
protected function initializeProductLinkAttributeInt(array $attr) |
161
|
|
|
{ |
162
|
|
|
return $attr; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Load the temporary persisted the last link ID. |
167
|
|
|
* |
168
|
|
|
* @return integer The last link ID |
169
|
|
|
*/ |
170
|
|
|
protected function getLastLinkId() |
171
|
|
|
{ |
172
|
|
|
return $this->getSubject()->getLastLinkId(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return the link type ID for the passed link type code. |
177
|
|
|
* |
178
|
|
|
* @param string $linkTypeCode The link type code to return the link type ID for |
179
|
|
|
* |
180
|
|
|
* @return integer The mapped link type ID |
181
|
|
|
* @throws \Exception Is thrown if the link type code is not mapped yet |
182
|
|
|
*/ |
183
|
|
|
protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
184
|
|
|
{ |
185
|
|
|
return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Persist's the passed product link attribute int data and return's the ID. |
190
|
|
|
* |
191
|
|
|
* @param array $productLinkAttributeInt The product link attribute int data to persist |
192
|
|
|
* |
193
|
|
|
* @return string The ID of the persisted entity |
194
|
|
|
*/ |
195
|
|
|
protected function persistProductLinkAttributeInt($productLinkAttributeInt) |
196
|
|
|
{ |
197
|
|
|
$this->getSubject()->persistProductLinkAttributeInt($productLinkAttributeInt); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|