Standard::for()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Review;
12
13
14
/**
15
 * Default implementation of the review frontend controller.
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	/** controller/frontend/review/name
25
	 * Class name of the used review frontend controller implementation
26
	 *
27
	 * Each default frontend controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Frontend\Review\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Review\Myreview
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/frontend/review/name = Myreview
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyReview"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2020.10
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/review/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the review frontend controllers
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/frontend/common/decorators/default" before they are wrapped
68
	 * around the frontend controller.
69
	 *
70
	 *  controller/frontend/review/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
74
	 * "controller/frontend/common/decorators/default" for the review frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2020.10
78
	 * @category Developer
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/review/decorators/global
81
	 * @see controller/frontend/review/decorators/local
82
	 */
83
84
	/** controller/frontend/review/decorators/global
85
	 * Adds a list of globally available decorators only to the review frontend controllers
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
94
	 *
95
	 *  controller/frontend/review/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2020.10
102
	 * @category Developer
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/review/decorators/excludes
105
	 * @see controller/frontend/review/decorators/local
106
	 */
107
108
	/** controller/frontend/review/decorators/local
109
	 * Adds a list of local decorators only to the review frontend controllers
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Frontend\Review\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/review/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Catalog\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2020.10
127
	 * @category Developer
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/review/decorators/excludes
130
	 * @see controller/frontend/review/decorators/global
131
	 */
132
133
134
	private \Aimeos\Base\Criteria\Iface $filter;
135
	private \Aimeos\MShop\Common\Manager\Iface $manager;
136
137
138
	/**
139
	 * Common initialization for controller classes
140
	 *
141
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
142
	 */
143
	public function __construct( \Aimeos\MShop\ContextIface $context )
144
	{
145
		parent::__construct( $context );
146
147
		$this->manager = \Aimeos\MShop::create( $context, 'review' );
148
		$this->filter = $this->manager->filter( true );
149
	}
150
151
152
	/**
153
	 * Clones objects in controller
154
	 */
155
	public function __clone()
156
	{
157
		$this->filter = clone $this->filter;
158
		parent::__clone();
159
	}
160
161
162
	/**
163
	 * Returns the aggregated count of products for the given key.
164
	 *
165
	 * @param string $key Search key to aggregate for, e.g. "review.rating"
166
	 * @param string|null $value Search key for aggregating the value column
167
	 * @param string|null $type Type of the aggregation, empty string for count or "sum"
168
	 * @return \Aimeos\Map Associative list of key values as key and the product count for this key as value
169
	 * @since 2020.10
170
	 */
171
	public function aggregate( string $key, ?string $value = null, ?string $type = null ) : \Aimeos\Map
172
	{
173
		$filter = clone $this->filter;
174
		$cond = $filter->is( 'review.status', '>', 0 );
175
176
		$filter->add( $filter->and( array_merge( $this->getConditions(), [$cond] ) ) );
177
		return $this->manager->aggregate( $filter, $key, $value, $type );
178
	}
179
180
181
	/**
182
	 * Adds generic condition for filtering
183
	 *
184
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
185
	 * @param string $key Search key defined by the review manager, e.g. "review.status"
186
	 * @param array|string $value Value or list of values to compare to
187
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
188
	 * @since 2020.10
189
	 */
190
	public function compare( string $operator, string $key, $value ) : Iface
191
	{
192
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns a new rating item
199
	 *
200
	 * @param array $vals Associative list of key/value pairs to initialize the item
201
	 * @return \Aimeos\MShop\Review\Item\Iface New review item
202
	 */
203
	public function create( array $vals = [] ) : \Aimeos\MShop\Review\Item\Iface
204
	{
205
		return $this->manager->create()->setOrderProductId( $vals['review.orderproductid'] ?? '' )->fromArray( $vals );
206
	}
207
208
209
	/**
210
	 * Deletes the review item for the given ID or IDs
211
	 *
212
	 * @param array|string $id Unique review ID or list of IDs
213
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
214
	 * @since 2020.10
215
	 */
216
	public function delete( $ids ) : Iface
217
	{
218
		$ids = (array) $ids;
219
		$filter = $this->manager->filter()->add( ['review.id' => $ids, 'review.customerid' => $this->context()->user()] );
220
		$this->manager->delete( $this->manager->search( $filter->slice( 0, count( $ids ) ) )->toArray() );
221
222
		return $this;
223
	}
224
225
226
	/**
227
	 * Sets the review domain for filtering
228
	 *
229
	 * @param string $domain Domain (e.g. "product") of the reviewed items
230
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
231
	 * @since 2020.10
232
	 */
233
	public function domain( string $domain ) : Iface
234
	{
235
		$this->addExpression( $this->filter->compare( '==', 'review.domain', $domain ) );
236
		return $this;
237
	}
238
239
240
	/**
241
	 * Restricts the reviews to a specific domain item
242
	 *
243
	 * @param string $domain Domain the reviews belong to (e.g. "product")
244
	 * @param array|string|null $refid Id of the item the reviews belong to, list of or NULL for all reviews from the domain
245
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
246
	 * @since 2020.10
247
	 */
248
	public function for( string $domain, $refid ) : Iface
249
	{
250
		$this->addExpression( $this->filter->compare( '==', 'review.domain', $domain ) );
251
252
		if( $refid !== null ) {
253
			$this->addExpression( $this->filter->compare( '==', 'review.refid', $refid ) );
254
		}
255
256
		return $this;
257
	}
258
259
260
	/**
261
	 * Returns the review item for the given ID
262
	 *
263
	 * @param string $id Unique review ID
264
	 * @return \Aimeos\MShop\Review\Item\Iface Review object
265
	 */
266
	public function get( string $id ) : \Aimeos\MShop\Review\Item\Iface
267
	{
268
		return $this->manager->get( $id, [], null );
269
	}
270
271
272
	/**
273
	 * Returns the reviews for the logged-in user
274
	 *
275
	 * @param int &$total Parameter where the total number of found reviews will be stored in
276
	 * @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface
277
	 * @since 2020.10
278
	 */
279
	public function list( ?int &$total = null ) : \Aimeos\Map
280
	{
281
		$filter = clone $this->filter;
282
		$cond = $filter->is( 'review.customerid', '==', $this->context()->user() );
283
284
		$filter->setConditions( $filter->and( array_merge( $this->getConditions(), [$cond] ) ) );
285
		$filter->setSortations( $this->getSortations() );
286
287
		return $this->manager->search( $filter, [], $total );
288
	}
289
290
291
	/**
292
	 * Parses the given array and adds the conditions to the list of conditions
293
	 *
294
	 * @param array $conditions List of conditions, e.g. ['>' => ['review.rating' => 3]]
295
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
296
	 * @since 2020.10
297
	 */
298
	public function parse( array $conditions ) : Iface
299
	{
300
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
301
			$this->addExpression( $cond );
302
		}
303
304
		return $this;
305
	}
306
307
308
	/**
309
	 * Adds or updates a review
310
	 *
311
	 * @param \Aimeos\MShop\Review\Item\Iface $item Review item including required data
312
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
313
	 * @since 2020.10
314
	 */
315
	public function save( \Aimeos\MShop\Review\Item\Iface $item ) : \Aimeos\MShop\Review\Item\Iface
316
	{
317
		$domain = $item->getDomain();
318
319
		if( !in_array( $domain, ['product', 'locale/site'] ) )
320
		{
321
			$msg = sprintf( 'Domain "%1$s" is not supported', $domain );
322
			throw new \Aimeos\Controller\Frontend\Review\Exception( $msg );
323
		}
324
325
		$context = $this->context();
326
		$manager = \Aimeos\MShop::create( $context, 'order' );
327
328
		$filter = $manager->filter( true )->add( [
329
			'order.product.id' => $item->getOrderProductId(),
330
			'order.customerid' => $context->user()
331
		] );
332
		$manager->search( $filter->slice( 0, 1 ) )->first( new \Aimeos\Controller\Frontend\Review\Exception(
333
			sprintf( 'You can only add a review if you have ordered a product' )
334
		) );
335
336
		$ordProdItem = \Aimeos\MShop::create( $context, 'order/product' )->get( $item->getOrderProductId() );
0 ignored issues
show
Bug introduced by
It seems like $item->getOrderProductId() can also be of type null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

336
		$ordProdItem = \Aimeos\MShop::create( $context, 'order/product' )->get( /** @scrutinizer ignore-type */ $item->getOrderProductId() );
Loading history...
337
338
		$filter = $this->manager->filter()->add( [
339
			'review.customerid' => $context->user(),
340
			'review.id' => $item->getId()
341
		] );
342
343
		/** controller/frontend/review/status
344
		 * Default status for new reviews
345
		 *
346
		 * By default, new reviews are stored with the status "in review" so they
347
		 * need to be approved by an admin or editor. Possible status values are:
348
		 *
349
		 * * 1 : enabled
350
		 * * 0 : disabled
351
		 * * -1 : in review
352
		 *
353
		 * @param integer Review status value
354
		 * @since 2020.10
355
		 */
356
		$status = $context->config()->get( 'controller/frontend/review/status', -1 );
357
358
		$real = $this->manager->search( $filter->slice( 0, 1 ) )->first( $this->manager->create() );
359
360
		$real = $real->setCustomerId( $context->user() )
361
			->setRefId( $ordProdItem->getType() === 'select' ? $ordProdItem->getParentProductId() : $ordProdItem->getProductId() )
362
			->setOrderProductId( $ordProdItem->getId() )
363
			->setComment( $item->getComment() )
364
			->setRating( $item->getRating() )
365
			->setName( $item->getName() )
366
			->setDomain( 'product' )
367
			->setStatus( $status );
368
369
		$item = $this->manager->save( $real );
370
371
		$filter = $this->manager->filter( true )->add( [
372
			'review.refid' => $item->getRefId(),
0 ignored issues
show
Bug introduced by
The method getRefId() does not exist on Aimeos\MShop\Common\Item\Iface. Did you maybe mean getId()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

372
			'review.refid' => $item->/** @scrutinizer ignore-call */ getRefId(),

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.

Loading history...
373
			'review.domain' => $domain,
374
		] );
375
376
		if( $status > 0
377
			&& ( $entry = $this->manager->aggregate( $filter, 'review.refid', 'review.rating', 'rate' )->first( [] ) ) !== []
378
			&& !empty( $cnt = current( $entry ) )
379
		) {
380
			$rateManager = \Aimeos\MShop::create( $context, $domain === 'product' ? 'index' : $domain );
381
			$rateManager->rate( $item->getRefId(), key( $entry ) / $cnt, $cnt );
382
383
			$context->cache()->deleteByTags( [$domain, $domain . '-' . $item->getRefId()] );
0 ignored issues
show
Bug introduced by
Are you sure $item->getRefId() of type Aimeos\MShop\Common\Item\Iface|Aimeos\Map|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

383
			$context->cache()->deleteByTags( [$domain, $domain . '-' . /** @scrutinizer ignore-type */ $item->getRefId()] );
Loading history...
384
		}
385
386
		return $item;
387
	}
388
389
390
	/**
391
	 * Returns the reviews filtered by the previously assigned conditions
392
	 *
393
	 * @param int &$total Parameter where the total number of found reviews will be stored in
394
	 * @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface
395
	 * @since 2020.10
396
	 */
397
	public function search( ?int &$total = null ) : \Aimeos\Map
398
	{
399
		$filter = clone $this->filter;
400
		$cond = $filter->is( 'review.status', '>', 0 );
401
402
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
403
		$filter->slice( $filter->getOffset(), min( $filter->getLimit(), $maxsize ) );
404
405
		$filter->setSortations( $this->getSortations() );
406
		$filter->add( $filter->and( array_merge( $this->getConditions(), [$cond] ) ) );
407
408
		return $this->manager->search( $filter, [], $total );
409
	}
410
411
412
	/**
413
	 * Sets the start value and the number of returned review items for slicing the list of found review items
414
	 *
415
	 * @param int $start Start value of the first review item in the list
416
	 * @param int $limit Number of returned review items
417
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
418
	 * @since 2020.10
419
	 */
420
	public function slice( int $start, int $limit ) : Iface
421
	{
422
		$this->filter->slice( $start, $limit );
423
		return $this;
424
	}
425
426
427
	/**
428
	 * Sets the sorting of the result list
429
	 *
430
	 * @param string|null $key Sorting key of the result list like "mtime" or "rating", null for no sorting
431
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
432
	 * @since 2020.10
433
	 */
434
	public function sort( ?string $key = null ) : Iface
435
	{
436
		$list = $this->splitKeys( $key );
437
438
		foreach( $list as $sortkey )
439
		{
440
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
441
			$sortkey = ltrim( $sortkey, '+-' );
442
443
			switch( $sortkey )
444
			{
445
				case 'ctime':
446
					$this->addExpression( $this->filter->sort( $direction, 'review.ctime' ) );
447
					break;
448
				case 'rating':
449
					$this->addExpression( $this->filter->sort( $direction, 'review.rating' ) );
450
					break;
451
				default:
452
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
453
			}
454
		}
455
456
		return $this;
457
	}
458
459
460
	/**
461
	 * Returns the manager used by the controller
462
	 *
463
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
464
	 */
465
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
466
	{
467
		return $this->manager;
468
	}
469
}
470