|
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 and initialize product link entity |
|
48
|
|
|
$productLink = $this->initializeProductLink($this->prepareAttributes()); |
|
49
|
|
|
|
|
50
|
|
|
// persist the product link entity and store the link ID |
|
51
|
|
|
$this->setLastLinkId($this->persistProductLink($productLink)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array The prepared attributes |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function prepareAttributes() |
|
60
|
|
|
{ |
|
61
|
|
|
|
|
62
|
|
|
// extract the parent/child ID as well as the link type code from the row |
|
63
|
|
|
$parentSku = $this->getValue(ColumnKeys::LINK_PARENT_SKU); |
|
64
|
|
|
$childSku = $this->getValue(ColumnKeys::LINK_CHILD_SKU); |
|
65
|
|
|
$linkTypeCode = $this->getValue(ColumnKeys::LINK_TYPE_CODE); |
|
66
|
|
|
|
|
67
|
|
|
// load parent/child IDs and link type ID |
|
68
|
|
|
$parentId = $this->mapSku($parentSku); |
|
69
|
|
|
$childId = $this->mapSkuToEntityId($childSku); |
|
70
|
|
|
$linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
|
71
|
|
|
|
|
72
|
|
|
// initialize and return the entity |
|
73
|
|
|
return $this->initializeEntity( |
|
74
|
|
|
array( |
|
75
|
|
|
MemberNames::PRODUCT_ID => $parentId, |
|
76
|
|
|
MemberNames::LINKED_PRODUCT_ID => $childId, |
|
77
|
|
|
MemberNames::LINK_TYPE_ID => $linkTypeId |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Initialize the product link with the passed attributes and returns an instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $attr The product link attributes |
|
86
|
|
|
* |
|
87
|
|
|
* @return array The initialized product link |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function initializeProductLink(array $attr) |
|
90
|
|
|
{ |
|
91
|
|
|
return $attr; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Temporary persist the last link ID. |
|
96
|
|
|
* |
|
97
|
|
|
* @param integer $lastLinkId The last link ID |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function setLastLinkId($lastLinkId) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->getSubject()->setLastLinkId($lastLinkId); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Persist's the passed product link data and return's the ID. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $productLink The product link data to persist |
|
110
|
|
|
* |
|
111
|
|
|
* @return string The ID of the persisted entity |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function persistProductLink($productLink) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->getSubject()->persistProductLink($productLink); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return the entity ID for the passed SKU. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
122
|
|
|
* |
|
123
|
|
|
* @return integer The mapped entity ID |
|
124
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function mapSkuToEntityId($sku) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return the entity ID for the passed SKU. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
135
|
|
|
* |
|
136
|
|
|
* @return integer The mapped entity ID |
|
137
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function mapSku($sku) |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Return the link type ID for the passed link type code. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $linkTypeCode The link type code to return the link type ID for |
|
148
|
|
|
* |
|
149
|
|
|
* @return integer The mapped link type ID |
|
150
|
|
|
* @throws \Exception Is thrown if the link type code is not mapped yet |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|