Total Complexity | 63 |
Total Lines | 580 |
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 |
||
20 | class Standard |
||
21 | extends \Aimeos\Controller\Frontend\Base |
||
22 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
23 | { |
||
24 | private $domains = []; |
||
25 | private $filter; |
||
26 | private $manager; |
||
27 | private $rules; |
||
|
|||
28 | |||
29 | |||
30 | /** |
||
31 | * Common initialization for controller classes |
||
32 | * |
||
33 | * @param \Aimeos\MShop\ContextIface $context Common MShop context object |
||
34 | */ |
||
35 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
36 | { |
||
37 | parent::__construct( $context ); |
||
38 | |||
39 | $this->manager = \Aimeos\MShop::create( $context, 'index' ); |
||
40 | $this->filter = $this->manager->filter( true ); |
||
41 | |||
42 | /** controller/frontend/product/show-all |
||
43 | * Require products to be assigned to categories |
||
44 | * |
||
45 | * By default, products that are shown in the frontend must be assigned to |
||
46 | * at least one category. When changing this setting to TRUE, also products |
||
47 | * without categories will be shown in the frontend. |
||
48 | * |
||
49 | * Caution: If you have discount products or variant articles in selection |
||
50 | * products, these products/articles will be displayed in the frontend too |
||
51 | * when disabling this setting! |
||
52 | * |
||
53 | * @param bool FALSE if products must be assigned to categories, TRUE if not |
||
54 | * @since 2010.10 |
||
55 | */ |
||
56 | if( $context->config()->get( 'controller/frontend/product/show-all', false ) == false ) { |
||
57 | $this->addExpression( $this->filter->compare( '!=', 'index.catalog.id', null ) ); |
||
58 | } |
||
59 | |||
60 | $this->addExpression( $this->filter->getConditions() ); |
||
61 | } |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Clones objects in controller and resets values |
||
66 | */ |
||
67 | public function __clone() |
||
68 | { |
||
69 | $this->filter = clone $this->filter; |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Returns the aggregated count of products for the given key. |
||
75 | * |
||
76 | * @param string $key Search key to aggregate for, e.g. "index.attribute.id" |
||
77 | * @param string|null $value Search key for aggregating the value column |
||
78 | * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average) |
||
79 | * @return \Aimeos\Map Associative list of key values as key and the product count for this key as value |
||
80 | * @since 2019.04 |
||
81 | */ |
||
82 | public function aggregate( string $key, string $value = null, string $type = null ) : \Aimeos\Map |
||
83 | { |
||
84 | $this->filter->setConditions( $this->filter->and( $this->getConditions() ) ); |
||
85 | $this->filter->setSortations( $this->getSortations() ); |
||
86 | |||
87 | return $this->manager->aggregate( $this->filter, $key, $value, $type ); |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Adds attribute IDs for filtering where products must reference all IDs |
||
93 | * |
||
94 | * @param array|string $attrIds Attribute ID or list of IDs |
||
95 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
96 | * @since 2019.04 |
||
97 | */ |
||
98 | public function allOf( $attrIds ) : Iface |
||
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Adds catalog IDs for filtering |
||
112 | * |
||
113 | * @param array|string $catIds Catalog ID or list of IDs |
||
114 | * @param string $listtype List type of the products referenced by the categories |
||
115 | * @param int $level Constant from \Aimeos\MW\Tree\Manager\Base if products in subcategories are matched too |
||
116 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
117 | * @since 2019.04 |
||
118 | */ |
||
119 | public function category( $catIds, string $listtype = 'default', int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) : Iface |
||
120 | { |
||
121 | if( !empty( $catIds ) && ( $ids = $this->validateIds( (array) $catIds ) ) !== [] ) |
||
122 | { |
||
123 | if( $level != \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) |
||
124 | { |
||
125 | $list = map(); |
||
126 | $cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'catalog' ); |
||
127 | |||
128 | foreach( $ids as $catId ) { |
||
129 | $list->union( $cntl->root( $catId )->getTree( $level )->toList() ); |
||
130 | } |
||
131 | |||
132 | $ids = $this->validateIds( $list->keys()->toArray() ); |
||
133 | } |
||
134 | |||
135 | $func = $this->filter->make( 'index.catalog:position', [$listtype, $ids] ); |
||
136 | |||
137 | $this->addExpression( $this->filter->compare( '==', 'index.catalog.id', $ids ) ); |
||
138 | $this->addExpression( $this->filter->compare( '>=', $func, 0 ) ); |
||
139 | |||
140 | $func = $this->filter->make( 'sort:index.catalog:position', [$listtype, $ids] ); |
||
141 | $this->addExpression( $this->filter->sort( '+', $func ) ); |
||
142 | $this->addExpression( $this->filter->sort( '+', 'product.id' ) ); // prevent flaky order if products have same position |
||
143 | } |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Adds generic condition for filtering products |
||
151 | * |
||
152 | * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
||
153 | * @param string $key Search key defined by the product manager, e.g. "product.status" |
||
154 | * @param array|string $value Value or list of values to compare to |
||
155 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
156 | * @since 2019.04 |
||
157 | */ |
||
158 | public function compare( string $operator, string $key, $value ) : Iface |
||
159 | { |
||
160 | $this->addExpression( $this->filter->compare( $operator, $key, $value ) ); |
||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | |||
165 | /** |
||
166 | * Returns the product for the given product code |
||
167 | * |
||
168 | * @param string $code Unique product code |
||
169 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
170 | * @since 2019.04 |
||
171 | */ |
||
172 | public function find( string $code ) : \Aimeos\MShop\Product\Item\Iface |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Creates a search function string for the given name and parameters |
||
181 | * |
||
182 | * @param string $name Name of the search function without parenthesis, e.g. "product:has" |
||
183 | * @param array $params List of parameters for the search function with numeric keys starting at 0 |
||
184 | * @return string Search function string that can be used in compare() |
||
185 | */ |
||
186 | public function function( string $name, array $params ) : string |
||
189 | } |
||
190 | |||
191 | |||
192 | /** |
||
193 | * Returns the product for the given product ID |
||
194 | * |
||
195 | * @param string $id Unique product ID |
||
196 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
197 | * @since 2019.04 |
||
198 | */ |
||
199 | public function get( string $id ) : \Aimeos\MShop\Product\Item\Iface |
||
200 | { |
||
201 | $item = $this->manager->get( $id, $this->domains, null ); |
||
202 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $item, 'catalog' ); |
||
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Adds a filter to return only items containing a reference to the given ID |
||
208 | * |
||
209 | * @param string $domain Domain name of the referenced item, e.g. "attribute" |
||
210 | * @param string|null $type Type code of the reference, e.g. "variant" or null for all types |
||
211 | * @param string|null $refId ID of the referenced item of the given domain or null for all references |
||
212 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
213 | * @since 2019.04 |
||
214 | */ |
||
215 | public function has( string $domain, string $type = null, string $refId = null ) : Iface |
||
216 | { |
||
217 | $params = [$domain]; |
||
218 | !$type ?: $params[] = $type; |
||
219 | !$refId ?: $params[] = $refId; |
||
220 | |||
221 | $func = $this->filter->make( 'product:has', $params ); |
||
222 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Adds attribute IDs for filtering where products must reference at least one ID |
||
229 | * |
||
230 | * If an array of ID lists is given, each ID list is added separately as condition. |
||
231 | * |
||
232 | * @param array|string $attrIds Attribute ID, list of IDs or array of lists with IDs |
||
233 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
234 | * @since 2019.04 |
||
235 | */ |
||
236 | public function oneOf( $attrIds ) : Iface |
||
257 | } |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Parses the given array and adds the conditions to the list of conditions |
||
262 | * |
||
263 | * @param array $conditions List of conditions, e.g. [['>' => ['product.status' => 0]], ['==' => ['product.type' => 'default']]] |
||
264 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
265 | * @since 2019.04 |
||
266 | */ |
||
267 | public function parse( array $conditions ) : Iface |
||
274 | } |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Adds price restrictions for filtering |
||
279 | * |
||
280 | * @param array|string $value Upper price limit, list of lower and upper price or NULL for no restrictions |
||
281 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
282 | * @since 2020.10 |
||
283 | */ |
||
284 | public function price( $value = null ) : Iface |
||
285 | { |
||
286 | if( $value ) |
||
287 | { |
||
288 | $value = (array) $value; |
||
289 | $func = $this->filter->make( 'index.price:value', [$this->context()->locale()->getCurrencyId()] ); |
||
290 | |||
291 | $this->addExpression( $this->filter->compare( '<=', $func, sprintf( '%013.2F', end( $value ) ) ) ); |
||
292 | |||
293 | if( count( $value ) > 1 ) { |
||
294 | $this->addExpression( $this->filter->compare( '>=', $func, sprintf( '%013.2F', reset( $value ) ) ) ); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | |||
302 | /** |
||
303 | * Adds product IDs for filtering |
||
304 | * |
||
305 | * @param array|string $prodIds Product ID or list of IDs |
||
306 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
307 | * @since 2019.04 |
||
308 | */ |
||
309 | public function product( $prodIds ) : Iface |
||
310 | { |
||
311 | if( !empty( $prodIds ) && ( $ids = array_unique( $this->validateIds( (array) $prodIds ) ) ) !== [] ) { |
||
312 | $this->addExpression( $this->filter->compare( '==', 'product.id', $ids ) ); |
||
313 | } |
||
314 | |||
315 | return $this; |
||
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * Adds a filter to return only items containing the property |
||
321 | * |
||
322 | * @param string $type Type code of the property, e.g. "isbn" |
||
323 | * @param string|null $value Exact value of the property |
||
324 | * @param string|null $langid ISO country code (en or en_US) or null if not language specific |
||
325 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
326 | * @since 2019.04 |
||
327 | */ |
||
328 | public function property( string $type, string $value = null, string $langid = null ) : Iface |
||
333 | } |
||
334 | |||
335 | |||
336 | /** |
||
337 | * Adds radius restrictions for filtering |
||
338 | * |
||
339 | * @param array $latlon Latitude and longitude value or empty for no restrictions |
||
340 | * @param float|null $dist Distance around latitude/longitude or NULL for no restrictions |
||
341 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
342 | * @since 2021.10 |
||
343 | */ |
||
344 | public function radius( array $latlon, float $dist = null ) : \Aimeos\Controller\Frontend\Product\Iface |
||
345 | { |
||
346 | if( $dist && count( $latlon ) === 2 ) |
||
347 | { |
||
348 | $func = $this->filter->make( 'index.supplier:radius', [reset( $latlon ), end( $latlon ), $dist] ); |
||
349 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
350 | } |
||
351 | |||
352 | return $this; |
||
353 | } |
||
354 | |||
355 | |||
356 | /** |
||
357 | * Returns the product for the given product URL name |
||
358 | * |
||
359 | * @param string $name Product URL name |
||
360 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
361 | * @since 2019.04 |
||
362 | */ |
||
363 | public function resolve( string $name ) : \Aimeos\MShop\Product\Item\Iface |
||
364 | { |
||
365 | $search = $this->manager->filter( null )->slice( 0, 1 )->add( ['index.text:url()' => $name] ); |
||
366 | |||
367 | if( ( $item = $this->manager->search( $search, $this->domains )->first() ) === null ) |
||
368 | { |
||
369 | $msg = $this->context()->translate( 'controller/frontend', 'Unable to find product "%1$s"' ); |
||
370 | throw new \Aimeos\Controller\Frontend\Product\Exception( sprintf( $msg, $name ), 404 ); |
||
371 | } |
||
372 | |||
373 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $item, 'catalog' ); |
||
374 | } |
||
375 | |||
376 | |||
377 | /** |
||
378 | * Returns the products filtered by the previously assigned conditions |
||
379 | * |
||
380 | * @param int &$total Parameter where the total number of found products will be stored in |
||
381 | * @return \Aimeos\Map Ordered list of product items implementing \Aimeos\MShop\Product\Item\Iface |
||
382 | * @since 2019.04 |
||
383 | */ |
||
384 | public function search( int &$total = null ) : \Aimeos\Map |
||
385 | { |
||
386 | $filter = clone $this->filter; |
||
387 | |||
388 | /** controller/frontend/common/max-size |
||
389 | * Maximum number of items that can be fetched at once |
||
390 | * |
||
391 | * This setting limits the number of items that is returned to the frontend. |
||
392 | * The frontend can request any number of items up to that hard limit to |
||
393 | * prevent denial of service attacks by requesting large amount of data. |
||
394 | * |
||
395 | * @param int Number of items |
||
396 | */ |
||
397 | $maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 ); |
||
398 | $filter->slice( $filter->getOffset(), min( $filter->getLimit(), $maxsize ) ); |
||
399 | |||
400 | $filter->setSortations( $this->getSortations() ); |
||
401 | $filter->setConditions( $filter->and( $this->getConditions() ) ); |
||
402 | |||
403 | $items = $this->manager->search( $filter, $this->domains, $total ); |
||
404 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $items, 'catalog' ); |
||
405 | } |
||
406 | |||
407 | |||
408 | /** |
||
409 | * Sets the start value and the number of returned products for slicing the list of found products |
||
410 | * |
||
411 | * @param int $start Start value of the first product in the list |
||
412 | * @param int $limit Number of returned products |
||
413 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
414 | * @since 2019.04 |
||
415 | */ |
||
416 | public function slice( int $start, int $limit ) : Iface |
||
417 | { |
||
418 | $this->filter->slice( $start, $limit ); |
||
419 | return $this; |
||
420 | } |
||
421 | |||
422 | |||
423 | /** |
||
424 | * Sets the sorting of the result list |
||
425 | * |
||
426 | * @param string|null $key Sorting of the result list like "name", "-name", "price", "-price", "code", "-code", |
||
427 | * "ctime, "-ctime", "relevance" or comma separated combinations and null for no sorting |
||
428 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
429 | * @since 2019.04 |
||
430 | */ |
||
431 | public function sort( string $key = null ) : Iface |
||
432 | { |
||
433 | $list = $this->splitKeys( $key ); |
||
434 | |||
435 | foreach( $list as $sortkey ) |
||
436 | { |
||
437 | $direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
||
438 | $sortkey = ltrim( $sortkey, '+-' ); |
||
439 | |||
440 | switch( $sortkey ) |
||
441 | { |
||
442 | case 'relevance': |
||
443 | break; |
||
444 | |||
445 | case 'code': |
||
446 | $this->addExpression( $this->filter->sort( $direction, 'product.code' ) ); |
||
447 | break; |
||
448 | |||
449 | case 'ctime': |
||
450 | $this->addExpression( $this->filter->sort( $direction, 'product.ctime' ) ); |
||
451 | break; |
||
452 | |||
453 | case 'name': |
||
454 | $langid = $this->context()->locale()->getLanguageId(); |
||
455 | |||
456 | $cmpfunc = $this->filter->make( 'index.text:name', [$langid] ); |
||
457 | $this->addExpression( $this->filter->compare( '!=', $cmpfunc, null ) ); |
||
458 | |||
459 | $sortfunc = $this->filter->make( 'sort:index.text:name', [$langid] ); |
||
460 | $this->addExpression( $this->filter->sort( $direction, $sortfunc ) ); |
||
461 | break; |
||
462 | |||
463 | case 'price': |
||
464 | $currencyid = $this->context()->locale()->getCurrencyId(); |
||
465 | $sortfunc = $this->filter->make( 'sort:index.price:value', [$currencyid] ); |
||
466 | |||
467 | $cmpfunc = $this->filter->make( 'index.price:value', [$currencyid] ); |
||
468 | $this->addExpression( $this->filter->compare( '!=', $cmpfunc, null ) ); |
||
469 | |||
470 | $this->addExpression( $this->filter->sort( $direction, $sortfunc ) ); |
||
471 | break; |
||
472 | |||
473 | default: |
||
474 | $this->addExpression( $this->filter->sort( $direction, $sortkey ) ); |
||
475 | } |
||
476 | } |
||
477 | |||
478 | return $this; |
||
479 | } |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Adds supplier IDs for filtering |
||
484 | * |
||
485 | * @param array|string $supIds Supplier ID or list of IDs |
||
486 | * @param string $listtype List type of the products referenced by the suppliers |
||
487 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
488 | * @since 2019.04 |
||
489 | */ |
||
490 | public function supplier( $supIds, string $listtype = 'default' ) : Iface |
||
491 | { |
||
492 | if( !empty( $supIds ) && ( $ids = array_unique( $this->validateIds( (array) $supIds ) ) ) !== [] ) |
||
493 | { |
||
494 | $func = $this->filter->make( 'index.supplier:position', [$listtype, $ids] ); |
||
495 | |||
496 | $this->addExpression( $this->filter->compare( '==', 'index.supplier.id', $ids ) ); |
||
497 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
498 | |||
499 | $func = $this->filter->make( 'sort:index.supplier:position', [$listtype, $ids] ); |
||
500 | $this->addExpression( $this->filter->sort( '+', $func ) ); |
||
501 | } |
||
502 | |||
503 | return $this; |
||
504 | } |
||
505 | |||
506 | |||
507 | /** |
||
508 | * Adds input string for full text search |
||
509 | * |
||
510 | * @param string|null $text User input for full text search |
||
511 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
512 | * @since 2019.04 |
||
513 | */ |
||
514 | public function text( string $text = null ) : Iface |
||
515 | { |
||
516 | if( !empty( $text ) ) |
||
517 | { |
||
518 | $langid = $this->context()->locale()->getLanguageId(); |
||
519 | $func = $this->filter->make( 'index.text:relevance', [$langid, $text] ); |
||
520 | $sortfunc = $this->filter->make( 'sort:index.text:relevance', [$langid, $text] ); |
||
521 | |||
522 | $this->addExpression( $this->filter->or( [ |
||
523 | $this->filter->compare( '>', $func, 0 ), |
||
524 | $this->filter->compare( '=~', 'product.code', $text ), |
||
525 | ] ) ); |
||
526 | $this->addExpression( $this->filter->sort( '-', $sortfunc ) ); |
||
527 | } |
||
528 | |||
529 | return $this; |
||
530 | } |
||
531 | |||
532 | |||
533 | /** |
||
534 | * Sets the referenced domains that will be fetched too when retrieving items |
||
535 | * |
||
536 | * @param array $domains Domain names of the referenced items that should be fetched too |
||
537 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
538 | * @since 2019.04 |
||
539 | */ |
||
540 | public function uses( array $domains ) : Iface |
||
541 | { |
||
542 | $this->domains = $domains; |
||
543 | return $this; |
||
544 | } |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Returns the list of catalog IDs for the given catalog tree |
||
549 | * |
||
550 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item with children |
||
551 | * @return array List of catalog IDs |
||
552 | */ |
||
553 | protected function getCatalogIdsFromTree( \Aimeos\MShop\Catalog\Item\Iface $item ) : array |
||
554 | { |
||
555 | if( $item->getStatus() < 1 ) { |
||
556 | return []; |
||
557 | } |
||
558 | |||
559 | $list = [$item->getId()]; |
||
560 | |||
561 | foreach( $item->getChildren() as $child ) { |
||
562 | $list = array_merge( $list, $this->getCatalogIdsFromTree( $child ) ); |
||
563 | } |
||
564 | |||
565 | return $list; |
||
566 | } |
||
567 | |||
568 | |||
569 | /** |
||
570 | * Returns the manager used by the controller |
||
571 | * |
||
572 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object |
||
573 | */ |
||
574 | protected function getManager() : \Aimeos\MShop\Common\Manager\Iface |
||
575 | { |
||
576 | return $this->manager; |
||
577 | } |
||
578 | |||
579 | |||
580 | /** |
||
581 | * Validates the given IDs as integers |
||
582 | * |
||
583 | * @param array $ids List of IDs to validate |
||
584 | * @return array List of validated IDs |
||
585 | */ |
||
586 | protected function validateIds( array $ids ) : array |
||
600 | } |
||
601 | } |
||
602 |