Total Complexity | 44 |
Total Lines | 586 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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() |
||
32 | { |
||
33 | $view = $this->getView(); |
||
34 | $context = $this->getContext(); |
||
35 | |||
36 | try |
||
37 | { |
||
38 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
39 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
40 | } |
||
41 | |||
42 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
43 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
44 | |||
45 | $view->itemData = $this->toArray( $view->item, true ); |
||
46 | $view->itemSubparts = $this->getSubClientNames(); |
||
47 | $view->itemTypes = $this->getTypeItems(); |
||
48 | $view->itemBody = ''; |
||
49 | |||
50 | foreach( $this->getSubClients() as $idx => $client ) |
||
51 | { |
||
52 | $view->tabindex = ++$idx + 1; |
||
53 | $view->itemBody .= $client->copy(); |
||
54 | } |
||
55 | } |
||
56 | catch( \Aimeos\MShop\Exception $e ) |
||
57 | { |
||
58 | $error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
59 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
60 | $this->logException( $e ); |
||
61 | } |
||
62 | catch( \Exception $e ) |
||
63 | { |
||
64 | $error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
65 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
66 | $this->logException( $e ); |
||
67 | } |
||
68 | |||
69 | return $this->render( $view ); |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Creates a new resource |
||
75 | * |
||
76 | * @return string HTML output |
||
77 | */ |
||
78 | public function create() |
||
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Deletes a resource |
||
123 | * |
||
124 | * @return string|null HTML output |
||
125 | */ |
||
126 | public function delete() |
||
127 | { |
||
128 | $view = $this->getView(); |
||
129 | $context = $this->getContext(); |
||
130 | |||
131 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
132 | $manager->begin(); |
||
133 | |||
134 | try |
||
135 | { |
||
136 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
137 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
138 | } |
||
139 | |||
140 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
141 | $search->setConditions( $search->compare( '==', 'product.id', $ids ) ); |
||
142 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
143 | |||
144 | foreach( $items as $item ) |
||
145 | { |
||
146 | $view->item = $item; |
||
147 | |||
148 | foreach( $this->getSubClients() as $client ) { |
||
149 | $client->delete(); |
||
150 | } |
||
151 | |||
152 | $manager->saveItem( $view->item ); |
||
|
|||
153 | } |
||
154 | |||
155 | $manager->deleteItems( array_keys( $items ) ); |
||
156 | $manager->commit(); |
||
157 | |||
158 | $this->nextAction( $view, 'search', 'product', null, 'delete' ); |
||
159 | return; |
||
160 | } |
||
161 | catch( \Aimeos\MShop\Exception $e ) |
||
162 | { |
||
163 | $error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
164 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
165 | $this->logException( $e ); |
||
166 | } |
||
167 | catch( \Exception $e ) |
||
168 | { |
||
169 | $error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
170 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
171 | $this->logException( $e ); |
||
172 | } |
||
173 | |||
174 | $manager->rollback(); |
||
175 | |||
176 | return $this->search(); |
||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Returns a single resource |
||
182 | * |
||
183 | * @return string HTML output |
||
184 | */ |
||
185 | public function get() |
||
186 | { |
||
187 | $view = $this->getView(); |
||
188 | $context = $this->getContext(); |
||
189 | |||
190 | try |
||
191 | { |
||
192 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
193 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
194 | } |
||
195 | |||
196 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
197 | |||
198 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
199 | $view->itemSubparts = $this->getSubClientNames(); |
||
200 | $view->itemData = $this->toArray( $view->item ); |
||
201 | $view->itemTypes = $this->getTypeItems(); |
||
202 | $view->itemBody = ''; |
||
203 | |||
204 | foreach( $this->getSubClients() as $idx => $client ) |
||
205 | { |
||
206 | $view->tabindex = ++$idx + 1; |
||
207 | $view->itemBody .= $client->get(); |
||
208 | } |
||
209 | } |
||
210 | catch( \Aimeos\MShop\Exception $e ) |
||
211 | { |
||
212 | $error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
213 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
214 | $this->logException( $e ); |
||
215 | } |
||
216 | catch( \Exception $e ) |
||
217 | { |
||
218 | $error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
219 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
220 | $this->logException( $e ); |
||
221 | } |
||
222 | |||
223 | return $this->render( $view ); |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Saves the data |
||
229 | * |
||
230 | * @return string HTML output |
||
231 | */ |
||
232 | public function save() |
||
233 | { |
||
234 | $view = $this->getView(); |
||
235 | $context = $this->getContext(); |
||
236 | |||
237 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
238 | $manager->begin(); |
||
239 | |||
240 | try |
||
241 | { |
||
242 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
243 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
244 | $view->itemBody = ''; |
||
245 | |||
246 | foreach( $this->getSubClients() as $client ) { |
||
247 | $view->itemBody .= $client->save(); |
||
248 | } |
||
249 | |||
250 | $manager->saveItem( clone $view->item ); |
||
251 | $manager->commit(); |
||
252 | |||
253 | $this->nextAction( $view, $view->param( 'next' ), 'product', $view->item->getId(), 'save' ); |
||
254 | return; |
||
255 | } |
||
256 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
257 | { |
||
258 | // fall through to create |
||
259 | } |
||
260 | catch( \Aimeos\MShop\Exception $e ) |
||
261 | { |
||
262 | $error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
263 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
264 | $this->logException( $e ); |
||
265 | } |
||
266 | catch( \Exception $e ) |
||
267 | { |
||
268 | $error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
269 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
270 | $this->logException( $e ); |
||
271 | } |
||
272 | |||
273 | $manager->rollback(); |
||
274 | |||
275 | return $this->create(); |
||
276 | } |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Returns a list of resource according to the conditions |
||
281 | * |
||
282 | * @return string HTML output |
||
283 | */ |
||
284 | public function search() |
||
285 | { |
||
286 | $view = $this->getView(); |
||
287 | $context = $this->getContext(); |
||
288 | |||
289 | try |
||
290 | { |
||
291 | $total = 0; |
||
292 | $params = $this->storeSearchParams( $view->param(), 'product' ); |
||
293 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
294 | |||
295 | $search = $manager->createSearch(); |
||
296 | $search->setSortations( [$search->sort( '+', 'product.id')] ); |
||
297 | $search = $this->initCriteria( $search, $params ); |
||
298 | |||
299 | $view->items = $manager->searchItems( $search, array_diff( $this->getDomains(), ['product'] ), $total ); |
||
300 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
301 | $view->filterOperators = $search->getOperators(); |
||
302 | $view->itemTypes = $this->getTypeItems(); |
||
303 | $view->total = $total; |
||
304 | $view->itemBody = ''; |
||
305 | |||
306 | foreach( $this->getSubClients() as $client ) { |
||
307 | $view->itemBody .= $client->search(); |
||
308 | } |
||
309 | } |
||
310 | catch( \Aimeos\MShop\Exception $e ) |
||
311 | { |
||
312 | $error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
313 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
314 | $this->logException( $e ); |
||
315 | } |
||
316 | catch( \Exception $e ) |
||
317 | { |
||
318 | $error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
319 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
320 | $this->logException( $e ); |
||
321 | } |
||
322 | |||
323 | /** admin/jqadm/product/template-list |
||
324 | * Relative path to the HTML body template for the product list. |
||
325 | * |
||
326 | * The template file contains the HTML code and processing instructions |
||
327 | * to generate the result shown in the body of the frontend. The |
||
328 | * configuration string is the path to the template file relative |
||
329 | * to the templates directory (usually in admin/jqadm/templates). |
||
330 | * |
||
331 | * You can overwrite the template file configuration in extensions and |
||
332 | * provide alternative templates. These alternative templates should be |
||
333 | * named like the default one but with the string "default" replaced by |
||
334 | * an unique name. You may use the name of your project for this. If |
||
335 | * you've implemented an alternative client class as well, "default" |
||
336 | * should be replaced by the name of the new class. |
||
337 | * |
||
338 | * @param string Relative path to the template creating the HTML code |
||
339 | * @since 2016.04 |
||
340 | * @category Developer |
||
341 | */ |
||
342 | $tplconf = 'admin/jqadm/product/template-list'; |
||
343 | $default = 'product/list-standard'; |
||
344 | |||
345 | return $view->render( $view->config( $tplconf, $default ) ); |
||
346 | } |
||
347 | |||
348 | |||
349 | /** |
||
350 | * Returns the sub-client given by its name. |
||
351 | * |
||
352 | * @param string $type Name of the client type |
||
353 | * @param string|null $name Name of the sub-client (Default if null) |
||
354 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
355 | */ |
||
356 | public function getSubClient( $type, $name = null ) |
||
432 | } |
||
433 | |||
434 | |||
435 | /** |
||
436 | * Returns the domain names whose items should be fetched too |
||
437 | * |
||
438 | * @return string[] List of domain names |
||
439 | */ |
||
440 | protected function getDomains() |
||
441 | { |
||
442 | /** admin/jqadm/product/domains |
||
443 | * List of domain items that should be fetched along with the product |
||
444 | * |
||
445 | * If you need to display additional content, you can configure your own |
||
446 | * list of domains (attribute, media, price, product, text, etc. are |
||
447 | * domains) whose items are fetched from the storage. |
||
448 | * |
||
449 | * @param array List of domain names |
||
450 | * @since 2016.01 |
||
451 | * @category Developer |
||
452 | */ |
||
453 | $domains = array( 'attribute', 'media', 'price', 'product', 'product/property', 'text' ); |
||
454 | |||
455 | return $this->getContext()->getConfig()->get( 'admin/jqadm/product/domains', $domains ); |
||
456 | } |
||
457 | |||
458 | |||
459 | /** |
||
460 | * Returns the list of sub-client names configured for the client. |
||
461 | * |
||
462 | * @return array List of JQAdm client names |
||
463 | */ |
||
464 | protected function getSubClientNames() |
||
465 | { |
||
466 | /** admin/jqadm/product/standard/subparts |
||
467 | * List of JQAdm sub-clients rendered within the product section |
||
468 | * |
||
469 | * The output of the frontend is composed of the code generated by the JQAdm |
||
470 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
471 | * that are responsible for rendering certain sub-parts of the output. The |
||
472 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
473 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
474 | * the output that is placed inside the container of its parent. |
||
475 | * |
||
476 | * At first, always the JQAdm code generated by the parent is printed, then |
||
477 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
478 | * determines the order of the output of these sub-clients inside the parent |
||
479 | * container. If the configured list of clients is |
||
480 | * |
||
481 | * array( "subclient1", "subclient2" ) |
||
482 | * |
||
483 | * you can easily change the order of the output by reordering the subparts: |
||
484 | * |
||
485 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
486 | * |
||
487 | * You can also remove one or more parts if they shouldn't be rendered: |
||
488 | * |
||
489 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
490 | * |
||
491 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
492 | * should support adding, removing or reordering content by a fluid like |
||
493 | * design. |
||
494 | * |
||
495 | * @param array List of sub-client names |
||
496 | * @since 2016.01 |
||
497 | * @category Developer |
||
498 | */ |
||
499 | return $this->getContext()->getConfig()->get( 'admin/jqadm/product/standard/subparts', [] ); |
||
500 | } |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Returns the available product type items |
||
505 | * |
||
506 | * @return array List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
507 | */ |
||
508 | protected function getTypeItems() |
||
516 | } |
||
517 | |||
518 | |||
519 | /** |
||
520 | * Creates new and updates existing items using the data array |
||
521 | * |
||
522 | * @param array $data Data array |
||
523 | * @return \Aimeos\MShop\Product\Item\Iface New product item object |
||
524 | */ |
||
525 | protected function fromArray( array $data ) |
||
526 | { |
||
527 | $conf = []; |
||
528 | |||
529 | foreach( (array) $this->getValue( $data, 'config', [] ) as $idx => $entry ) |
||
530 | { |
||
531 | if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) { |
||
532 | $conf[$key] = trim( $entry['val'] ?? '' ); |
||
533 | } |
||
534 | } |
||
535 | |||
536 | $manager = \Aimeos\MShop::create( $this->getContext(), 'index' ); |
||
537 | |||
538 | if( isset( $data['product.id'] ) && $data['product.id'] != '' ) { |
||
539 | $item = $manager->getItem( $data['product.id'], $this->getDomains() ); |
||
540 | } else { |
||
541 | $item = $manager->createItem(); |
||
542 | } |
||
543 | |||
544 | $item->fromArray( $data, true ); |
||
545 | $item->setConfig( $conf ); |
||
546 | |||
547 | return $item; |
||
548 | } |
||
549 | |||
550 | |||
551 | /** |
||
552 | * Constructs the data array for the view from the given item |
||
553 | * |
||
554 | * @param \Aimeos\MShop\Product\Item\Iface $item Product item object |
||
555 | * @return string[] Multi-dimensional associative list of item data |
||
556 | */ |
||
557 | protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, $copy = false ) |
||
574 | } |
||
575 | |||
576 | |||
577 | /** |
||
578 | * Returns the rendered template including the view data |
||
579 | * |
||
580 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
581 | * @return string HTML output |
||
582 | */ |
||
583 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
608 | } |
||
609 | } |
||
610 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.