Total Complexity | 88 |
Total Lines | 719 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class Base |
||
23 | implements \Aimeos\Admin\JQAdm\Iface |
||
24 | { |
||
25 | private $view; |
||
26 | private $aimeos; |
||
27 | private $context; |
||
28 | private $subclients; |
||
29 | private $object; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Initializes the class instance. |
||
34 | * |
||
35 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
36 | */ |
||
37 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
38 | { |
||
39 | $this->context = $context; |
||
40 | } |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Catch unknown methods |
||
45 | * |
||
46 | * @param string $name Name of the method |
||
47 | * @param array $param List of method parameter |
||
48 | * @throws \Aimeos\Admin\JQAdm\Exception If method call failed |
||
49 | */ |
||
50 | public function __call( string $name, array $param ) |
||
51 | { |
||
52 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Unable to call method "%1$s"', $name ) ); |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Adds the required data used in the attribute template |
||
58 | * |
||
59 | * @param \Aimeos\MW\View\Iface $view View object |
||
60 | * @return \Aimeos\MW\View\Iface View object with assigned parameters |
||
61 | */ |
||
62 | public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
||
63 | { |
||
64 | return $view; |
||
65 | } |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Returns the Aimeos bootstrap object |
||
70 | * |
||
71 | * @return \Aimeos\Bootstrap The Aimeos bootstrap object |
||
72 | */ |
||
73 | public function getAimeos() : \Aimeos\Bootstrap |
||
74 | { |
||
75 | if( !isset( $this->aimeos ) ) { |
||
76 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Aimeos object not available' ) ); |
||
77 | } |
||
78 | |||
79 | return $this->aimeos; |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Sets the Aimeos bootstrap object |
||
85 | * |
||
86 | * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object |
||
87 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
88 | */ |
||
89 | public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JQAdm\Iface |
||
90 | { |
||
91 | $this->aimeos = $aimeos; |
||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Makes the outer decorator object available to inner objects |
||
98 | * |
||
99 | * @param \Aimeos\Admin\JQAdm\Iface $object Outmost object |
||
100 | * @return \Aimeos\Admin\JQAdm\Iface Same object for fluent interface |
||
101 | */ |
||
102 | public function setObject( \Aimeos\Admin\JQAdm\Iface $object ) : \Aimeos\Admin\JQAdm\Iface |
||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Returns the view object that will generate the admin output. |
||
111 | * |
||
112 | * @return \Aimeos\MW\View\Iface The view object which generates the admin output |
||
113 | */ |
||
114 | public function getView() : \Aimeos\MW\View\Iface |
||
115 | { |
||
116 | if( !isset( $this->view ) ) { |
||
117 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'No view available' ) ); |
||
118 | } |
||
119 | |||
120 | return $this->view; |
||
121 | } |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Sets the view object that will generate the admin output. |
||
126 | * |
||
127 | * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
||
128 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
129 | */ |
||
130 | public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface |
||
134 | } |
||
135 | |||
136 | |||
137 | /** |
||
138 | * Copies a resource |
||
139 | * |
||
140 | * @return string|null Output to display |
||
141 | */ |
||
142 | public function copy() : ?string |
||
143 | { |
||
144 | $body = null; |
||
145 | $view = $this->getView(); |
||
146 | |||
147 | foreach( $this->getSubClients() as $idx => $client ) |
||
148 | { |
||
149 | $view->tabindex = ++$idx + 1; |
||
150 | $body .= $client->copy(); |
||
151 | } |
||
152 | |||
153 | return $body; |
||
154 | } |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Creates a new resource |
||
159 | * |
||
160 | * @return string|null Output to display |
||
161 | */ |
||
162 | public function create() : ?string |
||
163 | { |
||
164 | $body = null; |
||
165 | $view = $this->getView(); |
||
166 | |||
167 | foreach( $this->getSubClients() as $idx => $client ) |
||
168 | { |
||
169 | $view->tabindex = ++$idx + 1; |
||
170 | $body .= $client->create(); |
||
171 | } |
||
172 | |||
173 | return $body; |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Deletes a resource |
||
179 | * |
||
180 | * @return string|null Output to display |
||
181 | */ |
||
182 | public function delete() : ?string |
||
183 | { |
||
184 | $body = null; |
||
185 | |||
186 | foreach( $this->getSubClients() as $client ) { |
||
187 | $body .= $client->delete(); |
||
188 | } |
||
189 | |||
190 | return $body; |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Exports a resource |
||
196 | * |
||
197 | * @return string|null Output to display |
||
198 | */ |
||
199 | public function export() : ?string |
||
200 | { |
||
201 | $body = null; |
||
202 | |||
203 | foreach( $this->getSubClients() as $client ) { |
||
204 | $body .= $client->export(); |
||
205 | } |
||
206 | |||
207 | return $body; |
||
208 | } |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Returns a resource |
||
213 | * |
||
214 | * @return string|null Output to display |
||
215 | */ |
||
216 | public function get() : ?string |
||
217 | { |
||
218 | $body = null; |
||
219 | $view = $this->getView(); |
||
220 | |||
221 | foreach( $this->getSubClients() as $idx => $client ) |
||
222 | { |
||
223 | $view->tabindex = ++$idx + 1; |
||
224 | $body .= $client->get(); |
||
225 | } |
||
226 | |||
227 | return $body; |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Imports a resource |
||
233 | * |
||
234 | * @return string|null Output to display |
||
235 | * @deprecated 2021.01 |
||
236 | */ |
||
237 | public function import() : ?string |
||
238 | { |
||
239 | $body = null; |
||
240 | |||
241 | foreach( $this->getSubClients() as $client ) { |
||
242 | $body .= $client->import(); |
||
243 | } |
||
244 | |||
245 | return null; |
||
246 | } |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Saves the data |
||
251 | * |
||
252 | * @return string|null Output to display |
||
253 | */ |
||
254 | public function save() : ?string |
||
255 | { |
||
256 | $body = null; |
||
257 | |||
258 | foreach( $this->getSubClients() as $client ) { |
||
259 | $body .= $client->save(); |
||
260 | } |
||
261 | |||
262 | return $body; |
||
263 | } |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Returns a list of resource according to the conditions |
||
268 | * |
||
269 | * @return string|null Output to display |
||
270 | */ |
||
271 | public function search() : ?string |
||
272 | { |
||
273 | $body = null; |
||
274 | |||
275 | foreach( $this->getSubClients() as $client ) { |
||
276 | $body .= $client->search(); |
||
277 | } |
||
278 | |||
279 | return $body; |
||
280 | } |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Adds the decorators to the client object |
||
285 | * |
||
286 | * @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
||
287 | * @param array $decorators List of decorator name that should be wrapped around the client |
||
288 | * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\" |
||
289 | * @return \Aimeos\Admin\JQAdm\Iface Admin object |
||
290 | */ |
||
291 | protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface |
||
292 | { |
||
293 | foreach( $decorators as $name ) |
||
294 | { |
||
295 | if( ctype_alnum( $name ) === false ) |
||
296 | { |
||
297 | $classname = is_string( $name ) ? $classprefix . $name : '<not a string>'; |
||
298 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) ); |
||
299 | } |
||
300 | |||
301 | $classname = $classprefix . $name; |
||
302 | |||
303 | if( class_exists( $classname ) === false ) { |
||
304 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ) ); |
||
305 | } |
||
306 | |||
307 | $client = new $classname( $client, $this->context ); |
||
308 | |||
309 | \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client ); |
||
310 | } |
||
311 | |||
312 | return $client; |
||
313 | } |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Adds the decorators to the client object |
||
318 | * |
||
319 | * @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
||
320 | * @param string $path Admin string in lower case, e.g. "catalog/detail/basic" |
||
321 | * @return \Aimeos\Admin\JQAdm\Iface Admin object |
||
322 | */ |
||
323 | protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface |
||
324 | { |
||
325 | if( !is_string( $path ) || $path === '' ) { |
||
|
|||
326 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid domain "%1$s"', $path ) ); |
||
327 | } |
||
328 | |||
329 | $localClass = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
||
330 | $config = $this->context->getConfig(); |
||
331 | |||
332 | $classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\'; |
||
333 | $decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] ); |
||
334 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
335 | |||
336 | $classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\'; |
||
337 | $decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] ); |
||
338 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
339 | |||
340 | return $client; |
||
341 | } |
||
342 | |||
343 | |||
344 | /** |
||
345 | * Returns the sub-client given by its name. |
||
346 | * |
||
347 | * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
||
348 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
349 | * @return \Aimeos\Admin\JQAdm\Iface Sub-part object |
||
350 | */ |
||
351 | protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
376 | } |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Returns the value for the given key in the array |
||
381 | * |
||
382 | * @param array $values Multi-dimensional associative list of key/value pairs |
||
383 | * @param string $key Parameter key like "name" or "list/test" for associative arrays |
||
384 | * @param mixed $default Returned value if no one for key is available |
||
385 | * @return mixed Value from the array or default value if not present in array |
||
386 | */ |
||
387 | protected function getValue( array $values, $key, $default = null ) |
||
388 | { |
||
389 | foreach( explode( '/', trim( $key, '/' ) ) as $part ) |
||
390 | { |
||
391 | if( array_key_exists( $part, $values ) ) { |
||
392 | $values = $values[$part]; |
||
393 | } else { |
||
394 | return $default; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | return $values; |
||
399 | } |
||
400 | |||
401 | |||
402 | /** |
||
403 | * Returns the known client parameters and their values |
||
404 | * |
||
405 | * @param array $names List of parameter names |
||
406 | * @return array Associative list of parameters names as key and their values |
||
407 | */ |
||
408 | protected function getClientParams( $names = ['id', 'resource', 'site', 'lang'] ) : array |
||
409 | { |
||
410 | $list = []; |
||
411 | |||
412 | foreach( $names as $name ) |
||
413 | { |
||
414 | if( ( $val = $this->view->param( $name ) ) !== null ) { |
||
415 | $list[$name] = $val; |
||
416 | } |
||
417 | } |
||
418 | |||
419 | return $list; |
||
420 | } |
||
421 | |||
422 | |||
423 | /** |
||
424 | * Returns the context object. |
||
425 | * |
||
426 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
427 | */ |
||
428 | protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
||
431 | } |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Returns the list of sub-client names configured for the client. |
||
436 | * |
||
437 | * @return array List of admin client names |
||
438 | */ |
||
439 | abstract protected function getSubClientNames() : array; |
||
440 | |||
441 | |||
442 | /** |
||
443 | * Returns the available class names without namespace that are stored in the given path |
||
444 | * |
||
445 | * @param string $relpath Path relative to the include paths |
||
446 | * @param string[] $excludes List of file names to execlude |
||
447 | * @return string[] List of available class names |
||
448 | */ |
||
449 | protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array |
||
470 | } |
||
471 | |||
472 | |||
473 | /** |
||
474 | * Returns the array of criteria conditions based on the given parameters |
||
475 | * |
||
476 | * @param array $params List of criteria data with condition, sorting and paging |
||
477 | * @return array Multi-dimensional associative list of criteria conditions |
||
478 | */ |
||
479 | protected function getCriteriaConditions( array $params ) : array |
||
500 | } |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Returns the outer decoratorator of the object |
||
505 | * |
||
506 | * @return \Aimeos\Admin\JQAdm\Iface Outmost object |
||
507 | */ |
||
508 | protected function getObject() : Iface |
||
509 | { |
||
510 | if( isset( $this->object ) ) { |
||
511 | return $this->object; |
||
512 | } |
||
513 | |||
514 | return $this; |
||
515 | } |
||
516 | |||
517 | |||
518 | /** |
||
519 | * Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
||
520 | * |
||
521 | * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names |
||
522 | */ |
||
523 | protected function getSubClients() : array |
||
524 | { |
||
525 | if( !isset( $this->subclients ) ) |
||
526 | { |
||
527 | $this->subclients = []; |
||
528 | |||
529 | foreach( $this->getSubClientNames() as $name ) { |
||
530 | $this->subclients[] = $this->getSubClient( $name ); |
||
531 | } |
||
532 | } |
||
533 | |||
534 | return $this->subclients; |
||
535 | } |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Initializes the criteria object based on the given parameter |
||
540 | * |
||
541 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
542 | * @param array $params List of criteria data with condition, sorting and paging |
||
543 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
544 | */ |
||
545 | protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
||
546 | { |
||
547 | return $criteria->order( $params['sort'] ?? [] ) |
||
548 | ->slice( $params['page']['offset'] ?? 0, $params['page']['limit'] ?? 25 ) |
||
549 | ->add( $criteria->parse( $this->getCriteriaConditions( $params['filter'] ?? [] ) ) ); |
||
550 | } |
||
551 | |||
552 | |||
553 | /** |
||
554 | * Writes the exception details to the log |
||
555 | * |
||
556 | * @param \Exception $e Exception object |
||
557 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
558 | */ |
||
559 | protected function log( \Exception $e ) : Iface |
||
560 | { |
||
561 | $logger = $this->context->getLogger(); |
||
562 | $logger->log( $e->getMessage(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' ); |
||
563 | $logger->log( $e->getTraceAsString(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' ); |
||
564 | |||
565 | return $this; |
||
566 | } |
||
567 | |||
568 | |||
569 | /** |
||
570 | * Returns a map of code/item pairs |
||
571 | * |
||
572 | * @param \Aimeos\MShop\Common\Item\Type\Iface[] $items Associative list of type items |
||
573 | * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of codes as keys and items as values |
||
574 | * @deprecated 2021.01 |
||
575 | */ |
||
576 | protected function map( \Aimeos\Map $items ) : array |
||
585 | } |
||
586 | |||
587 | |||
588 | /** |
||
589 | * Adds a redirect to the response for the next action |
||
590 | * |
||
591 | * @param string $resource Resource name |
||
592 | * @param string|null $action Next action |
||
593 | * @param string|null $id ID of the next resource item |
||
594 | * @param string|null $act Current action name |
||
595 | * @return string|null Returns value for the actions |
||
596 | */ |
||
597 | protected function redirect( string $resource, ?string $action, string $id = null, |
||
598 | string $method = null ) : ?string |
||
599 | { |
||
600 | $params = $this->getClientParams(); |
||
601 | $context = $this->getContext(); |
||
602 | $view = $this->getView(); |
||
603 | |||
604 | $params['resource'] = $resource; |
||
605 | unset( $params['id'] ); |
||
606 | |||
607 | switch( $action ) |
||
608 | { |
||
609 | case 'search': |
||
610 | $target = $view->config( 'admin/jqadm/url/search/target' ); |
||
611 | $cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' ); |
||
612 | $action = $view->config( 'admin/jqadm/url/search/action', 'search' ); |
||
613 | $conf = $view->config( 'admin/jqadm/url/search/config', [] ); |
||
614 | $url = $view->url( $target, $cntl, $action, $params, [], $conf ); |
||
615 | break; |
||
616 | case 'create': |
||
617 | $params['parentid'] = $id; |
||
618 | $target = $view->config( 'admin/jqadm/url/create/target' ); |
||
619 | $cntl = $view->config( 'admin/jqadm/url/create/controller', 'Jqadm' ); |
||
620 | $action = $view->config( 'admin/jqadm/url/create/action', 'create' ); |
||
621 | $conf = $view->config( 'admin/jqadm/url/create/config', [] ); |
||
622 | $url = $view->url( $target, $cntl, $action, $params, [], $conf ); |
||
623 | break; |
||
624 | case 'copy': |
||
625 | $target = $view->config( 'admin/jqadm/url/copy/target' ); |
||
626 | $cntl = $view->config( 'admin/jqadm/url/copy/controller', 'Jqadm' ); |
||
627 | $action = $view->config( 'admin/jqadm/url/copy/action', 'copy' ); |
||
628 | $conf = $view->config( 'admin/jqadm/url/copy/config', [] ); |
||
629 | $url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf ); |
||
630 | break; |
||
631 | default: |
||
632 | $target = $view->config( 'admin/jqadm/url/get/target' ); |
||
633 | $cntl = $view->config( 'admin/jqadm/url/get/controller', 'Jqadm' ); |
||
634 | $action = $view->config( 'admin/jqadm/url/get/action', 'get' ); |
||
635 | $conf = $view->config( 'admin/jqadm/url/get/config', [] ); |
||
636 | $url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf ); |
||
637 | } |
||
638 | |||
639 | switch( $method ) |
||
640 | { |
||
641 | case 'save': |
||
642 | $context->getSession()->set( 'info', [$context->getI18n()->dt( 'admin', 'Item saved successfully' )] ); break; |
||
643 | case 'delete': |
||
644 | $context->getSession()->set( 'info', [$context->getI18n()->dt( 'admin', 'Item deleted successfully' )] ); break; |
||
645 | } |
||
646 | |||
647 | $view->response()->withStatus( 302 ); |
||
648 | $view->response()->withHeader( 'Location', $url ); |
||
649 | $view->response()->withHeader( 'Cache-Control', 'no-store' ); |
||
650 | |||
651 | return null; |
||
652 | } |
||
653 | |||
654 | |||
655 | /** |
||
656 | * Writes the exception details to the log |
||
657 | * |
||
658 | * @param \Exception $e Exception object |
||
659 | * @param string $method Method it's called from |
||
660 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
661 | */ |
||
662 | protected function report( \Exception $e, string $method ) : Iface |
||
688 | } |
||
689 | |||
690 | |||
691 | /** |
||
692 | * Checks and returns the request parameter for the given name |
||
693 | * |
||
694 | * @param string $name Name of the request parameter, can be a path like 'page/limit' |
||
695 | * @return mixed Parameter value |
||
696 | * @throws \Aimeos\Admin\JQAdm\Exception If the parameter is missing |
||
697 | */ |
||
698 | protected function require( string $name ) |
||
705 | } |
||
706 | |||
707 | |||
708 | /** |
||
709 | * Stores and returns the parameters used for searching items |
||
710 | * |
||
711 | * @param array $params GET/POST parameter set |
||
712 | * @param string $name Name of the panel/subpanel |
||
713 | * @return array Associative list of parameters for searching items |
||
714 | */ |
||
715 | protected function storeFilter( array $params, string $name ) : array |
||
744 |