Total Complexity | 51 |
Total Lines | 567 |
Duplicated Lines | 0 % |
Changes | 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() |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Creates a new resource |
||
75 | * |
||
76 | * @return string HTML output |
||
77 | */ |
||
78 | public function create() |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Deletes a resource |
||
124 | * |
||
125 | * @return string|null HTML output |
||
126 | */ |
||
127 | public function delete() |
||
128 | { |
||
129 | $view = $this->getView(); |
||
130 | $context = $this->getContext(); |
||
131 | |||
132 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
133 | $manager->begin(); |
||
134 | |||
135 | try |
||
136 | { |
||
137 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
138 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
139 | } |
||
140 | |||
141 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
142 | $search->setConditions( $search->compare( '==', 'catalog.id', $ids ) ); |
||
143 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
144 | |||
145 | foreach( $items as $item ) |
||
146 | { |
||
147 | $view->item = $item; |
||
148 | |||
149 | foreach( $this->getSubClients() as $client ) { |
||
150 | $client->delete(); |
||
151 | } |
||
152 | |||
153 | $manager->saveItem( $view->item ); |
||
154 | } |
||
155 | |||
156 | $manager->deleteItems( array_keys( $items ) ); |
||
157 | $manager->commit(); |
||
158 | |||
159 | $this->nextAction( $view, 'search', 'catalog', null, 'delete' ); |
||
160 | return; |
||
161 | } |
||
162 | catch( \Aimeos\MShop\Exception $e ) |
||
163 | { |
||
164 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
165 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
166 | $this->logException( $e ); |
||
167 | } |
||
168 | catch( \Exception $e ) |
||
169 | { |
||
170 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
171 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
172 | $this->logException( $e ); |
||
173 | } |
||
174 | |||
175 | $manager->rollback(); |
||
176 | |||
177 | return $this->search(); |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Returns a single resource |
||
183 | * |
||
184 | * @return string HTML output |
||
185 | */ |
||
186 | public function get() |
||
187 | { |
||
188 | $view = $this->getView(); |
||
189 | $context = $this->getContext(); |
||
190 | |||
191 | try |
||
192 | { |
||
193 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
194 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
195 | } |
||
196 | |||
197 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
198 | |||
199 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
200 | $view->itemSubparts = $this->getSubClientNames(); |
||
201 | $view->itemData = $this->toArray( $view->item ); |
||
202 | $view->itemRootId = $this->getRootId(); |
||
203 | $view->itemBody = ''; |
||
204 | |||
205 | foreach( $this->getSubClients() as $idx => $client ) |
||
206 | { |
||
207 | $view->tabindex = ++$idx + 1; |
||
208 | $view->itemBody .= $client->get(); |
||
209 | } |
||
210 | } |
||
211 | catch( \Aimeos\MShop\Exception $e ) |
||
212 | { |
||
213 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
214 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
215 | $this->logException( $e ); |
||
216 | } |
||
217 | catch( \Exception $e ) |
||
218 | { |
||
219 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
220 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
221 | $this->logException( $e ); |
||
222 | } |
||
223 | |||
224 | return $this->render( $view ); |
||
225 | } |
||
226 | |||
227 | |||
228 | /** |
||
229 | * Saves the data |
||
230 | * |
||
231 | * @return string HTML output |
||
232 | */ |
||
233 | public function save() |
||
234 | { |
||
235 | $view = $this->getView(); |
||
236 | $context = $this->getContext(); |
||
237 | |||
238 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
239 | $manager->begin(); |
||
240 | |||
241 | try |
||
242 | { |
||
243 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
244 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
245 | $view->itemBody = ''; |
||
246 | |||
247 | foreach( $this->getSubClients() as $client ) { |
||
248 | $view->itemBody .= $client->save(); |
||
249 | } |
||
250 | |||
251 | $manager->saveItem( clone $view->item ); |
||
252 | $manager->commit(); |
||
253 | |||
254 | $action = $view->param( 'next' ); |
||
255 | $id = ( $action === 'create' ? $view->item->getParentId() : $view->item->getId() ); |
||
|
|||
256 | |||
257 | $this->nextAction( $view, $action, 'catalog', $id, 'save' ); |
||
258 | return; |
||
259 | } |
||
260 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
261 | { |
||
262 | // fall through to create |
||
263 | } |
||
264 | catch( \Aimeos\MShop\Exception $e ) |
||
265 | { |
||
266 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
267 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
268 | $this->logException( $e ); |
||
269 | } |
||
270 | catch( \Exception $e ) |
||
271 | { |
||
272 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
273 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
274 | $this->logException( $e ); |
||
275 | } |
||
276 | |||
277 | $manager->rollback(); |
||
278 | |||
279 | return $this->create(); |
||
280 | } |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Returns the catalog root node |
||
285 | * |
||
286 | * @return string HTML output |
||
287 | */ |
||
288 | public function search() |
||
289 | { |
||
290 | $view = $this->getView(); |
||
291 | $context = $this->getContext(); |
||
292 | |||
293 | try |
||
294 | { |
||
295 | $view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem(); |
||
296 | $view->itemRootId = $this->getRootId(); |
||
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( 'catalog-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( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
312 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
313 | $this->logException( $e ); |
||
314 | } |
||
315 | |||
316 | return $this->render( $view ); |
||
317 | } |
||
318 | |||
319 | |||
320 | /** |
||
321 | * Returns the sub-client given by its name. |
||
322 | * |
||
323 | * @param string $type Name of the client type |
||
324 | * @param string|null $name Name of the sub-client (Default if null) |
||
325 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
326 | */ |
||
327 | public function getSubClient( $type, $name = null ) |
||
403 | } |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Returns the domain names whose items should be fetched too |
||
408 | * |
||
409 | * @return string[] List of domain names |
||
410 | */ |
||
411 | protected function getDomains() |
||
412 | { |
||
413 | /** admin/jqadm/catalog/domains |
||
414 | * List of domain items that should be fetched along with the catalog |
||
415 | * |
||
416 | * If you need to display additional content, you can configure your own |
||
417 | * list of domains (attribute, media, price, catalog, text, etc. are |
||
418 | * domains) whose items are fetched from the storage. |
||
419 | * |
||
420 | * @param array List of domain names |
||
421 | * @since 2016.01 |
||
422 | * @category Developer |
||
423 | */ |
||
424 | $domains = array( 'media', 'text' ); |
||
425 | |||
426 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/domains', $domains ); |
||
427 | } |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Returns the IDs of the root category |
||
432 | * |
||
433 | * @return string|null ID of the root category |
||
434 | */ |
||
435 | protected function getRootId() |
||
436 | { |
||
437 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
438 | |||
439 | try { |
||
440 | return $manager->getTree( null, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE )->getId(); |
||
441 | } catch( \Exception $e ) { |
||
442 | return null; |
||
443 | } |
||
444 | } |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Returns the list of sub-client names configured for the client. |
||
449 | * |
||
450 | * @return array List of JQAdm client names |
||
451 | */ |
||
452 | protected function getSubClientNames() |
||
453 | { |
||
454 | /** admin/jqadm/catalog/standard/subparts |
||
455 | * List of JQAdm sub-clients rendered within the catalog section |
||
456 | * |
||
457 | * The output of the frontend is composed of the code generated by the JQAdm |
||
458 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
459 | * that are responsible for rendering certain sub-parts of the output. The |
||
460 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
461 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
462 | * the output that is placed inside the container of its parent. |
||
463 | * |
||
464 | * At first, always the JQAdm code generated by the parent is printed, then |
||
465 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
466 | * determines the order of the output of these sub-clients inside the parent |
||
467 | * container. If the configured list of clients is |
||
468 | * |
||
469 | * array( "subclient1", "subclient2" ) |
||
470 | * |
||
471 | * you can easily change the order of the output by reordering the subparts: |
||
472 | * |
||
473 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
474 | * |
||
475 | * You can also remove one or more parts if they shouldn't be rendered: |
||
476 | * |
||
477 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
478 | * |
||
479 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
480 | * should support adding, removing or reordering content by a fluid like |
||
481 | * design. |
||
482 | * |
||
483 | * @param array List of sub-client names |
||
484 | * @since 2016.01 |
||
485 | * @category Developer |
||
486 | */ |
||
487 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/standard/subparts', [] ); |
||
488 | } |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Creates new and updates existing items using the data array |
||
493 | * |
||
494 | * @param array $data Data array |
||
495 | * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object |
||
496 | */ |
||
497 | protected function fromArray( array $data ) |
||
498 | { |
||
499 | $conf = []; |
||
500 | |||
501 | if( isset( $data['config']['key'] ) ) |
||
502 | { |
||
503 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
504 | { |
||
505 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) { |
||
506 | $conf[$key] = $data['config']['val'][$idx]; |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | |||
511 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
512 | |||
513 | if( isset( $data['catalog.id'] ) && $data['catalog.id'] != '' ) { |
||
514 | $item = $manager->getItem( $data['catalog.id'], $this->getDomains() ); |
||
515 | } else { |
||
516 | $item = $manager->createItem(); |
||
517 | } |
||
518 | |||
519 | $item->fromArray( $data, true ); |
||
520 | $item->setConfig( $conf ); |
||
521 | |||
522 | if( $item->getId() == null ) { |
||
523 | return $manager->insertItem( $item, $data['catalog.parentid'] ?: null ); |
||
524 | } |
||
525 | |||
526 | return $item; |
||
527 | } |
||
528 | |||
529 | |||
530 | /** |
||
531 | * Constructs the data array for the view from the given item |
||
532 | * |
||
533 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object |
||
534 | * @return string[] Multi-dimensional associative list of item data |
||
535 | */ |
||
536 | protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, $copy = false ) |
||
555 | } |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Returns the rendered template including the view data |
||
560 | * |
||
561 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
562 | * @return string HTML output |
||
563 | */ |
||
564 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
589 | } |
||
590 | } |
||
591 |