1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Link\Observers\LinkObserver |
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 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 LinkObserver extends AbstractProductImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Process the observer's business logic. |
41
|
|
|
* |
42
|
|
|
* @return array The processed row |
43
|
|
|
*/ |
44
|
|
|
protected function process() |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// prepare the product link entity |
48
|
|
|
if ($attr = $this->prepareAttributes()) { |
49
|
|
|
// initialize the product link entity |
50
|
|
|
$productLink = $this->initializeProductLink($attr); |
51
|
|
|
|
52
|
|
|
// persist the product link entity and store the link ID |
53
|
|
|
$this->setLastLinkId($this->persistProductLink($productLink)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
59
|
|
|
* |
60
|
|
|
* @return array The prepared attributes |
61
|
|
|
*/ |
62
|
|
|
protected function prepareAttributes() |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
// extract the parent/child ID as well as the link type code from the row |
66
|
|
|
$parentSku = $this->getValue(ColumnKeys::LINK_PARENT_SKU); |
67
|
|
|
$childSku = $this->getValue(ColumnKeys::LINK_CHILD_SKU); |
68
|
|
|
$linkTypeCode = $this->getValue(ColumnKeys::LINK_TYPE_CODE); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
// load parent/child IDs and link type ID |
72
|
|
|
$parentId = $this->mapSku($parentSku); |
73
|
|
|
$childId = $this->mapSkuToEntityId($childSku); |
74
|
|
|
$linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
75
|
|
|
|
76
|
|
|
// initialize and return the entity |
77
|
|
|
return $this->initializeEntity( |
78
|
|
|
array( |
79
|
|
|
MemberNames::PRODUCT_ID => $parentId, |
80
|
|
|
MemberNames::LINKED_PRODUCT_ID => $childId, |
81
|
|
|
MemberNames::LINK_TYPE_ID => $linkTypeId |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
// query whether or not, debug mode is enabled |
87
|
|
|
if ($this->isDebugMode()) { |
88
|
|
|
// log a warning and return immediately |
89
|
|
|
$this->getSystemLogger()->warning($e->getMessage()); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// if we're NOT in debug mode, re-throw the exception |
94
|
|
|
throw $e; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Initialize the product link with the passed attributes and returns an instance. |
100
|
|
|
* |
101
|
|
|
* @param array $attr The product link attributes |
102
|
|
|
* |
103
|
|
|
* @return array The initialized product link |
104
|
|
|
*/ |
105
|
|
|
protected function initializeProductLink(array $attr) |
106
|
|
|
{ |
107
|
|
|
return $attr; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Temporary persist the last link ID. |
112
|
|
|
* |
113
|
|
|
* @param integer $lastLinkId The last link ID |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function setLastLinkId($lastLinkId) |
118
|
|
|
{ |
119
|
|
|
$this->getSubject()->setLastLinkId($lastLinkId); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Persist's the passed product link data and return's the ID. |
124
|
|
|
* |
125
|
|
|
* @param array $productLink The product link data to persist |
126
|
|
|
* |
127
|
|
|
* @return string The ID of the persisted entity |
128
|
|
|
*/ |
129
|
|
|
protected function persistProductLink($productLink) |
130
|
|
|
{ |
131
|
|
|
return $this->getSubject()->persistProductLink($productLink); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return the entity ID for the passed SKU. |
136
|
|
|
* |
137
|
|
|
* @param string $sku The SKU to return the entity ID for |
138
|
|
|
* |
139
|
|
|
* @return integer The mapped entity ID |
140
|
|
|
* @throws \TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
141
|
|
|
*/ |
142
|
|
|
protected function mapSkuToEntityId($sku) |
143
|
|
|
{ |
144
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the entity ID for the passed SKU. |
149
|
|
|
* |
150
|
|
|
* @param string $sku The SKU to return the entity ID for |
151
|
|
|
* |
152
|
|
|
* @return integer The mapped entity ID |
153
|
|
|
* @throws \TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
154
|
|
|
*/ |
155
|
|
|
protected function mapSku($sku) |
156
|
|
|
{ |
157
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the link type ID for the passed link type code. |
162
|
|
|
* |
163
|
|
|
* @param string $linkTypeCode The link type code to return the link type ID for |
164
|
|
|
* |
165
|
|
|
* @return integer The mapped link type ID |
166
|
|
|
* @throws \TechDivision\Import\Product\Link\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet |
167
|
|
|
*/ |
168
|
|
|
protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
169
|
|
|
{ |
170
|
|
|
return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Queries whether or not debug mode is enabled or not, default is TRUE. |
175
|
|
|
* |
176
|
|
|
* @return boolean TRUE if debug mode is enabled, else FALSE |
177
|
|
|
*/ |
178
|
|
|
protected function isDebugMode() |
179
|
|
|
{ |
180
|
|
|
return $this->getSubject()->isDebugMode(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|