Total Complexity | 52 |
Total Lines | 617 |
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() |
||
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 | $this->checkSite( $view->access( 'super' ), $id ); |
||
43 | |||
44 | $manager = \Aimeos\MShop::create( $context, 'locale/site' ); |
||
45 | $view->item = $manager->getItem( $id ); |
||
46 | |||
47 | $view->itemData = $this->toArray( $view->item, true ); |
||
48 | $view->itemSubparts = $this->getSubClientNames(); |
||
49 | $view->itemBody = ''; |
||
50 | |||
51 | foreach( $this->getSubClients() as $idx => $client ) |
||
52 | { |
||
53 | $view->tabindex = ++$idx + 1; |
||
54 | $view->itemBody .= $client->copy(); |
||
55 | } |
||
56 | } |
||
57 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
58 | { |
||
59 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'admin', $e->getMessage() ) ); |
||
60 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
61 | $this->logException( $e ); |
||
62 | } |
||
63 | catch( \Aimeos\MShop\Exception $e ) |
||
64 | { |
||
65 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
66 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
67 | $this->logException( $e ); |
||
68 | } |
||
69 | catch( \Exception $e ) |
||
70 | { |
||
71 | $error = array( 'locale-site-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
72 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
73 | $this->logException( $e ); |
||
74 | } |
||
75 | |||
76 | return $this->render( $view ); |
||
77 | } |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Creates a new resource |
||
82 | * |
||
83 | * @return string HTML output |
||
84 | */ |
||
85 | public function create() |
||
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Deletes a resource |
||
135 | * |
||
136 | * @return string|null HTML output |
||
137 | */ |
||
138 | public function delete() |
||
139 | { |
||
140 | $view = $this->getView(); |
||
141 | $context = $this->getContext(); |
||
142 | |||
143 | $manager = \Aimeos\MShop::create( $context, 'locale/site' ); |
||
144 | $manager->begin(); |
||
145 | |||
146 | try |
||
147 | { |
||
148 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
149 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
150 | } |
||
151 | |||
152 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
153 | $search->setConditions( $search->compare( '==', 'locale.site.id', $ids ) ); |
||
154 | $items = $manager->searchItems( $search ); |
||
155 | |||
156 | foreach( $items as $id => $item ) |
||
157 | { |
||
158 | $this->checkSite( $view->access( 'super' ), $id ); |
||
159 | $view->item = $item; |
||
160 | |||
161 | foreach( $this->getSubClients() as $client ) { |
||
162 | $client->delete(); |
||
163 | } |
||
164 | |||
165 | $manager->saveItem( $view->item ); |
||
|
|||
166 | } |
||
167 | |||
168 | $manager->deleteItems( array_keys( $items ) ); |
||
169 | $manager->commit(); |
||
170 | |||
171 | $this->nextAction( $view, 'search', 'locale/site', null, 'delete' ); |
||
172 | return; |
||
173 | } |
||
174 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
175 | { |
||
176 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'admin', $e->getMessage() ) ); |
||
177 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
178 | $this->logException( $e ); |
||
179 | } |
||
180 | catch( \Aimeos\MShop\Exception $e ) |
||
181 | { |
||
182 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
183 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
184 | $this->logException( $e ); |
||
185 | } |
||
186 | catch( \Exception $e ) |
||
187 | { |
||
188 | $error = array( 'locale-site-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
189 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
190 | $this->logException( $e ); |
||
191 | } |
||
192 | |||
193 | $manager->rollback(); |
||
194 | |||
195 | return $this->search(); |
||
196 | } |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Returns a single resource |
||
201 | * |
||
202 | * @return string HTML output |
||
203 | */ |
||
204 | public function get() |
||
205 | { |
||
206 | $view = $this->getView(); |
||
207 | $context = $this->getContext(); |
||
208 | |||
209 | try |
||
210 | { |
||
211 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
212 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
213 | } |
||
214 | |||
215 | $this->checkSite( $view->access( 'super' ), $id ); |
||
216 | |||
217 | $manager = \Aimeos\MShop::create( $context, 'locale/site' ); |
||
218 | |||
219 | $view->item = $manager->getItem( $id ); |
||
220 | $view->itemSubparts = $this->getSubClientNames(); |
||
221 | $view->itemData = $this->toArray( $view->item ); |
||
222 | $view->itemBody = ''; |
||
223 | |||
224 | foreach( $this->getSubClients() as $idx => $client ) |
||
225 | { |
||
226 | $view->tabindex = ++$idx + 1; |
||
227 | $view->itemBody .= $client->get(); |
||
228 | } |
||
229 | } |
||
230 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
231 | { |
||
232 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'admin', $e->getMessage() ) ); |
||
233 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
234 | $this->logException( $e ); |
||
235 | } |
||
236 | catch( \Aimeos\MShop\Exception $e ) |
||
237 | { |
||
238 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
239 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
240 | $this->logException( $e ); |
||
241 | } |
||
242 | catch( \Exception $e ) |
||
243 | { |
||
244 | $error = array( 'locale-site-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
245 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
246 | $this->logException( $e ); |
||
247 | } |
||
248 | |||
249 | return $this->render( $view ); |
||
250 | } |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Saves the data |
||
255 | * |
||
256 | * @return string HTML output |
||
257 | */ |
||
258 | public function save() |
||
259 | { |
||
260 | $view = $this->getView(); |
||
261 | $context = $this->getContext(); |
||
262 | |||
263 | $manager = \Aimeos\MShop::create( $context, 'locale/site' ); |
||
264 | $manager->begin(); |
||
265 | |||
266 | try |
||
267 | { |
||
268 | $item = $this->fromArray( $view->param( 'item', [] ), $view->access( 'super' ) ); |
||
269 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
270 | $view->itemBody = ''; |
||
271 | |||
272 | foreach( $this->getSubClients() as $client ) { |
||
273 | $view->itemBody .= $client->save(); |
||
274 | } |
||
275 | |||
276 | $manager->saveItem( clone $view->item ); |
||
277 | $manager->commit(); |
||
278 | |||
279 | $this->nextAction( $view, $view->param( 'next' ), 'locale/site', $view->item->getId(), 'save' ); |
||
280 | return; |
||
281 | } |
||
282 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
283 | { |
||
284 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'admin', $e->getMessage() ) ); |
||
285 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
286 | $this->logException( $e ); |
||
287 | } |
||
288 | catch( \Aimeos\MShop\Exception $e ) |
||
289 | { |
||
290 | $error = array( 'locale-site-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
291 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
292 | $this->logException( $e ); |
||
293 | } |
||
294 | catch( \Exception $e ) |
||
295 | { |
||
296 | $error = array( 'locale-site-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
297 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
298 | $this->logException( $e ); |
||
299 | } |
||
300 | |||
301 | $manager->rollback(); |
||
302 | |||
303 | return $this->create(); |
||
304 | } |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Returns a list of resource according to the conditions |
||
309 | * |
||
310 | * @return string HTML output |
||
311 | */ |
||
312 | public function search() |
||
313 | { |
||
314 | $view = $this->getView(); |
||
315 | $context = $this->getContext(); |
||
316 | |||
317 | try |
||
318 | { |
||
319 | $total = 0; |
||
320 | $params = $this->storeSearchParams( $view->param(), 'locale/site' ); |
||
321 | $manager = \Aimeos\MShop::create( $context, 'locale/site' ); |
||
322 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
323 | |||
324 | if( $view->access( 'super' ) === false ) |
||
325 | { |
||
326 | $search->setConditions( $search->combine( '&&', [ |
||
327 | $search->compare( '==', 'locale.site.id', $this->getUserSiteId() ), |
||
328 | $search->getConditions(), |
||
329 | ] ) ); |
||
330 | } |
||
331 | |||
332 | $view->items = $manager->searchItems( $search, [], $total ); |
||
333 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
334 | $view->filterOperators = $search->getOperators(); |
||
335 | $view->total = $total; |
||
336 | $view->itemBody = ''; |
||
337 | |||
338 | foreach( $this->getSubClients() as $client ) { |
||
339 | $view->itemBody .= $client->search(); |
||
340 | } |
||
341 | } |
||
342 | catch( \Aimeos\MShop\Exception $e ) |
||
343 | { |
||
344 | $error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
345 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
346 | $this->logException( $e ); |
||
347 | } |
||
348 | catch( \Exception $e ) |
||
349 | { |
||
350 | $error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
351 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
352 | $this->logException( $e ); |
||
353 | } |
||
354 | |||
355 | /** admin/jqadm/locale/site/template-list |
||
356 | * Relative path to the HTML body template for the locale list. |
||
357 | * |
||
358 | * The template file contains the HTML code and processing instructions |
||
359 | * to generate the result shown in the body of the frontend. The |
||
360 | * configuration string is the path to the template file relative |
||
361 | * to the templates directory (usually in admin/jqadm/templates). |
||
362 | * |
||
363 | * You can overwrite the template file configuration in extensions and |
||
364 | * provide alternative templates. These alternative templates should be |
||
365 | * named like the default one but with the string "default" replaced by |
||
366 | * an unique name. You may use the name of your project for this. If |
||
367 | * you've implemented an alternative client class as well, "default" |
||
368 | * should be replaced by the name of the new class. |
||
369 | * |
||
370 | * @param string Relative path to the template creating the HTML code |
||
371 | * @since 2016.04 |
||
372 | * @category Developer |
||
373 | */ |
||
374 | $tplconf = 'admin/jqadm/locale/site/template-list'; |
||
375 | $default = 'locale/site/list-standard'; |
||
376 | |||
377 | return $view->render( $view->config( $tplconf, $default ) ); |
||
378 | } |
||
379 | |||
380 | |||
381 | /** |
||
382 | * Returns the sub-client given by its name. |
||
383 | * |
||
384 | * @param string $type Name of the client type |
||
385 | * @param string|null $name Name of the sub-client (Default if null) |
||
386 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
387 | */ |
||
388 | public function getSubClient( $type, $name = null ) |
||
389 | { |
||
390 | /** admin/jqadm/locale/site/decorators/excludes |
||
391 | * Excludes decorators added by the "common" option from the locale JQAdm client |
||
392 | * |
||
393 | * Decorators extend the functionality of a class by adding new aspects |
||
394 | * (e.g. log what is currently done), executing the methods of the underlying |
||
395 | * class only in certain conditions (e.g. only for logged in users) or |
||
396 | * modify what is returned to the caller. |
||
397 | * |
||
398 | * This option allows you to remove a decorator added via |
||
399 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
400 | * around the JQAdm client. |
||
401 | * |
||
402 | * admin/jqadm/locale/site/decorators/excludes = array( 'decorator1' ) |
||
403 | * |
||
404 | * This would remove the decorator named "decorator1" from the list of |
||
405 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
406 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
407 | * |
||
408 | * @param array List of decorator names |
||
409 | * @since 2017.10 |
||
410 | * @category Developer |
||
411 | * @see admin/jqadm/common/decorators/default |
||
412 | * @see admin/jqadm/locale/site/decorators/global |
||
413 | * @see admin/jqadm/locale/site/decorators/local |
||
414 | */ |
||
415 | |||
416 | /** admin/jqadm/locale/site/decorators/global |
||
417 | * Adds a list of globally available decorators only to the locale JQAdm client |
||
418 | * |
||
419 | * Decorators extend the functionality of a class by adding new aspects |
||
420 | * (e.g. log what is currently done), executing the methods of the underlying |
||
421 | * class only in certain conditions (e.g. only for logged in users) or |
||
422 | * modify what is returned to the caller. |
||
423 | * |
||
424 | * This option allows you to wrap global decorators |
||
425 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
426 | * |
||
427 | * admin/jqadm/locale/site/decorators/global = array( 'decorator1' ) |
||
428 | * |
||
429 | * This would add the decorator named "decorator1" defined by |
||
430 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
431 | * |
||
432 | * @param array List of decorator names |
||
433 | * @since 2017.10 |
||
434 | * @category Developer |
||
435 | * @see admin/jqadm/common/decorators/default |
||
436 | * @see admin/jqadm/locale/site/decorators/excludes |
||
437 | * @see admin/jqadm/locale/site/decorators/local |
||
438 | */ |
||
439 | |||
440 | /** admin/jqadm/locale/site/decorators/local |
||
441 | * Adds a list of local decorators only to the locale JQAdm client |
||
442 | * |
||
443 | * Decorators extend the functionality of a class by adding new aspects |
||
444 | * (e.g. log what is currently done), executing the methods of the underlying |
||
445 | * class only in certain conditions (e.g. only for logged in users) or |
||
446 | * modify what is returned to the caller. |
||
447 | * |
||
448 | * This option allows you to wrap local decorators |
||
449 | * ("\Aimeos\Admin\JQAdm\Locale\Site\Decorator\*") around the JQAdm client. |
||
450 | * |
||
451 | * admin/jqadm/locale/site/decorators/local = array( 'decorator2' ) |
||
452 | * |
||
453 | * This would add the decorator named "decorator2" defined by |
||
454 | * "\Aimeos\Admin\JQAdm\Locale\Site\Decorator\Decorator2" only to the JQAdm client. |
||
455 | * |
||
456 | * @param array List of decorator names |
||
457 | * @since 2017.10 |
||
458 | * @category Developer |
||
459 | * @see admin/jqadm/common/decorators/default |
||
460 | * @see admin/jqadm/locale/site/decorators/excludes |
||
461 | * @see admin/jqadm/locale/site/decorators/global |
||
462 | */ |
||
463 | return $this->createSubClient( 'locale/site' . $type, $name ); |
||
464 | } |
||
465 | |||
466 | |||
467 | /** |
||
468 | * Checks if the user is allowed to access the site item |
||
469 | * |
||
470 | * @param boolean $super True if user is a super user |
||
471 | * @param string $id ID of the site to access |
||
472 | * @throws \Aimeos\Admin\JQAdm\Exception If user isn't allowed to access the site |
||
473 | */ |
||
474 | protected function checkSite( $super, $id = null ) |
||
481 | } |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Returns the site ID of the current user |
||
486 | * |
||
487 | * @return string|null Site ID of the current user |
||
488 | */ |
||
489 | protected function getUserSiteId() |
||
495 | } |
||
496 | |||
497 | |||
498 | /** |
||
499 | * Returns the list of sub-client names configured for the client. |
||
500 | * |
||
501 | * @return array List of JQAdm client names |
||
502 | */ |
||
503 | protected function getSubClientNames() |
||
539 | } |
||
540 | |||
541 | |||
542 | /** |
||
543 | * Creates new and updates existing items using the data array |
||
544 | * |
||
545 | * @param array $data Data array |
||
546 | * @param boolean $super If current user is a super user |
||
547 | * @return \Aimeos\MShop\Locale\Item\Iface New locale item object |
||
548 | */ |
||
549 | protected function fromArray( array $data, $super ) |
||
581 | } |
||
582 | |||
583 | |||
584 | /** |
||
585 | * Constructs the data array for the view from the given item |
||
586 | * |
||
587 | * @param \Aimeos\MShop\Locale\Item\Iface $item Locale item object |
||
588 | * @return string[] Multi-dimensional associative list of item data |
||
589 | */ |
||
590 | protected function toArray( \Aimeos\MShop\Locale\Item\Site\Iface $item, $copy = false ) |
||
606 | } |
||
607 | |||
608 | |||
609 | /** |
||
610 | * Returns the rendered template including the view data |
||
611 | * |
||
612 | * @return string HTML output |
||
613 | */ |
||
614 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
641 |
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.