1 | <?php |
||
37 | class ProductObserver extends AbstractProductImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The product bunch processor instance. |
||
42 | * |
||
43 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
44 | */ |
||
45 | protected $productBunchProcessor; |
||
46 | |||
47 | /** |
||
48 | * Initialize the observer with the passed product bunch processor instance. |
||
49 | * |
||
50 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
51 | 1 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
|
52 | */ |
||
53 | 1 | public function __construct(ProductBunchProcessorInterface $productBunchProcessor, StateDetectorInterface $stateDetector = null) |
|
62 | |||
63 | 1 | /** |
|
64 | * Return's the product bunch processor instance. |
||
65 | * |
||
66 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
67 | */ |
||
68 | protected function getProductBunchProcessor() |
||
72 | |||
73 | /** |
||
74 | * Process the observer's business logic. |
||
75 | 1 | * |
|
76 | * @return void |
||
77 | */ |
||
78 | protected function process() |
||
91 | 1 | ||
92 | /** |
||
93 | * Prepare the attributes of the entity that has to be persisted. |
||
94 | * |
||
95 | 1 | * @return array The prepared attributes |
|
96 | 1 | */ |
|
97 | protected function prepareAttributes() |
||
125 | |||
126 | /** |
||
127 | 1 | * Merge's and return's the entity with the passed attributes and set's the |
|
128 | * passed status. |
||
129 | * |
||
130 | * @param array $entity The entity to merge the attributes into |
||
131 | 1 | * @param array $attr The attributes to be merged |
|
132 | 1 | * @param string|null $changeSetName The change set name to use |
|
133 | * |
||
134 | * @return array The merged entity |
||
135 | */ |
||
136 | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
||
145 | |||
146 | 1 | /** |
|
147 | * Initialize the product with the passed attributes and returns an instance. |
||
148 | 1 | * |
|
149 | * @param array $attr The product attributes |
||
150 | * |
||
151 | * @return array The initialized product |
||
152 | */ |
||
153 | protected function initializeProduct(array $attr) |
||
164 | |||
165 | /** |
||
166 | * Load's and return's the product with the passed SKU. |
||
167 | * |
||
168 | 1 | * @param string $sku The SKU of the product to load |
|
169 | * |
||
170 | 1 | * @return array The product |
|
171 | */ |
||
172 | protected function loadProduct($sku) |
||
176 | |||
177 | /** |
||
178 | * Persist's the passed product data. |
||
179 | * |
||
180 | 1 | * @param array $product The product data to persist |
|
181 | * |
||
182 | 1 | * @return void |
|
183 | 1 | */ |
|
184 | protected function persistProduct($product) |
||
188 | |||
189 | /** |
||
190 | * Return's the attribute set of the product that has to be created. |
||
191 | * |
||
192 | * @return array The attribute set |
||
193 | */ |
||
194 | protected function getAttributeSet() |
||
198 | |||
199 | /** |
||
200 | * Set's the ID of the product that has been created recently. |
||
201 | * |
||
202 | * @param string $lastEntityId The entity ID |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | protected function setLastEntityId($lastEntityId) |
||
210 | } |
||
211 |
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 sub-classes 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 parent class: