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
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
41
|
|
|
* |
42
|
|
|
* @param array $row The row to handle |
43
|
|
|
* |
44
|
|
|
* @return array The modified row |
45
|
|
|
* @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
46
|
|
|
*/ |
47
|
|
|
public function handle(array $row) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
// initialize the row |
51
|
|
|
$this->setRow($row); |
|
|
|
|
52
|
|
|
|
53
|
|
|
// process the functionality and return the row |
54
|
|
|
$this->process(); |
55
|
|
|
|
56
|
|
|
// return the processed row |
57
|
|
|
return $this->getRow(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Process the observer's business logic. |
62
|
|
|
* |
63
|
|
|
* @return array The processed row |
64
|
|
|
*/ |
65
|
|
|
protected function process() |
66
|
|
|
{ |
67
|
|
|
// prepare and initialize product link entity |
68
|
|
|
$productLink = $this->initializeProductLink($this->prepareAttributes()); |
69
|
|
|
|
70
|
|
|
// persist the product link entity and store the link ID |
71
|
|
|
$this->setLastLinkId($this->persistProductLink($productLink)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
76
|
|
|
* |
77
|
|
|
* @return array The prepared attributes |
78
|
|
|
*/ |
79
|
|
|
protected function prepareAttributes() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
// extract the parent/child ID as well as the link type code from the row |
83
|
|
|
$parentSku = $this->getValue(ColumnKeys::LINK_PARENT_SKU); |
|
|
|
|
84
|
|
|
$childSku = $this->getValue(ColumnKeys::LINK_CHILD_SKU); |
|
|
|
|
85
|
|
|
$linkTypeCode = $this->getValue(ColumnKeys::LINK_TYPE_CODE); |
|
|
|
|
86
|
|
|
|
87
|
|
|
// load parent/child IDs and link type ID |
88
|
|
|
$parentId = $this->mapSku($parentSku); |
89
|
|
|
$childId = $this->mapSkuToEntityId($childSku); |
90
|
|
|
$linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
91
|
|
|
|
92
|
|
|
return $this->initializeEntity( |
|
|
|
|
93
|
|
|
array( |
94
|
|
|
MemberNames::PRODUCT_ID => $parentId, |
95
|
|
|
MemberNames::LINKED_PRODUCT_ID => $childId, |
96
|
|
|
MemberNames::LINK_TYPE_ID => $linkTypeId |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Initialize the product link with the passed attributes and returns an instance. |
103
|
|
|
* |
104
|
|
|
* @param array $attr The product link attributes |
105
|
|
|
* |
106
|
|
|
* @return array The initialized product link |
107
|
|
|
*/ |
108
|
|
|
protected function initializeProductLink(array $attr) |
109
|
|
|
{ |
110
|
|
|
return $attr; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Temporary persist the last link ID. |
115
|
|
|
* |
116
|
|
|
* @param integer $lastLinkId The last link ID |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function setLastLinkId($lastLinkId) |
121
|
|
|
{ |
122
|
|
|
$this->getSubject()->setLastLinkId($lastLinkId); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Persist's the passed product link data and return's the ID. |
127
|
|
|
* |
128
|
|
|
* @param array $productLink The product link data to persist |
129
|
|
|
* |
130
|
|
|
* @return string The ID of the persisted entity |
131
|
|
|
*/ |
132
|
|
|
protected function persistProductLink($productLink) |
133
|
|
|
{ |
134
|
|
|
return $this->getSubject()->persistProductLink($productLink); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return the entity ID for the passed SKU. |
139
|
|
|
* |
140
|
|
|
* @param string $sku The SKU to return the entity ID for |
141
|
|
|
* |
142
|
|
|
* @return integer The mapped entity ID |
143
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
144
|
|
|
*/ |
145
|
|
|
protected function mapSkuToEntityId($sku) |
146
|
|
|
{ |
147
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return the entity ID for the passed SKU. |
152
|
|
|
* |
153
|
|
|
* @param string $sku The SKU to return the entity ID for |
154
|
|
|
* |
155
|
|
|
* @return integer The mapped entity ID |
156
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
157
|
|
|
*/ |
158
|
|
|
protected function mapSku($sku) |
159
|
|
|
{ |
160
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return the link type ID for the passed link type code. |
165
|
|
|
* |
166
|
|
|
* @param string $linkTypeCode The link type code to return the link type ID for |
167
|
|
|
* |
168
|
|
|
* @return integer The mapped link type ID |
169
|
|
|
* @throws \Exception Is thrown if the link type code is not mapped yet |
170
|
|
|
*/ |
171
|
|
|
protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
172
|
|
|
{ |
173
|
|
|
return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.