Total Complexity | 60 |
Total Lines | 560 |
Duplicated Lines | 0 % |
Changes | 24 | ||
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 |
||
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\Context\Item\Iface $context Common MShop context object |
||
34 | */ |
||
35 | public function __construct( \Aimeos\MShop\Context\Item\Iface $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->getConfig()->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->getContext(), '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, true ); |
||
202 | return \Aimeos\MShop::create( $this->getContext(), '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->getContext()->getLocale()->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 | * Returns the product for the given product URL name |
||
338 | * |
||
339 | * @param string $name Product URL name |
||
340 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
341 | * @since 2019.04 |
||
342 | */ |
||
343 | public function resolve( string $name ) : \Aimeos\MShop\Product\Item\Iface |
||
354 | } |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Returns the products filtered by the previously assigned conditions |
||
359 | * |
||
360 | * @param int &$total Parameter where the total number of found products will be stored in |
||
361 | * @return \Aimeos\Map Ordered list of product items implementing \Aimeos\MShop\Product\Item\Iface |
||
362 | * @since 2019.04 |
||
363 | */ |
||
364 | public function search( int &$total = null ) : \Aimeos\Map |
||
365 | { |
||
366 | $filter = clone $this->filter; |
||
367 | |||
368 | /** controller/frontend/common/max-size |
||
369 | * Maximum number of items that can be fetched at once |
||
370 | * |
||
371 | * This setting limits the number of items that is returned to the frontend. |
||
372 | * The frontend can request any number of items up to that hard limit to |
||
373 | * prevent denial of service attacks by requesting large amount of data. |
||
374 | * |
||
375 | * @param int Number of items |
||
376 | */ |
||
377 | $maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 ); |
||
378 | $filter->slice( $filter->getOffset(), min( $filter->getLimit(), $maxsize ) ); |
||
379 | |||
380 | $filter->setSortations( $this->getSortations() ); |
||
381 | $filter->setConditions( $filter->and( $this->getConditions() ) ); |
||
382 | |||
383 | $items = $this->manager->search( $filter, $this->domains, $total ); |
||
384 | return \Aimeos\MShop::create( $this->getContext(), 'rule' )->apply( $items, 'catalog' ); |
||
385 | } |
||
386 | |||
387 | |||
388 | /** |
||
389 | * Sets the start value and the number of returned products for slicing the list of found products |
||
390 | * |
||
391 | * @param int $start Start value of the first product in the list |
||
392 | * @param int $limit Number of returned products |
||
393 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
394 | * @since 2019.04 |
||
395 | */ |
||
396 | public function slice( int $start, int $limit ) : Iface |
||
400 | } |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Sets the sorting of the result list |
||
405 | * |
||
406 | * @param string|null $key Sorting of the result list like "name", "-name", "price", "-price", "code", "-code", |
||
407 | * "ctime, "-ctime", "relevance" or comma separated combinations and null for no sorting |
||
408 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
409 | * @since 2019.04 |
||
410 | */ |
||
411 | public function sort( string $key = null ) : Iface |
||
459 | } |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Adds supplier IDs for filtering |
||
464 | * |
||
465 | * @param array|string $supIds Supplier ID or list of IDs |
||
466 | * @param string $listtype List type of the products referenced by the suppliers |
||
467 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
468 | * @since 2019.04 |
||
469 | */ |
||
470 | public function supplier( $supIds, string $listtype = 'default' ) : Iface |
||
484 | } |
||
485 | |||
486 | |||
487 | /** |
||
488 | * Adds input string for full text search |
||
489 | * |
||
490 | * @param string|null $text User input for full text search |
||
491 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
492 | * @since 2019.04 |
||
493 | */ |
||
494 | public function text( string $text = null ) : Iface |
||
495 | { |
||
496 | if( !empty( $text ) ) |
||
497 | { |
||
498 | $langid = $this->getContext()->getLocale()->getLanguageId(); |
||
499 | $func = $this->filter->make( 'index.text:relevance', [$langid, $text] ); |
||
500 | $sortfunc = $this->filter->make( 'sort:index.text:relevance', [$langid, $text] ); |
||
501 | |||
502 | $this->addExpression( $this->filter->or( [ |
||
503 | $this->filter->compare( '>', $func, 0 ), |
||
504 | $this->filter->compare( '=~', 'product.code', $text ), |
||
505 | ] ) ); |
||
506 | $this->addExpression( $this->filter->sort( '-', $sortfunc ) ); |
||
507 | } |
||
508 | |||
509 | return $this; |
||
510 | } |
||
511 | |||
512 | |||
513 | /** |
||
514 | * Sets the referenced domains that will be fetched too when retrieving items |
||
515 | * |
||
516 | * @param array $domains Domain names of the referenced items that should be fetched too |
||
517 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
518 | * @since 2019.04 |
||
519 | */ |
||
520 | public function uses( array $domains ) : Iface |
||
521 | { |
||
522 | $this->domains = $domains; |
||
523 | return $this; |
||
524 | } |
||
525 | |||
526 | |||
527 | /** |
||
528 | * Returns the list of catalog IDs for the given catalog tree |
||
529 | * |
||
530 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item with children |
||
531 | * @return array List of catalog IDs |
||
532 | */ |
||
533 | protected function getCatalogIdsFromTree( \Aimeos\MShop\Catalog\Item\Iface $item ) : array |
||
546 | } |
||
547 | |||
548 | |||
549 | /** |
||
550 | * Returns the manager used by the controller |
||
551 | * |
||
552 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object |
||
553 | */ |
||
554 | protected function getManager() : \Aimeos\MShop\Common\Manager\Iface |
||
555 | { |
||
556 | return $this->manager; |
||
557 | } |
||
558 | |||
559 | |||
560 | /** |
||
561 | * Validates the given IDs as integers |
||
562 | * |
||
563 | * @param array $ids List of IDs to validate |
||
564 | * @return array List of validated IDs |
||
565 | */ |
||
566 | protected function validateIds( array $ids ) : array |
||
580 | } |
||
581 | } |
||
582 |