Total Complexity | 40 |
Total Lines | 549 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Standard |
||
23 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
24 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
25 | { |
||
26 | /** |
||
27 | * Copies a resource |
||
28 | * |
||
29 | * @return string HTML output |
||
30 | */ |
||
31 | public function copy() |
||
69 | } |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Creates a new resource |
||
74 | * |
||
75 | * @return string HTML output |
||
76 | */ |
||
77 | public function create() |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Deletes a resource |
||
121 | * |
||
122 | * @return string|null HTML output |
||
123 | */ |
||
124 | public function delete() |
||
125 | { |
||
126 | $view = $this->getView(); |
||
127 | $context = $this->getContext(); |
||
128 | |||
129 | $manager = \Aimeos\MShop::create( $context, 'supplier' ); |
||
130 | $manager->begin(); |
||
131 | |||
132 | try |
||
133 | { |
||
134 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
135 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
136 | } |
||
137 | |||
138 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
139 | $search->setConditions( $search->compare( '==', 'supplier.id', $ids ) ); |
||
140 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
141 | |||
142 | foreach( $items as $item ) |
||
143 | { |
||
144 | $view->item = $item; |
||
145 | |||
146 | foreach( $this->getSubClients() as $client ) { |
||
147 | $client->delete(); |
||
148 | } |
||
149 | |||
150 | $manager->saveItem( $view->item ); |
||
151 | } |
||
152 | |||
153 | $manager->deleteItems( array_keys( $items ) ); |
||
154 | $manager->commit(); |
||
155 | |||
156 | $this->nextAction( $view, 'search', 'supplier', null, 'delete' ); |
||
157 | return; |
||
158 | } |
||
159 | catch( \Aimeos\MShop\Exception $e ) |
||
160 | { |
||
161 | $error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
162 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
163 | $this->logException( $e ); |
||
164 | } |
||
165 | catch( \Exception $e ) |
||
166 | { |
||
167 | $error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
168 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
169 | $this->logException( $e ); |
||
170 | } |
||
171 | |||
172 | $manager->rollback(); |
||
173 | |||
174 | return $this->search(); |
||
175 | } |
||
176 | |||
177 | |||
178 | /** |
||
179 | * Returns a single resource |
||
180 | * |
||
181 | * @return string HTML output |
||
182 | */ |
||
183 | public function get() |
||
184 | { |
||
185 | $view = $this->getView(); |
||
186 | $context = $this->getContext(); |
||
187 | |||
188 | try |
||
189 | { |
||
190 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
191 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
192 | } |
||
193 | |||
194 | $manager = \Aimeos\MShop::create( $context, 'supplier' ); |
||
195 | |||
196 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
197 | $view->itemSubparts = $this->getSubClientNames(); |
||
198 | $view->itemData = $this->toArray( $view->item ); |
||
199 | $view->itemBody = ''; |
||
200 | |||
201 | foreach( $this->getSubClients() as $idx => $client ) |
||
202 | { |
||
203 | $view->tabindex = ++$idx + 1; |
||
204 | $view->itemBody .= $client->get(); |
||
205 | } |
||
206 | } |
||
207 | catch( \Aimeos\MShop\Exception $e ) |
||
208 | { |
||
209 | $error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
210 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
211 | $this->logException( $e ); |
||
212 | } |
||
213 | catch( \Exception $e ) |
||
214 | { |
||
215 | $error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
216 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
217 | $this->logException( $e ); |
||
218 | } |
||
219 | |||
220 | return $this->render( $view ); |
||
221 | } |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Saves the data |
||
226 | * |
||
227 | * @return string HTML output |
||
228 | */ |
||
229 | public function save() |
||
230 | { |
||
231 | $view = $this->getView(); |
||
232 | $context = $this->getContext(); |
||
233 | |||
234 | $manager = \Aimeos\MShop::create( $context, 'supplier' ); |
||
235 | $manager->begin(); |
||
236 | |||
237 | try |
||
238 | { |
||
239 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
240 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
241 | $view->itemBody = ''; |
||
242 | |||
243 | foreach( $this->getSubClients() as $client ) { |
||
244 | $view->itemBody .= $client->save(); |
||
245 | } |
||
246 | |||
247 | $manager->saveItem( clone $view->item ); |
||
248 | $manager->commit(); |
||
249 | |||
250 | $this->nextAction( $view, $view->param( 'next' ), 'supplier', $view->item->getId(), 'save' ); |
||
251 | return; |
||
252 | } |
||
253 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
254 | { |
||
255 | // fall through to create |
||
256 | } |
||
257 | catch( \Aimeos\MShop\Exception $e ) |
||
258 | { |
||
259 | $error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
260 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
261 | $this->logException( $e ); |
||
262 | } |
||
263 | catch( \Exception $e ) |
||
264 | { |
||
265 | $error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
266 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
267 | $this->logException( $e ); |
||
268 | } |
||
269 | |||
270 | $manager->rollback(); |
||
271 | |||
272 | return $this->create(); |
||
273 | } |
||
274 | |||
275 | |||
276 | /** |
||
277 | * Returns a list of resource according to the conditions |
||
278 | * |
||
279 | * @return string HTML output |
||
280 | */ |
||
281 | public function search() |
||
282 | { |
||
283 | $view = $this->getView(); |
||
284 | $context = $this->getContext(); |
||
285 | |||
286 | try |
||
287 | { |
||
288 | $total = 0; |
||
289 | $params = $this->storeSearchParams( $view->param(), 'supplier' ); |
||
290 | $manager = \Aimeos\MShop::create( $context, 'supplier' ); |
||
291 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
292 | |||
293 | $view->items = $manager->searchItems( $search, $this->getDomains(), $total ); |
||
294 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
295 | $view->filterOperators = $search->getOperators(); |
||
296 | $view->total = $total; |
||
297 | $view->itemBody = ''; |
||
298 | |||
299 | foreach( $this->getSubClients() as $client ) { |
||
300 | $view->itemBody .= $client->search(); |
||
301 | } |
||
302 | } |
||
303 | catch( \Aimeos\MShop\Exception $e ) |
||
304 | { |
||
305 | $error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
306 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
307 | $this->logException( $e ); |
||
308 | } |
||
309 | catch( \Exception $e ) |
||
310 | { |
||
311 | $error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
312 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
313 | $this->logException( $e ); |
||
314 | } |
||
315 | |||
316 | /** admin/jqadm/supplier/template-list |
||
317 | * Relative path to the HTML body template for the supplier list. |
||
318 | * |
||
319 | * The template file contains the HTML code and processing instructions |
||
320 | * to generate the result shown in the body of the frontend. The |
||
321 | * configuration string is the path to the template file relative |
||
322 | * to the templates directory (usually in admin/jqadm/templates). |
||
323 | * |
||
324 | * You can overwrite the template file configuration in extensions and |
||
325 | * provide alternative templates. These alternative templates should be |
||
326 | * named like the default one but with the string "default" replaced by |
||
327 | * an unique name. You may use the name of your project for this. If |
||
328 | * you've implemented an alternative client class as well, "default" |
||
329 | * should be replaced by the name of the new class. |
||
330 | * |
||
331 | * @param string Relative path to the template creating the HTML code |
||
332 | * @since 2016.04 |
||
333 | * @category Developer |
||
334 | */ |
||
335 | $tplconf = 'admin/jqadm/supplier/template-list'; |
||
336 | $default = 'supplier/list-standard'; |
||
337 | |||
338 | return $view->render( $view->config( $tplconf, $default ) ); |
||
339 | } |
||
340 | |||
341 | |||
342 | /** |
||
343 | * Returns the sub-client given by its name. |
||
344 | * |
||
345 | * @param string $type Name of the client type |
||
346 | * @param string|null $name Name of the sub-client (Default if null) |
||
347 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
348 | */ |
||
349 | public function getSubClient( $type, $name = null ) |
||
350 | { |
||
351 | /** admin/jqadm/supplier/decorators/excludes |
||
352 | * Excludes decorators added by the "common" option from the supplier JQAdm client |
||
353 | * |
||
354 | * Decorators extend the functionality of a class by adding new aspects |
||
355 | * (e.g. log what is currently done), executing the methods of the underlying |
||
356 | * class only in certain conditions (e.g. only for logged in users) or |
||
357 | * modify what is returned to the caller. |
||
358 | * |
||
359 | * This option allows you to remove a decorator added via |
||
360 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
361 | * around the JQAdm client. |
||
362 | * |
||
363 | * admin/jqadm/supplier/decorators/excludes = array( 'decorator1' ) |
||
364 | * |
||
365 | * This would remove the decorator named "decorator1" from the list of |
||
366 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
367 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
368 | * |
||
369 | * @param array List of decorator names |
||
370 | * @since 2017.10 |
||
371 | * @category Developer |
||
372 | * @see admin/jqadm/common/decorators/default |
||
373 | * @see admin/jqadm/supplier/decorators/global |
||
374 | * @see admin/jqadm/supplier/decorators/local |
||
375 | */ |
||
376 | |||
377 | /** admin/jqadm/supplier/decorators/global |
||
378 | * Adds a list of globally available decorators only to the supplier JQAdm client |
||
379 | * |
||
380 | * Decorators extend the functionality of a class by adding new aspects |
||
381 | * (e.g. log what is currently done), executing the methods of the underlying |
||
382 | * class only in certain conditions (e.g. only for logged in users) or |
||
383 | * modify what is returned to the caller. |
||
384 | * |
||
385 | * This option allows you to wrap global decorators |
||
386 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
387 | * |
||
388 | * admin/jqadm/supplier/decorators/global = array( 'decorator1' ) |
||
389 | * |
||
390 | * This would add the decorator named "decorator1" defined by |
||
391 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
392 | * |
||
393 | * @param array List of decorator names |
||
394 | * @since 2017.10 |
||
395 | * @category Developer |
||
396 | * @see admin/jqadm/common/decorators/default |
||
397 | * @see admin/jqadm/supplier/decorators/excludes |
||
398 | * @see admin/jqadm/supplier/decorators/local |
||
399 | */ |
||
400 | |||
401 | /** admin/jqadm/supplier/decorators/local |
||
402 | * Adds a list of local decorators only to the supplier JQAdm client |
||
403 | * |
||
404 | * Decorators extend the functionality of a class by adding new aspects |
||
405 | * (e.g. log what is currently done), executing the methods of the underlying |
||
406 | * class only in certain conditions (e.g. only for logged in users) or |
||
407 | * modify what is returned to the caller. |
||
408 | * |
||
409 | * This option allows you to wrap local decorators |
||
410 | * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client. |
||
411 | * |
||
412 | * admin/jqadm/supplier/decorators/local = array( 'decorator2' ) |
||
413 | * |
||
414 | * This would add the decorator named "decorator2" defined by |
||
415 | * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client. |
||
416 | * |
||
417 | * @param array List of decorator names |
||
418 | * @since 2017.10 |
||
419 | * @category Developer |
||
420 | * @see admin/jqadm/common/decorators/default |
||
421 | * @see admin/jqadm/supplier/decorators/excludes |
||
422 | * @see admin/jqadm/supplier/decorators/global |
||
423 | */ |
||
424 | return $this->createSubClient( 'supplier/' . $type, $name ); |
||
425 | } |
||
426 | |||
427 | |||
428 | /** |
||
429 | * Returns the domain names whose items should be fetched too |
||
430 | * |
||
431 | * @return string[] List of domain names |
||
432 | */ |
||
433 | protected function getDomains() |
||
434 | { |
||
435 | /** admin/jqadm/supplier/domains |
||
436 | * List of domain items that should be fetched along with the supplier |
||
437 | * |
||
438 | * If you need to display additional content, you can configure your own |
||
439 | * list of domains (attribute, media, price, supplier, text, etc. are |
||
440 | * domains) whose items are fetched from the storage. |
||
441 | * |
||
442 | * @param array List of domain names |
||
443 | * @since 2017.10 |
||
444 | * @category Developer |
||
445 | */ |
||
446 | $domains = ['media', 'supplier/address', 'text']; |
||
447 | |||
448 | return $this->getContext()->getConfig()->get( 'admin/jqadm/supplier/domains', $domains ); |
||
449 | } |
||
450 | |||
451 | |||
452 | /** |
||
453 | * Returns the list of sub-client names configured for the client. |
||
454 | * |
||
455 | * @return array List of JQAdm client names |
||
456 | */ |
||
457 | protected function getSubClientNames() |
||
493 | } |
||
494 | |||
495 | |||
496 | |||
497 | /** |
||
498 | * Creates new and updates existing items using the data array |
||
499 | * |
||
500 | * @param array $data Data array |
||
501 | * @return \Aimeos\MShop\Supplier\Item\Iface New supplier item object |
||
502 | */ |
||
503 | protected function fromArray( array $data ) |
||
504 | { |
||
505 | $manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' ); |
||
506 | |||
507 | if( isset( $data['supplier.id'] ) && $data['supplier.id'] != '' ) { |
||
508 | $item = $manager->getItem( $data['supplier.id'], $this->getDomains() ); |
||
509 | } else { |
||
510 | $item = $manager->createItem(); |
||
511 | } |
||
512 | |||
513 | $item->fromArray( $data, true ); |
||
514 | |||
515 | return $item; |
||
516 | } |
||
517 | |||
518 | |||
519 | /** |
||
520 | * Constructs the data array for the view from the given item |
||
521 | * |
||
522 | * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object |
||
523 | * @return string[] Multi-dimensional associative list of item data |
||
524 | */ |
||
525 | protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, $copy = false ) |
||
526 | { |
||
527 | $data = $item->toArray( true ); |
||
528 | |||
529 | if( $copy === true ) |
||
530 | { |
||
531 | $data['supplier.siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
532 | $data['supplier.code'] = $data['supplier.code'] . '_copy'; |
||
533 | $data['supplier.id'] = ''; |
||
534 | } |
||
535 | |||
536 | return $data; |
||
537 | } |
||
538 | |||
539 | |||
540 | /** |
||
541 | * Returns the rendered template including the view data |
||
542 | * |
||
543 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
544 | * @return string HTML output |
||
545 | */ |
||
546 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
571 | } |
||
572 | } |
||
573 |