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\Bundle\Services\ProductBundleProcessorInterface 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() |
||
118 | |||
119 | /** |
||
120 | * Prepare the attributes of the entity that has to be persisted. |
||
121 | * |
||
122 | * @return array The prepared attributes |
||
123 | */ |
||
124 | protected function prepareAttributes() |
||
145 | |||
146 | /** |
||
147 | * Temporary persist the product link attribute ID. |
||
148 | * |
||
149 | * @param integer $productLinkAttributeId The product link attribute ID |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | protected function setProductLinkAttributeId($productLinkAttributeId) |
||
157 | |||
158 | /** |
||
159 | * Return's the temporary persisted product link attribute ID. |
||
160 | * |
||
161 | * @return integer The product link attribute ID |
||
162 | */ |
||
163 | protected function getProductLinkAttributeId() |
||
167 | |||
168 | /** |
||
169 | * Return's the link attribute for the passed link type ID and attribute code. |
||
170 | * |
||
171 | * @param integer $linkTypeId The link type |
||
172 | * @param string $attributeCode The attribute code |
||
173 | * |
||
174 | * @return array The link attribute |
||
175 | */ |
||
176 | protected function getProductLinkAttribute($linkTypeId, $attributeCode) |
||
180 | |||
181 | /** |
||
182 | * Initialize the product link attribute with the passed attributes and returns an instance. |
||
183 | * |
||
184 | * @param array $attr The product link attribute |
||
185 | * |
||
186 | * @return array The initialized product link attribute |
||
187 | */ |
||
188 | protected function initializeProductLinkAttributeInt(array $attr) |
||
192 | |||
193 | /** |
||
194 | * Load the temporary persisted the last link ID. |
||
195 | * |
||
196 | * @return integer The last link ID |
||
197 | */ |
||
198 | protected function getLastLinkId() |
||
202 | |||
203 | /** |
||
204 | * Return the link type ID for the passed link type code. |
||
205 | * |
||
206 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
207 | * |
||
208 | * @return integer The mapped link type ID |
||
209 | * @throws \Exception Is thrown if the link type code is not mapped yet |
||
210 | */ |
||
211 | protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
||
215 | |||
216 | /** |
||
217 | * Persist's the passed product link attribute int data and return's the ID. |
||
218 | * |
||
219 | * @param array $productLinkAttributeInt The product link attribute int data to persist |
||
220 | * |
||
221 | * @return string The ID of the persisted entity |
||
222 | */ |
||
223 | protected function persistProductLinkAttributeInt($productLinkAttributeInt) |
||
227 | } |
||
228 |
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: