Passed
Push — master ( 8b8a73...4b518d )
by Aimeos
03:02
created

Standard::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
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
	private $conditions = [];
25
	private $filter;
26
	private $manager;
27
28
29
	/**
30
	 * Common initialization for controller classes
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
35
	{
36
		parent::__construct( $context );
37
38
		$this->manager = \Aimeos\MShop::create( $context, 'review' );
39
		$this->filter = $this->manager->createSearch();
40
	}
41
42
43
	/**
44
	 * Clones objects in controller and resets values
45
	 */
46
	public function __clone()
47
	{
48
		$this->filter = clone $this->filter;
49
	}
50
51
52
	/**
53
	 * Adds generic condition for filtering
54
	 *
55
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
56
	 * @param string $key Search key defined by the review manager, e.g. "review.status"
57
	 * @param array|string $value Value or list of values to compare to
58
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
59
	 * @since 2020.10
60
	 */
61
	public function compare( string $operator, string $key, $value ) : Iface
62
	{
63
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
64
		return $this;
65
	}
66
67
68
	/**
69
	 * Deletes the review item for the given ID
70
	 *
71
	 * @param string $id Unique review ID
72
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
73
	 * @since 2020.10
74
	 */
75
	public function delete( string $id ) : Iface
76
	{
77
		$item = $this->manager->get( $id );
78
79
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order/base' );
80
		$filter = $manager->filter( true )->add( ['order.base.product.id' => $item->getOrderProductId()] );
81
82
		if( $manager->search( $filter->slice( 0, 1 ) )->empty() )
83
		{
84
			$msg = sprintf( 'You are not allowed to delete the review' );
85
			throw new \Aimeos\Controller\Frontend\Review\Exception( $msg );
86
		}
87
88
		$this->manager->delete( $id );
89
		return $this;
90
	}
91
92
93
	/**
94
	 * Sets the review domain for filtering
95
	 *
96
	 * @param string $domain Domain ("customer" or "product") of the reviewed items
97
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
98
	 * @since 2020.10
99
	 */
100
	public function domain( string $domain ) : Iface
101
	{
102
		$this->conditions['domain'] = $this->filter->compare( '==', 'review.domain', $domain );
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Restricts the ratings to a specific domain item
109
	 *
110
	 * @param string $domain Domain the ratings belong to ("customer" or "product")
111
	 * @param string $refid Id of the item the ratings belong to
112
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
113
	 * @since 2020.10
114
	 */
115
	public function for( string $domain, string $refid ) : Iface
116
	{
117
		$this->conditions['domain'] = $this->filter->compare( '==', 'review.domain', $domain );
118
		$this->conditions['refid'] = $this->filter->compare( '==', 'review.refid', $refid );
119
		return $this;
120
	}
121
122
123
	/**
124
	 * Returns the review item for the given ID
125
	 *
126
	 * @param string $id Unique review ID
127
	 * @return \Aimeos\MShop\Review\Item\Iface Review object
128
	 */
129
	public function get( string $id ) : \Aimeos\MShop\Review\Item\Iface
130
	{
131
		return $this->manager->getItem( $id, [], true );
132
	}
133
134
135
	/**
136
	 * Returns the reviews for the logged-in user
137
	 *
138
	 * @param int &$total Parameter where the total number of found reviews will be stored in
139
	 * @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface
140
	 * @since 2020.10
141
	 */
142
	public function list( int &$total = null ) : \Aimeos\Map
143
	{
144
		$filter = clone $this->filter;
145
		$cond = $filter->is( 'review.customerid', '==', $this->getContext()->getUserId() );
146
147
		$filter->setConditions( $filter->combine( '&&', array_merge( $this->conditions, [$cond] ) ) );
148
		return $this->manager->searchItems( $filter, [], $total );
149
	}
150
151
152
	/**
153
	 * Parses the given array and adds the conditions to the list of conditions
154
	 *
155
	 * @param array $conditions List of conditions, e.g. ['>' => ['review.rating' => 3]]
156
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
157
	 * @since 2020.10
158
	 */
159
	public function parse( array $conditions ) : Iface
160
	{
161
		if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) {
162
			$this->conditions[] = $cond;
163
		}
164
165
		return $this;
166
	}
167
168
169
	/**
170
	 * Saves the modified review item
171
	 *
172
	 * @param \Aimeos\MShop\Review\Item\Iface $item Review object
173
	 * @return \Aimeos\MShop\Review\Item\Iface Saved review item
174
	 */
175
	public function save( \Aimeos\MShop\Review\Item\Iface $item ) : \Aimeos\MShop\Review\Item\Iface
176
	{
177
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order/base' );
178
		$filter = $manager->filter( true )->add( ['order.base.product.id' => $item->getOrderProductId()] );
179
180
		if( $manager->search( $filter->slice( 0, 1 ) )->empty() )
181
		{
182
			$msg = sprintf( 'You are not allowed to add or change the review' );
183
			throw new \Aimeos\Controller\Frontend\Review\Exception( $msg );
184
		}
185
186
		return $this->manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

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

186
		return $this->manager->/** @scrutinizer ignore-call */ saveItem( $item );

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...
187
	}
188
189
190
	/**
191
	 * Returns the reviews filtered by the previously assigned conditions
192
	 *
193
	 * @param int &$total Parameter where the total number of found reviews will be stored in
194
	 * @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface
195
	 * @since 2020.10
196
	 */
197
	public function search( int &$total = null ) : \Aimeos\Map
198
	{
199
		$filter = clone $this->filter;
200
		$cond = $filter->is( 'review.status', '>', 0 );
201
202
		$filter->setConditions( $filter->combine( '&&', array_merge( $this->conditions, [$cond] ) ) );
203
		return $this->manager->searchItems( $filter, [], $total );
204
	}
205
206
207
	/**
208
	 * Sets the start value and the number of returned review items for slicing the list of found review items
209
	 *
210
	 * @param int $start Start value of the first review item in the list
211
	 * @param int $limit Number of returned review items
212
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
213
	 * @since 2020.10
214
	 */
215
	public function slice( int $start, int $limit ) : Iface
216
	{
217
		$this->filter->setSlice( $start, $limit );
218
		return $this;
219
	}
220
221
222
	/**
223
	 * Sets the sorting of the result list
224
	 *
225
	 * @param string|null $key Sorting key of the result list like "mtime" or "rating", null for no sorting
226
	 * @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface
227
	 * @since 2020.10
228
	 */
229
	public function sort( string $key = null ) : Iface
230
	{
231
		$sort = [];
232
		$list = ( $key ? explode( ',', $key ) : [] );
233
234
		foreach( $list as $sortkey )
235
		{
236
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
237
			$sortkey = ltrim( $sortkey, '+-' );
238
239
			switch( $sortkey )
240
			{
241
				case 'mtime':
242
					$sort[] = $this->filter->sort( $direction, 'review.mtime' );
243
					break;
244
				case 'rating':
245
					$sort[] = $this->filter->sort( $direction, 'review.rating' );
246
					break;
247
				default:
248
					$sort[] = $this->filter->sort( $direction, $sortkey );
249
			}
250
		}
251
252
		$this->filter->setSortations( $sort );
253
		return $this;
254
	}
255
}
256