Passed
Push — master ( baa5ed...e67b42 )
by Aimeos
05:46
created

Standard::saveItem()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 164
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 164
rs 8.3626
c 0
b 0
f 0
cc 7
nc 17
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2024
6
 * @package MShop
7
 * @subpackage Review
8
 */
9
10
11
namespace Aimeos\MShop\Review\Manager;
12
13
14
/**
15
 * Default review manager implementation
16
 *
17
 * @package MShop
18
 * @subpackage Review
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Review\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	/** mshop/review/manager/name
25
	 * Class name of the used review manager implementation
26
	 *
27
	 * Each default manager 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 manager factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\MShop\Review\Manager\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\MShop\Review\Manager\Mymanager
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  mshop/review/manager/name = Mymanager
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 "MyManager"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2020.10
55
	 * @category Developer
56
	 */
57
58
	/** mshop/review/manager/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the review manager
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
	 * "mshop/common/manager/decorators/default" before they are wrapped
68
	 * around the review manager.
69
	 *
70
	 *  mshop/review/manager/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
74
	 * "mshop/common/manager/decorators/default" for the review manager.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2020.10
78
	 * @category Developer
79
	 * @see mshop/common/manager/decorators/default
80
	 * @see mshop/review/manager/decorators/global
81
	 * @see mshop/review/manager/decorators/local
82
	 */
83
84
	/** mshop/review/manager/decorators/global
85
	 * Adds a list of globally available decorators only to the review manager
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\MShop\Common\Manager\Decorator\*") around the review
94
	 * manager.
95
	 *
96
	 *  mshop/review/manager/decorators/global = array( 'decorator1' )
97
	 *
98
	 * This would add the decorator named "decorator1" defined by
99
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the
100
	 * review manager.
101
	 *
102
	 * @param array List of decorator names
103
	 * @since 2020.10
104
	 * @category Developer
105
	 * @see mshop/common/manager/decorators/default
106
	 * @see mshop/review/manager/decorators/excludes
107
	 * @see mshop/review/manager/decorators/local
108
	 */
109
110
	/** mshop/review/manager/decorators/local
111
	 * Adds a list of local decorators only to the review manager
112
	 *
113
	 * Decorators extend the functionality of a class by adding new aspects
114
	 * (e.g. log what is currently done), executing the methods of the underlying
115
	 * class only in certain conditions (e.g. only for logged in users) or
116
	 * modify what is returned to the caller.
117
	 *
118
	 * This option allows you to wrap local decorators
119
	 * ("\Aimeos\MShop\Review\Manager\Decorator\*") around the review
120
	 * manager.
121
	 *
122
	 *  mshop/review/manager/decorators/local = array( 'decorator2' )
123
	 *
124
	 * This would add the decorator named "decorator2" defined by
125
	 * "\Aimeos\MShop\Review\Manager\Decorator\Decorator2" only to the
126
	 * review manager.
127
	 *
128
	 * @param array List of decorator names
129
	 * @since 2020.10
130
	 * @category Developer
131
	 * @see mshop/common/manager/decorators/default
132
	 * @see mshop/review/manager/decorators/excludes
133
	 * @see mshop/review/manager/decorators/global
134
	 */
135
136
137
	private array $searchConfig = array(
138
		'customerid' => array(
139
			'code' => 'review.customerid',
140
			'internalcode' => 'mrev."customerid"',
141
			'label' => 'Customer ID',
142
			'public' => false,
143
		),
144
		'ordprodid' => array(
145
			'code' => 'review.orderproductid',
146
			'internalcode' => 'mrev."ordprodid"',
147
			'label' => 'Order product ID',
148
			'public' => false,
149
		),
150
		'domain' => array(
151
			'code' => 'review.domain',
152
			'internalcode' => 'mrev."domain"',
153
			'label' => 'Domain',
154
		),
155
		'refid' => array(
156
			'code' => 'review.refid',
157
			'internalcode' => 'mrev."refid"',
158
			'label' => 'ID from the referenced domain',
159
		),
160
		'name' => array(
161
			'code' => 'review.name',
162
			'internalcode' => 'mrev."name"',
163
			'label' => 'Name',
164
		),
165
		'comment' => array(
166
			'code' => 'review.comment',
167
			'internalcode' => 'mrev."comment"',
168
			'label' => 'Comment',
169
		),
170
		'response' => array(
171
			'code' => 'review.response',
172
			'internalcode' => 'mrev."response"',
173
			'label' => 'Response',
174
		),
175
		'rating' => array(
176
			'code' => 'review.rating',
177
			'internalcode' => 'mrev."rating"',
178
			'label' => 'Rating',
179
			'type' => 'int',
180
		),
181
		'status' => array(
182
			'code' => 'review.status',
183
			'internalcode' => 'mrev."status"',
184
			'label' => 'Review status',
185
			'type' => 'int',
186
		),
187
	);
188
189
190
	/**
191
	 * Counts the number items that are available for the values of the given key.
192
	 *
193
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria
194
	 * @param array|string $key Search key or list of keys to aggregate items for
195
	 * @param string|null $value Search key for aggregating the value column
196
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
197
	 * @return \Aimeos\Map List of the search keys as key and the number of counted items as value
198
	 */
199
	public function aggregate( \Aimeos\Base\Criteria\Iface $search, $key, string $value = null, string $type = null ) : \Aimeos\Map
200
	{
201
		/** mshop/review/manager/aggregate/mysql
202
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
203
		 *
204
		 * @see mshop/review/manager/aggregate/ansi
205
		 */
206
207
		/** mshop/review/manager/aggregate/ansi
208
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
209
		 *
210
		 * Groups all records by the values in the key column and counts their
211
		 * occurence. The matched records can be limited by the given criteria
212
		 * from the review database. The records must be from one of the sites
213
		 * that are configured via the context item. If the current site is part
214
		 * of a tree of sites, the statement can count all records from the
215
		 * current site and the complete sub-tree of sites.
216
		 *
217
		 * As the records can normally be limited by criteria from sub-managers,
218
		 * their tables must be joined in the SQL context. This is done by
219
		 * using the "internaldeps" property from the definition of the ID
220
		 * column of the sub-managers. These internal dependencies specify
221
		 * the JOIN between the tables and the used columns for joining. The
222
		 * ":joins" placeholder is then replaced by the JOIN strings from
223
		 * the sub-managers.
224
		 *
225
		 * To limit the records matched, conditions can be added to the given
226
		 * criteria object. It can contain comparisons like column names that
227
		 * must match specific values which can be combined by AND, OR or NOT
228
		 * operators. The resulting string of SQL conditions replaces the
229
		 * ":cond" placeholder before the statement is sent to the database
230
		 * server.
231
		 *
232
		 * This statement doesn't return any records. Instead, it returns pairs
233
		 * of the different values found in the key column together with the
234
		 * number of records that have been found for that key values.
235
		 *
236
		 * The SQL statement should conform to the ANSI standard to be
237
		 * compatible with most relational database systems. This also
238
		 * includes using double quotes for table and column names.
239
		 *
240
		 * @param string SQL statement for aggregating review items
241
		 * @since 2020.10
242
		 * @category Developer
243
		 * @see mshop/review/manager/insert/ansi
244
		 * @see mshop/review/manager/update/ansi
245
		 * @see mshop/review/manager/newid/ansi
246
		 * @see mshop/review/manager/delete/ansi
247
		 * @see mshop/review/manager/search/ansi
248
		 * @see mshop/review/manager/count/ansi
249
		 */
250
		$cfgkey = 'mshop/review/manager/aggregate';
251
		$cfgkey .= ( $type === 'rate' ? 'rate' : '' );
252
253
		return $this->aggregateBase( $search, $key, $cfgkey, [], $value, $type );
254
	}
255
256
257
	/**
258
	 * Creates a new empty item instance
259
	 *
260
	 * @param array $values Values the item should be initialized with
261
	 * @return \Aimeos\MShop\Review\Item\Iface New review item object
262
	 */
263
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
264
	{
265
		$values['review.siteid'] = $values['review.siteid'] ?? $this->context()->locale()->getSiteId();
266
		return new \Aimeos\MShop\Review\Item\Standard( 'review.', $values );
267
	}
268
269
270
	/**
271
	 * Creates a filter object.
272
	 *
273
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
274
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
275
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
276
	 */
277
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
278
	{
279
		return $this->filterBase( 'review', $default );
280
	}
281
282
283
	/**
284
	 * Returns the additional column/search definitions
285
	 *
286
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
287
	 */
288
	public function getSaveAttributes() : array
289
	{
290
		return $this->createAttributes( $this->searchConfig );
291
	}
292
293
294
	/**
295
	 * Returns the prefix for the item properties and search keys.
296
	 *
297
	 * @return string Prefix for the item properties and search keys
298
	 */
299
	protected function getPrefix() : string
300
	{
301
		return 'review.';
302
	}
303
}
304