1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Link\Subjects\LinkSubject |
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\Subjects; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
24
|
|
|
use TechDivision\Import\Product\Subjects\AbstractProductSubject; |
25
|
|
|
use TechDivision\Import\Product\Link\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A SLSB that handles the process to import product links. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import-product-link |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class LinkSubject extends AbstractProductSubject |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The available link types. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $linkTypes = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The mapping for the SKUs to the created entity IDs. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $skuEntityIdMapping = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Intializes the previously loaded global data for exactly one variants. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* @see \Importer\Csv\Actions\ProductImportAction::prepare() |
59
|
|
|
*/ |
60
|
|
|
public function setUp() |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// invoke the parent method |
64
|
|
|
parent::setUp(); |
65
|
|
|
|
66
|
|
|
// load the entity manager and the registry processor |
67
|
|
|
$registryProcessor = $this->getRegistryProcessor(); |
68
|
|
|
|
69
|
|
|
// load the status of the actual import process |
70
|
|
|
$status = $registryProcessor->getAttribute($this->getSerial()); |
71
|
|
|
|
72
|
|
|
// load the attribute set we've prepared intially |
73
|
|
|
$this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return the entity ID for the passed SKU. |
78
|
|
|
* |
79
|
|
|
* @param string $sku The SKU to return the entity ID for |
80
|
|
|
* |
81
|
|
|
* @return integer The mapped entity ID |
82
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
83
|
|
|
*/ |
84
|
|
|
public function mapSkuToEntityId($sku) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
// query weather or not the SKU has been mapped |
88
|
|
|
if (isset($this->skuEntityIdMapping[$sku])) { |
89
|
|
|
return $this->skuEntityIdMapping[$sku]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// throw an exception if the SKU has not been mapped yet |
93
|
|
|
throw new \Exception(sprintf('Found not mapped SKU %s', $sku)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the link type ID for the passed link type code. |
98
|
|
|
* |
99
|
|
|
* @param string $linkTypeCode The link type code to return the link type ID for |
100
|
|
|
* |
101
|
|
|
* @return integer The mapped link type ID |
102
|
|
|
* @throws \Exception Is thrown if the link type code is not mapped yet |
103
|
|
|
*/ |
104
|
|
|
public function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
// query weather or not the link type code has been mapped |
108
|
|
|
if (isset($this->linkTypes[$linkTypeCode])) { |
109
|
|
|
return $this->linkTypes[$linkTypeCode][MemberNames::LINK_TYPE_ID]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// throw an exception if the link type code has not been mapped yet |
113
|
|
|
throw new \Exception(sprintf('Found not mapped link type code %s', $linkTypeCode)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Persist's the passed product link data and return's the ID. |
118
|
|
|
* |
119
|
|
|
* @param array $productLink The product link data to persist |
120
|
|
|
* |
121
|
|
|
* @return string The ID of the persisted entity |
122
|
|
|
*/ |
123
|
|
|
public function persistProductLink($productLink) |
124
|
|
|
{ |
125
|
|
|
return $this->getProductProcessor()->persistProductLink($productLink); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Persist's the passed product link attribute data and return's the ID. |
130
|
|
|
* |
131
|
|
|
* @param array $productLinkAttribute The product link attribute data to persist |
132
|
|
|
* |
133
|
|
|
* @return string The ID of the persisted entity |
134
|
|
|
*/ |
135
|
|
|
public function persistProductLinkAttribute($productLinkAttribute) |
136
|
|
|
{ |
137
|
|
|
return $this->getProductProcessor()->persistProductLinkAttribute($productLinkAttribute); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Persist's the passed product link attribute decimal data. |
142
|
|
|
* |
143
|
|
|
* @param array $productLinkAttributeDecimal The product link attribute decimal data to persist |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function persistProductLinkAttributeDecimal($productLinkAttributeDecimal) |
148
|
|
|
{ |
149
|
|
|
$this->getProductProcessor()->persistProductLinkAttributeDecimal($productLinkAttributeDecimal); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Persist's the passed product link attribute integer data. |
154
|
|
|
* |
155
|
|
|
* @param array $productLinkAttributeInt The product link attribute integer data to persist |
156
|
|
|
* |
157
|
|
|
* @return string The ID of the persisted entity |
158
|
|
|
*/ |
159
|
|
|
public function persistProductLinkAttributeInt($productLinkAttributeInt) |
160
|
|
|
{ |
161
|
|
|
$this->getProductProcessor()->persistProductLinkAttributeInt($productLinkAttributeInt); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Persist's the passed product link attribute varchar data. |
166
|
|
|
* |
167
|
|
|
* @param array $productLinkAttributeVarchar The product link attribute varchar data to persist |
168
|
|
|
* |
169
|
|
|
* @return string The ID of the persisted entity |
170
|
|
|
*/ |
171
|
|
|
public function persistProductLinkAttributeVarchar($productLinkAttributeVarchar) |
172
|
|
|
{ |
173
|
|
|
$this->getProductProcessor()->persistProductLinkAttributeVarchar($productLinkAttributeVarchar); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: