1 | <?php |
||
39 | class ProductInventoryObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The product bunch processor instance. |
||
44 | * |
||
45 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
46 | */ |
||
47 | protected $productBunchProcessor; |
||
48 | |||
49 | /** |
||
50 | * The attribute loader instance. |
||
51 | * |
||
52 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
53 | */ |
||
54 | protected $attributeLoader; |
||
55 | |||
56 | /** |
||
57 | * Initialize the observer with the passed product bunch processor instance. |
||
58 | * |
||
59 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
60 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface $attributeLoader The attribute loader instance |
||
61 | */ |
||
62 | 2 | public function __construct( |
|
69 | |||
70 | /** |
||
71 | * Return's the product bunch processor instance. |
||
72 | * |
||
73 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
74 | */ |
||
75 | 1 | protected function getProductBunchProcessor() |
|
79 | |||
80 | /** |
||
81 | * Process the observer's business logic. |
||
82 | * |
||
83 | * @return array The processed row |
||
84 | */ |
||
85 | 2 | protected function process() |
|
96 | |||
97 | /** |
||
98 | * Prepare the basic attributes of the stock status/item entity that has to be persisted. |
||
99 | * |
||
100 | * @return array The prepared stock status/item attributes |
||
101 | */ |
||
102 | 1 | protected function prepareAttributes() |
|
120 | |||
121 | /** |
||
122 | * Prepare the stock item attributes of the entity that has to be persisted. |
||
123 | * |
||
124 | * @return array The prepared stock status item |
||
125 | */ |
||
126 | 1 | protected function prepareStockItemAttributes() |
|
130 | |||
131 | /** |
||
132 | * Initialize the stock item with the passed attributes and returns an instance. |
||
133 | * |
||
134 | * @param array $attr The stock item attributes |
||
135 | * |
||
136 | * @return array The initialized stock item |
||
137 | */ |
||
138 | 1 | protected function initializeStockItem(array $attr) |
|
142 | |||
143 | /** |
||
144 | * Return's the appings for the table column => CSV column header. |
||
145 | * |
||
146 | * @return array The header stock mappings |
||
147 | */ |
||
148 | 1 | protected function getHeaderStockMappings() |
|
152 | |||
153 | /** |
||
154 | * Persist's the passed stock item data and return's the ID. |
||
155 | * |
||
156 | * @param array $stockItem The stock item data to persist |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | 1 | protected function persistStockItem($stockItem) |
|
164 | } |
||
165 |
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: