1 | <?php |
||
37 | class ProductWebsiteObserver 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 | */ |
||
52 | 1 | public function __construct(ProductBunchProcessorInterface $productBunchProcessor) |
|
56 | |||
57 | /** |
||
58 | * Return's the product bunch processor instance. |
||
59 | * |
||
60 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
61 | */ |
||
62 | protected function getProductBunchProcessor() |
||
66 | |||
67 | /** |
||
68 | * The actual website code that has to be processed. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $code; |
||
73 | |||
74 | /** |
||
75 | * Set's the actual website code that has to be processed. |
||
76 | * |
||
77 | * @param string $code The website code |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | protected function setCode($code) |
||
85 | |||
86 | /** |
||
87 | * Return's the webiste code that has to be processed. |
||
88 | * |
||
89 | * @return string The website code |
||
90 | */ |
||
91 | protected function getCode() |
||
95 | |||
96 | /** |
||
97 | * Process the observer's business logic. |
||
98 | * |
||
99 | * @return array The processed row |
||
100 | */ |
||
101 | 1 | protected function process() |
|
102 | { |
||
103 | |||
104 | // query whether or not, we've found a new SKU => means we've found a new product |
||
105 | 1 | if ($this->hasBeenProcessed($sku = $this->getValue(ColumnKeys::SKU))) { |
|
106 | return; |
||
107 | } |
||
108 | |||
109 | // load the product => website relations |
||
110 | 1 | $codes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode')); |
|
111 | |||
112 | // query whether or not we've to clean up existing website product relations |
||
113 | 1 | if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_WEBSITE_PRODUCT_RELATIONS) && |
|
114 | 1 | $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_WEBSITE_PRODUCT_RELATIONS) |
|
115 | ) { |
||
116 | // initialize the array for the website IDs |
||
117 | $websiteIds = array(); |
||
118 | |||
119 | // prepare the website IDs |
||
120 | foreach ($codes as $code) { |
||
121 | $websiteIds[$code] = $this->getStoreWebsiteIdByCode($code); |
||
122 | } |
||
123 | |||
124 | // load the available product websites |
||
125 | if ($productWebsites = $this->loadProductWebsitesBySku($sku)) { |
||
126 | // iterate over the products websites |
||
127 | foreach ($productWebsites as $productWebsite) { |
||
128 | // if the product websit relation is in the CSV file, do nothing |
||
129 | if (in_array($productWebsite[MemberNames::WEBSITE_ID], $websiteIds)) { |
||
130 | continue; |
||
131 | } |
||
132 | |||
133 | // delete it, because we don't need it any longer |
||
134 | $this->deleteProductWebsite( |
||
135 | array( |
||
136 | MemberNames::PRODUCT_ID => $productWebsite[MemberNames::PRODUCT_ID], |
||
137 | MemberNames::WEBSITE_ID => $productWebsite[MemberNames::WEBSITE_ID] |
||
138 | ) |
||
139 | ); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // append the product => website relations found |
||
145 | 1 | foreach ($codes as $code) { |
|
146 | // set the code of the website that has to be processed |
||
147 | $this->setCode($code); |
||
148 | // prepare the product website relation attributes |
||
149 | $attr = $this->prepareAttributes(); |
||
150 | |||
151 | try { |
||
152 | // create the product website relation |
||
153 | $productWebsite = $this->initializeProductWebsite($attr); |
||
154 | $this->persistProductWebsite($productWebsite); |
||
155 | } catch (\RuntimeException $re) { |
||
156 | $this->getSystemLogger()->debug($re->getMessage()); |
||
157 | } |
||
158 | } |
||
159 | 1 | } |
|
160 | |||
161 | /** |
||
162 | * Prepare the attributes of the entity that has to be persisted. |
||
163 | * |
||
164 | * @return array The prepared attributes |
||
165 | */ |
||
166 | protected function prepareAttributes() |
||
183 | |||
184 | /** |
||
185 | * Initialize the product website with the passed attributes and returns an instance. |
||
186 | * |
||
187 | * @param array $attr The product website attributes |
||
188 | * |
||
189 | * @return array The initialized product website |
||
190 | * @throws \RuntimeException Is thrown, if the attributes can not be initialized |
||
191 | */ |
||
192 | protected function initializeProductWebsite(array $attr) |
||
196 | |||
197 | /** |
||
198 | * Load's and return's the product website relations for the product with the passed SKU. |
||
199 | * |
||
200 | * @param string $sku The SKU to of the product to load the product website relations for |
||
201 | * |
||
202 | * @return array The product website relations |
||
203 | */ |
||
204 | protected function loadProductWebsitesBySku($sku) |
||
208 | |||
209 | /** |
||
210 | * Persist's the passed product website data. |
||
211 | * |
||
212 | * @param array $productWebsite The product website data to persist |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function persistProductWebsite(array $productWebsite) |
||
220 | |||
221 | /** |
||
222 | * Delete's the passed product website data. |
||
223 | * |
||
224 | * @param array $productWebsite The product website data to delete |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | protected function deleteProductWebsite(array $productWebsite) |
||
232 | |||
233 | /** |
||
234 | * Return's the store website for the passed code. |
||
235 | * |
||
236 | * @param string $code The code of the store website to return the ID for |
||
237 | * |
||
238 | * @return integer The store website ID |
||
239 | */ |
||
240 | protected function getStoreWebsiteIdByCode($code) |
||
244 | } |
||
245 |
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: