1 | <?php |
||
42 | class ProductInventoryObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface, StateDetectorAwareObserverInterface, ObserverFactoryInterface |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * The product bunch processor instance. |
||
47 | * |
||
48 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
49 | */ |
||
50 | protected $productBunchProcessor; |
||
51 | |||
52 | /** |
||
53 | * The attribute loader instance. |
||
54 | * |
||
55 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
56 | */ |
||
57 | protected $attributeLoader; |
||
58 | |||
59 | /** |
||
60 | * The array with the column mappings that has to be computed. |
||
61 | 2 | * |
|
62 | * @var array |
||
63 | */ |
||
64 | protected $columns = array(); |
||
65 | 2 | ||
66 | 2 | /** |
|
67 | 2 | * Initialize the observer with the passed product bunch processor instance. |
|
68 | * |
||
69 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
70 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
71 | */ |
||
72 | public function __construct( |
||
85 | |||
86 | /** |
||
87 | * Will be invoked by the observer visitor when a factory has been defined to create the observer instance. |
||
88 | 2 | * |
|
89 | 1 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
|
90 | * |
||
91 | * @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
||
92 | */ |
||
93 | 1 | public function createObserver(SubjectInterface $subject) |
|
110 | |||
111 | 1 | /** |
|
112 | * Returns an array of the columns with their types to detect state. |
||
113 | 1 | * |
|
114 | 1 | * @return array The array with the column names as key and their type as value |
|
115 | 1 | */ |
|
116 | public function getColumns() |
||
120 | |||
121 | /** |
||
122 | * Return's the product bunch processor instance. |
||
123 | * |
||
124 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
125 | 1 | */ |
|
126 | protected function getProductBunchProcessor() |
||
130 | |||
131 | /** |
||
132 | * Process the observer's business logic. |
||
133 | * |
||
134 | * @return array The processed row |
||
135 | */ |
||
136 | protected function process() |
||
149 | 1 | ||
150 | /** |
||
151 | * Prepare the basic attributes of the stock status/item entity that has to be persisted. |
||
152 | * |
||
153 | * @return array The prepared stock status/item attributes |
||
154 | */ |
||
155 | protected function prepareAttributes() |
||
173 | |||
174 | /** |
||
175 | * Prepare the stock item attributes of the entity that has to be persisted. |
||
176 | * |
||
177 | * @return array The prepared stock status item |
||
178 | */ |
||
179 | protected function prepareStockItemAttributes() |
||
183 | |||
184 | /** |
||
185 | * Initialize the stock item with the passed attributes and returns an instance. |
||
186 | * |
||
187 | * @param array $attr The stock item attributes |
||
188 | * |
||
189 | * @return array The initialized stock item |
||
190 | */ |
||
191 | protected function initializeStockItem(array $attr) |
||
195 | |||
196 | /** |
||
197 | * Return's the appings for the table column => CSV column header. |
||
198 | * |
||
199 | * @return array The header stock mappings |
||
200 | */ |
||
201 | protected function getHeaderStockMappings() |
||
205 | |||
206 | /** |
||
207 | * Persist's the passed stock item data and return's the ID. |
||
208 | * |
||
209 | * @param array $stockItem The stock item data to persist |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | protected function persistStockItem($stockItem) |
||
217 | } |
||
218 |
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: