1 | <?php |
||
37 | class LinkAttributePositionObserver extends AbstractProductImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The link attribute that has to be handled by this observer. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const ATTRIBUTE_CODE = 'position'; |
||
46 | |||
47 | /** |
||
48 | * The product link attribute ID to persist the the position attribute for. |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | protected $productLinktAttributeId; |
||
53 | |||
54 | /** |
||
55 | * The product link processor instance. |
||
56 | * |
||
57 | * @var \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface |
||
58 | */ |
||
59 | protected $productLinkProcessor; |
||
60 | |||
61 | /** |
||
62 | * Initialize the observer with the passed product link processor instance. |
||
63 | * |
||
64 | * @param \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface $productLinkProcessor The product link processor instance |
||
65 | */ |
||
66 | public function __construct(ProductLinkProcessorInterface $productLinkProcessor) |
||
70 | |||
71 | /** |
||
72 | * Return's the product link processor instance. |
||
73 | * |
||
74 | * @return \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface The product link processor instance |
||
75 | */ |
||
76 | protected function getProductLinkProcessor() |
||
80 | |||
81 | /** |
||
82 | * Process the observer's business logic. |
||
83 | * |
||
84 | * @return array The processed row |
||
85 | */ |
||
86 | protected function process() |
||
105 | |||
106 | /** |
||
107 | * Prepare the attributes of the entity that has to be persisted. |
||
108 | * |
||
109 | * @return array The prepared attributes |
||
110 | */ |
||
111 | protected function prepareAttributes() |
||
132 | |||
133 | /** |
||
134 | * Temporary persist the product link attribute ID. |
||
135 | * |
||
136 | * @param integer $productLinkAttributeId The product link attribute ID |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | protected function setProductLinkAttributeId($productLinkAttributeId) |
||
144 | |||
145 | /** |
||
146 | * Return's the temporary persisted product link attribute ID. |
||
147 | * |
||
148 | * @return integer The product link attribute ID |
||
149 | */ |
||
150 | protected function getProductLinkAttributeId() |
||
154 | |||
155 | /** |
||
156 | * Return's the link attribute for the passed link type and attribute code. |
||
157 | * |
||
158 | * @param string $linkTypeCode The link type code |
||
159 | * @param string $attributeCode The attribute code |
||
160 | * |
||
161 | * @return array The link attribute |
||
162 | */ |
||
163 | protected function getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $attributeCode) |
||
167 | |||
168 | /** |
||
169 | * Initialize the product link attribute with the passed attributes and returns an instance. |
||
170 | * |
||
171 | * @param array $attr The product link attribute |
||
172 | * |
||
173 | * @return array The initialized product link attribute |
||
174 | */ |
||
175 | protected function initializeProductLinkAttributeInt(array $attr) |
||
179 | |||
180 | /** |
||
181 | * Initialize the product link attribute with the passed attributes and returns an instance. |
||
182 | * |
||
183 | * @param array $attr The product link attribute |
||
184 | * |
||
185 | * @return array The initialized product link attribute |
||
186 | */ |
||
187 | protected function initializeProductLinkAttributeDecimal(array $attr) |
||
191 | |||
192 | /** |
||
193 | * Initialize the product link attribute with the passed attributes and returns an instance. |
||
194 | * |
||
195 | * @param array $attr The product link attribute |
||
196 | * |
||
197 | * @return array The initialized product link attribute |
||
198 | */ |
||
199 | protected function initializeProductLinkAttributeVarchar(array $attr) |
||
203 | |||
204 | /** |
||
205 | * Load the temporary persisted the last link ID. |
||
206 | * |
||
207 | * @return integer The last link ID |
||
208 | */ |
||
209 | protected function getLastLinkId() |
||
213 | |||
214 | /** |
||
215 | * Return the link type ID for the passed link type code. |
||
216 | * |
||
217 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
218 | * |
||
219 | * @return integer The mapped link type ID |
||
220 | * @throws \Exception Is thrown if the link type code is not mapped yet |
||
221 | */ |
||
222 | protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
||
226 | |||
227 | /** |
||
228 | * Persists the passed product link attribute int data and return's the ID. |
||
229 | * |
||
230 | * @param array $productLinkAttributeInt The product link attribute int data to persist |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | protected function persistProductLinkAttributeInt($productLinkAttributeInt) |
||
238 | |||
239 | /** |
||
240 | * Persists the passed product link attribute decimal data and return's the ID. |
||
241 | * |
||
242 | * @param array $productLinkAttributeDecimal The product link attribute decimal data to persist |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | protected function persistProductLinkAttributeDecimal($productLinkAttributeDecimal) |
||
250 | |||
251 | /** |
||
252 | * Persists the passed product link attribute varchar data and return's the ID. |
||
253 | * |
||
254 | * @param array $productLinkAttributeVarchar The product link attribute varchar data to persist |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | protected function persistProductLinkAttributeVarchar($productLinkAttributeVarchar) |
||
262 | } |
||
263 |
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: