Total Complexity | 46 |
Total Lines | 528 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
23 | class Standard |
||
24 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
25 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
26 | { |
||
27 | /** |
||
28 | * Adds the required data used in the template |
||
29 | * |
||
30 | * @param \Aimeos\MW\View\Iface $view View object |
||
31 | * @return \Aimeos\MW\View\Iface View object with assigned parameters |
||
32 | */ |
||
33 | public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
||
34 | { |
||
35 | $view->itemSubparts = $this->getSubClientNames(); |
||
36 | return $view; |
||
37 | } |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Copies a resource |
||
42 | * |
||
43 | * @return string|null HTML output |
||
44 | */ |
||
45 | public function copy() : ?string |
||
46 | { |
||
47 | $view = $this->getObject()->addData( $this->getView() ); |
||
48 | |||
49 | try |
||
50 | { |
||
51 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
52 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
53 | } |
||
54 | |||
55 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
56 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
57 | |||
58 | $view->itemGroups = $this->getGroupItems( $view->item ); |
||
59 | $view->itemData = $this->toArray( $view->item, true ); |
||
60 | $view->itemBody = parent::copy(); |
||
61 | } |
||
62 | catch( \Exception $e ) |
||
63 | { |
||
64 | $this->report( $e, 'copy' ); |
||
65 | } |
||
66 | |||
67 | return $this->render( $view ); |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Creates a new resource |
||
73 | * |
||
74 | * @return string|null HTML output |
||
75 | */ |
||
76 | public function create() : ?string |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Deletes a resource |
||
105 | * |
||
106 | * @return string|null HTML output |
||
107 | */ |
||
108 | public function delete() : ?string |
||
109 | { |
||
110 | $view = $this->getView(); |
||
111 | |||
112 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
113 | $manager->begin(); |
||
114 | |||
115 | try |
||
116 | { |
||
117 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
118 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
119 | } |
||
120 | |||
121 | $search = $manager->filter()->slice( 0, count( (array) $ids ) ); |
||
122 | $search->setConditions( $search->compare( '==', 'customer.id', $ids ) ); |
||
123 | $items = $manager->search( $search, $this->getDomains() ); |
||
124 | |||
125 | foreach( $items as $item ) |
||
126 | { |
||
127 | $view->item = $item; |
||
128 | parent::delete(); |
||
129 | } |
||
130 | |||
131 | $manager->delete( $items->toArray() ); |
||
132 | $manager->commit(); |
||
133 | |||
134 | return $this->redirect( 'customer', 'search', null, 'delete' ); |
||
135 | } |
||
136 | catch( \Exception $e ) |
||
137 | { |
||
138 | $manager->rollback(); |
||
139 | $this->report( $e, 'delete' ); |
||
140 | } |
||
141 | |||
142 | return $this->search(); |
||
143 | } |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Returns a single resource |
||
148 | * |
||
149 | * @return string|null HTML output |
||
150 | */ |
||
151 | public function get() : ?string |
||
152 | { |
||
153 | $view = $this->getObject()->addData( $this->getView() ); |
||
154 | |||
155 | try |
||
156 | { |
||
157 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
158 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
159 | } |
||
160 | |||
161 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
162 | |||
163 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
164 | $view->itemGroups = $this->getGroupItems( $view->item ); |
||
165 | $view->itemData = $this->toArray( $view->item ); |
||
166 | $view->itemBody = parent::get(); |
||
167 | } |
||
168 | catch( \Exception $e ) |
||
169 | { |
||
170 | $this->report( $e, 'get' ); |
||
171 | } |
||
172 | |||
173 | return $this->render( $view ); |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Saves the data |
||
179 | * |
||
180 | * @return string|null HTML output |
||
181 | */ |
||
182 | public function save() : ?string |
||
183 | { |
||
184 | $view = $this->getView(); |
||
185 | |||
186 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
187 | $manager->begin(); |
||
188 | |||
189 | try |
||
190 | { |
||
191 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
192 | $view->item = $item->getId() ? $item : $manager->save( $item ); |
||
193 | $view->itemBody = parent::save(); |
||
194 | |||
195 | $manager->save( clone $view->item ); |
||
196 | $manager->commit(); |
||
197 | |||
198 | return $this->redirect( 'customer', $view->param( 'next' ), $view->item->getId(), 'save' ); |
||
|
|||
199 | } |
||
200 | catch( \Exception $e ) |
||
201 | { |
||
202 | $manager->rollback(); |
||
203 | $this->report( $e, 'save' ); |
||
204 | } |
||
205 | |||
206 | return $this->create(); |
||
207 | } |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Returns a list of resource according to the conditions |
||
212 | * |
||
213 | * @return string|null HTML output |
||
214 | */ |
||
215 | public function search() : ?string |
||
216 | { |
||
217 | $view = $this->getView(); |
||
218 | |||
219 | try |
||
220 | { |
||
221 | $total = 0; |
||
222 | $params = $this->storeFilter( $view->param(), 'customer' ); |
||
223 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
224 | $search = $this->initCriteria( $manager->filter(), $params ); |
||
225 | |||
226 | $view->items = $manager->search( $search, $this->getDomains(), $total ); |
||
227 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
228 | $view->filterOperators = $search->getOperators(); |
||
229 | $view->itemBody = parent::search(); |
||
230 | $view->total = $total; |
||
231 | } |
||
232 | catch( \Exception $e ) |
||
233 | { |
||
234 | $this->report( $e, 'search' ); |
||
235 | } |
||
236 | |||
237 | /** admin/jqadm/customer/template-list |
||
238 | * Relative path to the HTML body template for the customer list. |
||
239 | * |
||
240 | * The template file contains the HTML code and processing instructions |
||
241 | * to generate the result shown in the body of the frontend. The |
||
242 | * configuration string is the path to the template file relative |
||
243 | * to the templates directory (usually in admin/jqadm/templates). |
||
244 | * |
||
245 | * You can overwrite the template file configuration in extensions and |
||
246 | * provide alternative templates. These alternative templates should be |
||
247 | * named like the default one but with the string "default" replaced by |
||
248 | * an unique name. You may use the name of your project for this. If |
||
249 | * you've implemented an alternative client class as well, "default" |
||
250 | * should be replaced by the name of the new class. |
||
251 | * |
||
252 | * @param string Relative path to the template creating the HTML code |
||
253 | * @since 2016.04 |
||
254 | * @category Developer |
||
255 | */ |
||
256 | $tplconf = 'admin/jqadm/customer/template-list'; |
||
257 | $default = 'customer/list-standard'; |
||
258 | |||
259 | return $view->render( $view->config( $tplconf, $default ) ); |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Returns the sub-client given by its name. |
||
265 | * |
||
266 | * @param string $type Name of the client type |
||
267 | * @param string|null $name Name of the sub-client (Default if null) |
||
268 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
269 | */ |
||
270 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
271 | { |
||
272 | /** admin/jqadm/customer/decorators/excludes |
||
273 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
274 | * |
||
275 | * Decorators extend the functionality of a class by adding new aspects |
||
276 | * (e.g. log what is currently done), executing the methods of the underlying |
||
277 | * class only in certain conditions (e.g. only for logged in users) or |
||
278 | * modify what is returned to the caller. |
||
279 | * |
||
280 | * This option allows you to remove a decorator added via |
||
281 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
282 | * around the JQAdm client. |
||
283 | * |
||
284 | * admin/jqadm/customer/decorators/excludes = array( 'decorator1' ) |
||
285 | * |
||
286 | * This would remove the decorator named "decorator1" from the list of |
||
287 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
288 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
289 | * |
||
290 | * @param array List of decorator names |
||
291 | * @since 2017.07 |
||
292 | * @category Developer |
||
293 | * @see admin/jqadm/common/decorators/default |
||
294 | * @see admin/jqadm/customer/decorators/global |
||
295 | * @see admin/jqadm/customer/decorators/local |
||
296 | */ |
||
297 | |||
298 | /** admin/jqadm/customer/decorators/global |
||
299 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
300 | * |
||
301 | * Decorators extend the functionality of a class by adding new aspects |
||
302 | * (e.g. log what is currently done), executing the methods of the underlying |
||
303 | * class only in certain conditions (e.g. only for logged in users) or |
||
304 | * modify what is returned to the caller. |
||
305 | * |
||
306 | * This option allows you to wrap global decorators |
||
307 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
308 | * |
||
309 | * admin/jqadm/customer/decorators/global = array( 'decorator1' ) |
||
310 | * |
||
311 | * This would add the decorator named "decorator1" defined by |
||
312 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
313 | * |
||
314 | * @param array List of decorator names |
||
315 | * @since 2017.07 |
||
316 | * @category Developer |
||
317 | * @see admin/jqadm/common/decorators/default |
||
318 | * @see admin/jqadm/customer/decorators/excludes |
||
319 | * @see admin/jqadm/customer/decorators/local |
||
320 | */ |
||
321 | |||
322 | /** admin/jqadm/customer/decorators/local |
||
323 | * Adds a list of local decorators only to the customer JQAdm client |
||
324 | * |
||
325 | * Decorators extend the functionality of a class by adding new aspects |
||
326 | * (e.g. log what is currently done), executing the methods of the underlying |
||
327 | * class only in certain conditions (e.g. only for logged in users) or |
||
328 | * modify what is returned to the caller. |
||
329 | * |
||
330 | * This option allows you to wrap local decorators |
||
331 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
332 | * |
||
333 | * admin/jqadm/customer/decorators/local = array( 'decorator2' ) |
||
334 | * |
||
335 | * This would add the decorator named "decorator2" defined by |
||
336 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
337 | * |
||
338 | * @param array List of decorator names |
||
339 | * @since 2017.07 |
||
340 | * @category Developer |
||
341 | * @see admin/jqadm/common/decorators/default |
||
342 | * @see admin/jqadm/customer/decorators/excludes |
||
343 | * @see admin/jqadm/customer/decorators/global |
||
344 | */ |
||
345 | return $this->createSubClient( 'customer/' . $type, $name ); |
||
346 | } |
||
347 | |||
348 | |||
349 | /** |
||
350 | * Returns the domain names whose items should be fetched too |
||
351 | * |
||
352 | * @return string[] List of domain names |
||
353 | */ |
||
354 | protected function getDomains() : array |
||
368 | } |
||
369 | |||
370 | |||
371 | /** |
||
372 | * Returns the available group items |
||
373 | * |
||
374 | * @param \Aimeos\MShop\Customer\Item\Iface|null $item Customer item that should be updated |
||
375 | * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values |
||
376 | */ |
||
377 | protected function getGroupItems( \Aimeos\MShop\Customer\Item\Iface $item = null ) : array |
||
378 | { |
||
379 | $list = []; |
||
380 | $context = $this->getContext(); |
||
381 | |||
382 | $isSuper = $this->getView()->access( ['super'] ); |
||
383 | $isAdmin = $this->getView()->access( ['admin'] ); |
||
384 | $isEditor = $this->getView()->access( ['editor'] ); |
||
385 | |||
386 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||
387 | $search = $manager->filter( true )->slice( 0, 10000 ); |
||
388 | $search->setSortations( [$search->sort( '+', 'customer.group.label' )] ); |
||
389 | |||
390 | foreach( $manager->search( $search ) as $groupId => $groupItem ) |
||
391 | { |
||
392 | if( !$isSuper && $groupItem->getCode() === 'super' ) { |
||
393 | continue; |
||
394 | } |
||
395 | |||
396 | if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'admin' ) { |
||
397 | continue; |
||
398 | } |
||
399 | |||
400 | if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'editor' |
||
401 | && ( !$isEditor || $item === null || (string) $context->getUserId() !== (string) $item->getId() ) |
||
402 | ) { |
||
403 | continue; |
||
404 | } |
||
405 | |||
406 | $list[$groupId] = $groupItem; |
||
407 | } |
||
408 | |||
409 | return $list; |
||
410 | } |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Returns the list of sub-client names configured for the client. |
||
415 | * |
||
416 | * @return array List of JQAdm client names |
||
417 | */ |
||
418 | protected function getSubClientNames() : array |
||
454 | } |
||
455 | |||
456 | |||
457 | |||
458 | /** |
||
459 | * Creates new and updates existing items using the data array |
||
460 | * |
||
461 | * @param array $data Data array |
||
462 | * @return \Aimeos\MShop\Customer\Item\Iface New customer item object |
||
463 | */ |
||
464 | protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface |
||
465 | { |
||
466 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
467 | |||
468 | if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) { |
||
469 | $item = $manager->get( $data['customer.id'], $this->getDomains() ); |
||
470 | } else { |
||
471 | $item = $manager->create(); |
||
472 | } |
||
473 | |||
474 | $addr = $item->getPaymentAddress(); |
||
475 | $label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname(); |
||
476 | $label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' ); |
||
477 | $groupIds = $this->getValue( $data, 'customer.groups', [] ); |
||
478 | |||
479 | $pass = $data['customer.password'] ?? null; |
||
480 | unset( $data['customer.password'] ); |
||
481 | |||
482 | $item->fromArray( $data, true ) |
||
483 | ->setGroups( array_intersect( array_keys( $this->getGroupItems( $item ) ), $groupIds ) ) |
||
484 | ->setCode( $item->getCode() ?: $addr->getEmail() ) |
||
485 | ->setLabel( $label ); |
||
486 | |||
487 | if( $this->getView()->access( ['super'] ) ) { |
||
488 | $item->setPassword( $pass ); |
||
489 | } |
||
490 | |||
491 | return $item; |
||
492 | } |
||
493 | |||
494 | |||
495 | /** |
||
496 | * Constructs the data array for the view from the given item |
||
497 | * |
||
498 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
499 | * @return string[] Multi-dimensional associative list of item data |
||
500 | */ |
||
501 | protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array |
||
517 | } |
||
518 | |||
519 | |||
520 | /** |
||
521 | * Returns the rendered template including the view data |
||
522 | * |
||
523 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
524 | * @return string HTML output |
||
525 | */ |
||
526 | protected function render( \Aimeos\MW\View\Iface $view ) : string |
||
551 | } |
||
552 | } |
||
553 |