Laravel::getSearchAttributes()   C
last analyzed

Complexity

Conditions 11
Paths 1

Size

Total Lines 74
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 74
rs 6.8569
cc 11
nc 1
nop 1

How to fix   Long Method    Complexity   

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, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager;
12
13
14
/**
15
 * Customer class implementation for Laravel.
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class Laravel
21
	extends \Aimeos\MShop\Customer\Manager\Standard
22
{
23
	/**
24
	 * Counts the number items that are available for the values of the given key.
25
	 *
26
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria
27
	 * @param array|string $key Search key or list of key to aggregate items for
28
	 * @param string|null $value Search key for aggregating the value column
29
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
30
	 * @return \Aimeos\Map List of the search keys as key and the number of counted items as value
31
	 */
32
	public function aggregate( \Aimeos\Base\Criteria\Iface $search, $key, ?string $value = null, ?string $type = null ) : \Aimeos\Map
33
	{
34
		/** mshop/customer/manager/laravel/aggregate/mysql
35
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
36
		 *
37
		 * @see mshop/customer/manager/laravel/aggregate/ansi
38
		 */
39
40
		/** mshop/customer/manager/laravel/aggregate/ansi
41
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
42
		 *
43
		 * Groups all records by the values in the key column and counts their
44
		 * occurence. The matched records can be limited by the given criteria
45
		 * from the customer database. The records must be from one of the sites
46
		 * that are configured via the context item. If the current site is part
47
		 * of a tree of sites, the statement can count all records from the
48
		 * current site and the complete sub-tree of sites.
49
		 *
50
		 * As the records can normally be limited by criteria from sub-managers,
51
		 * their tables must be joined in the SQL context. This is done by
52
		 * using the "internaldeps" property from the definition of the ID
53
		 * column of the sub-managers. These internal dependencies specify
54
		 * the JOIN between the tables and the used columns for joining. The
55
		 * ":joins" placeholder is then replaced by the JOIN strings from
56
		 * the sub-managers.
57
		 *
58
		 * To limit the records matched, conditions can be added to the given
59
		 * criteria object. It can contain comparisons like column names that
60
		 * must match specific values which can be combined by AND, OR or NOT
61
		 * operators. The resulting string of SQL conditions replaces the
62
		 * ":cond" placeholder before the statement is sent to the database
63
		 * server.
64
		 *
65
		 * This statement doesn't return any records. Instead, it returns pairs
66
		 * of the different values found in the key column together with the
67
		 * number of records that have been found for that key values.
68
		 *
69
		 * The SQL statement should conform to the ANSI standard to be
70
		 * compatible with most relational database systems. This also
71
		 * includes using double quotes for table and column names.
72
		 *
73
		 * @param string SQL statement for aggregating customer items
74
		 * @since 2021.04
75
		 * @category Developer
76
		 * @see mshop/customer/manager/laravel/insert/ansi
77
		 * @see mshop/customer/manager/laravel/update/ansi
78
		 * @see mshop/customer/manager/laravel/newid/ansi
79
		 * @see mshop/customer/manager/laravel/delete/ansi
80
		 * @see mshop/customer/manager/laravel/search/ansi
81
		 * @see mshop/customer/manager/laravel/count/ansi
82
		 */
83
84
		$cfgkey = 'mshop/customer/manager/laravel/aggregate' . $type;
85
		return $this->aggregateBase( $search, $key, $cfgkey, ['customer'], $value );
86
	}
87
88
89
	/**
90
	 * Removes old entries from the storage.
91
	 *
92
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
93
	 * @return \Aimeos\MShop\Common\Manager\Iface Same object for fluent interface
94
	 */
95
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
96
	{
97
		$path = 'mshop/customer/manager/submanagers';
98
		$default = ['address', 'lists', 'property'];
99
100
		foreach( $this->context()->config()->get( $path, $default ) as $domain ) {
101
			$this->object()->getSubManager( $domain )->clear( $siteids );
102
		}
103
104
		return $this->clearBase( $siteids, 'mshop/customer/manager/laravel/clear' );
105
	}
106
107
108
	/**
109
	 * Removes multiple items.
110
	 *
111
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $items List of item objects or IDs of the items
112
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
113
	 */
114
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
115
	{
116
		return $this->deleteItemsBase( $items, 'mshop/customer/manager/laravel/delete' );
117
	}
118
119
120
	/**
121
	 * Returns the list attributes that can be used for searching.
122
	 *
123
	 * @param bool $withsub Return also attributes of sub-managers if true
124
	 * @return array List of attribute items implementing \Aimeos\Base\Criteria\Attribute\Iface
125
	 */
126
	public function getSearchAttributes( bool $withsub = true ) : array
127
	{
128
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
129
		$level = $this->context()->config()->get( 'mshop/customer/manager/sitemode', $level );
130
131
		return array_replace( parent::getSearchAttributes( $withsub ), $this->createAttributes( [
132
			'customer.code' => [
133
				'label' => 'Username',
134
				'internalcode' => 'email',
135
			],
136
			'customer.label' => [
137
				'label' => 'Label',
138
				'internalcode' => 'name',
139
			],
140
			'customer.dateverified' => [
141
				'label' => 'Customer verification date',
142
				'internalcode' => 'mcus."email_verified_at"',
143
				'type' => 'date',
144
			],
145
			'customer.ctime' => [
146
				'label' => 'Customer creation time',
147
				'internalcode' => 'mcus."created_at"',
148
				'type' => 'datetime',
149
			],
150
			'customer.mtime' => [
151
				'label' => 'Customer modification time',
152
				'internalcode' => 'mcus."updated_at"',
153
				'type' => 'datetime',
154
			],
155
			'customer:has' => [
156
				'code' => 'customer:has()',
157
				'internalcode' => ':site AND :key AND mcusli."id"',
158
				'internaldeps' => ['LEFT JOIN "users_list" AS mcusli ON ( mcusli."parentid" = mcus."id" )'],
159
				'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
160
				'type' => 'null',
161
				'public' => false,
162
				'function' => function( &$source, array $params ) use ( $level ) {
163
					$keys = [];
164
165
					foreach( (array) ( $params[1] ?? '' ) as $type ) {
166
						foreach( (array) ( $params[2] ?? '' ) as $id ) {
167
							$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
168
						}
169
					}
170
171
					$sitestr = $this->siteString( 'mcusli."siteid"', $level );
172
					$keystr = $this->toExpression( 'mcusli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
173
					$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
174
175
					return $params;
176
				}
177
			],
178
			'customer:prop' => [
179
				'code' => 'customer:prop()',
180
				'internalcode' => ':site AND :key AND mcuspr."id"',
181
				'internaldeps' => ['LEFT JOIN "users_property" AS mcuspr ON ( mcuspr."parentid" = mcus."id" )'],
182
				'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
183
				'type' => 'null',
184
				'public' => false,
185
				'function' => function( &$source, array $params ) use ( $level ) {
186
					$keys = [];
187
					$langs = array_key_exists( 1, $params ) ? ( $params[1] ?? 'null' ) : '';
188
189
					foreach( (array) $langs as $lang ) {
190
						foreach( (array) ( $params[2] ?? '' ) as $val ) {
191
							$keys[] = substr( $params[0] . '|' . ( $lang === null ? 'null|' : ( $lang ? $lang . '|' : '' ) ) . $val, 0, 255 );
192
						}
193
					}
194
195
					$sitestr = $this->siteString( 'mcuspr."siteid"', $level );
196
					$keystr = $this->toExpression( 'mcuspr."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
197
					$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
198
199
					return $params;
200
				}
201
			],
202
		] ) );
203
	}
204
205
206
	/**
207
	 * Saves a customer item object.
208
	 *
209
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
210
	 * @param boolean $fetch True if the new ID should be returned in the item
211
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
212
	 */
213
	protected function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface
214
	{
215
		$item = $this->addGroups( $item );
216
217
		if( !$item->isModified() ) {
218
			return $this->object()->saveRefs( $item, $fetch );
219
		}
220
221
		$context = $this->context();
222
		$conn = $context->db( $this->getResourceName() );
223
224
		$id = $item->getId();
225
		$billingAddress = $item->getPaymentAddress();
226
		$columns = $this->object()->getSaveAttributes();
227
228
		if( $id === null )
229
		{
230
			/** mshop/customer/manager/laravel/insert
231
			 * Inserts a new customer record into the database table
232
			 *
233
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
234
			 * the database and the newly created ID retrieved afterwards
235
			 * using the "newid" SQL statement.
236
			 *
237
			 * The SQL statement must be a string suitable for being used as
238
			 * prepared statement. It must include question marks for binding
239
			 * the values from the customer item to the statement before they are
240
			 * sent to the database server. The number of question marks must
241
			 * be the same as the number of columns listed in the INSERT
242
			 * statement. The order of the columns must correspond to the
243
			 * order in the save() method, so the correct values are
244
			 * bound to the columns.
245
			 *
246
			 * The SQL statement should conform to the ANSI standard to be
247
			 * compatible with most relational database systems. This also
248
			 * includes using double quotes for table and column names.
249
			 *
250
			 * @param string SQL statement for inserting records
251
			 * @since 2015.01
252
			 * @category Developer
253
			 * @see mshop/customer/manager/laravel/update
254
			 * @see mshop/customer/manager/laravel/newid
255
			 * @see mshop/customer/manager/laravel/delete
256
			 * @see mshop/customer/manager/laravel/search
257
			 * @see mshop/customer/manager/laravel/count
258
			 */
259
			$path = 'mshop/customer/manager/laravel/insert';
260
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig($path) can also be of type array; however, parameter $sql of Aimeos\MShop\Common\Manager\Base::addSqlColumns() 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

260
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
261
		}
262
		else
263
		{
264
			/** mshop/customer/manager/laravel/update
265
			 * Updates an existing customer record in the database
266
			 *
267
			 * Items which already have an ID (i.e. the ID is not NULL) will
268
			 * be updated in the database.
269
			 *
270
			 * The SQL statement must be a string suitable for being used as
271
			 * prepared statement. It must include question marks for binding
272
			 * the values from the customer item to the statement before they are
273
			 * sent to the database server. The order of the columns must
274
			 * correspond to the order in the save() method, so the
275
			 * correct values are bound to the columns.
276
			 *
277
			 * The SQL statement should conform to the ANSI standard to be
278
			 * compatible with most relational database systems. This also
279
			 * includes using double quotes for table and column names.
280
			 *
281
			 * @param string SQL statement for updating records
282
			 * @since 2015.01
283
			 * @category Developer
284
			 * @see mshop/customer/manager/laravel/insert
285
			 * @see mshop/customer/manager/laravel/newid
286
			 * @see mshop/customer/manager/laravel/delete
287
			 * @see mshop/customer/manager/laravel/search
288
			 * @see mshop/customer/manager/laravel/count
289
			 */
290
			$path = 'mshop/customer/manager/laravel/update';
291
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
292
		}
293
294
		$idx = 1;
295
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
296
297
		foreach( $columns as $name => $entry ) {
298
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
299
		}
300
301
		$stmt->bind( $idx++, $item->getLabel() );
302
		$stmt->bind( $idx++, $item->getCode() );
303
		$stmt->bind( $idx++, $billingAddress->getCompany() );
304
		$stmt->bind( $idx++, $billingAddress->getVatID() );
305
		$stmt->bind( $idx++, $billingAddress->getSalutation() );
306
		$stmt->bind( $idx++, $billingAddress->getTitle() );
307
		$stmt->bind( $idx++, $billingAddress->getFirstname() );
308
		$stmt->bind( $idx++, $billingAddress->getLastname() );
309
		$stmt->bind( $idx++, $billingAddress->getAddress1() );
310
		$stmt->bind( $idx++, $billingAddress->getAddress2() );
311
		$stmt->bind( $idx++, $billingAddress->getAddress3() );
312
		$stmt->bind( $idx++, $billingAddress->getPostal() );
313
		$stmt->bind( $idx++, $billingAddress->getCity() );
314
		$stmt->bind( $idx++, $billingAddress->getState() );
315
		$stmt->bind( $idx++, $billingAddress->getCountryId() );
316
		$stmt->bind( $idx++, $billingAddress->getLanguageId() );
317
		$stmt->bind( $idx++, $billingAddress->getTelephone() );
318
		$stmt->bind( $idx++, $billingAddress->getTelefax() );
319
		$stmt->bind( $idx++, $billingAddress->getMobile() );
320
		$stmt->bind( $idx++, $billingAddress->getWebsite() );
321
		$stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT );
322
		$stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT );
323
		$stmt->bind( $idx++, $billingAddress->getBirthday() );
324
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
325
		$stmt->bind( $idx++, $item->getDateVerified() );
326
		$stmt->bind( $idx++, $item->getPassword() );
327
		$stmt->bind( $idx++, $context->datetime() ); // Modification time
328
		$stmt->bind( $idx++, $context->editor() );
329
330
		if( $id !== null ) {
331
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
332
			$stmt->bind( $idx++, (string) $context->user()?->getSiteId() );
333
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
334
			$item->setId( $id );
335
		} else {
336
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
337
			$stmt->bind( $idx++, $context->datetime() ); // Creation time
338
		}
339
340
		$stmt->execute()->finish();
341
342
		if( $id === null && $fetch === true )
343
		{
344
			/** mshop/customer/manager/laravel/newid
345
			 * Retrieves the ID generated by the database when inserting a new record
346
			 *
347
			 * As soon as a new record is inserted into the database table,
348
			 * the database server generates a new and unique identifier for
349
			 * that record. This ID can be used for retrieving, updating and
350
			 * deleting that specific record from the table again.
351
			 *
352
			 * For MySQL:
353
			 *  SELECT LAST_INSERT_ID()
354
			 * For PostgreSQL:
355
			 *  SELECT currval('seq_mcus_id')
356
			 * For SQL Server:
357
			 *  SELECT SCOPE_IDENTITY()
358
			 * For Oracle:
359
			 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
360
			 *
361
			 * There's no way to retrive the new ID by a SQL statements that
362
			 * fits for most database servers as they implement their own
363
			 * specific way.
364
			 *
365
			 * @param string SQL statement for retrieving the last inserted record ID
366
			 * @since 2015.01
367
			 * @category Developer
368
			 * @see mshop/customer/manager/laravel/insert
369
			 * @see mshop/customer/manager/laravel/update
370
			 * @see mshop/customer/manager/laravel/delete
371
			 * @see mshop/customer/manager/laravel/search
372
			 * @see mshop/customer/manager/laravel/count
373
			 */
374
			$path = 'mshop/customer/manager/laravel/newid';
375
			$id = $this->newId( $conn, $path );
376
		}
377
378
		return $this->object()->saveRefs( $item->setId( $id ), $fetch );
379
	}
380
381
382
	/**
383
	 * Returns the full configuration key for the passed last part
384
	 *
385
	 * @param string $name Configuration last part
386
	 * @return string Full configuration key
387
	 */
388
	protected function getConfigKey( string $name, string $default = '' ) : string
389
	{
390
		if( $this->context()->config()->get( 'mshop/customer/manager/laravel/' . $name ) ) {
391
			return 'mshop/customer/manager/laravel/' . $name;
392
		}
393
394
		return parent::getConfigKey( $name, $default );
395
	}
396
397
398
	/**
399
	 * Returns a new manager for customer extensions
400
	 *
401
	 * @param string $manager Name of the sub manager type in lower case
402
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
403
	 * @return mixed Manager for different extensions, e.g stock, tags, locations, etc.
404
	 */
405
	public function getSubManager( string $manager, ?string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
406
	{
407
		return $this->getSubManagerBase( 'customer', $manager, $name ?: 'Laravel' );
408
	}
409
410
411
	/**
412
	 * Returns the name of the used table
413
	 *
414
	 * @return string Table name
415
	 */
416
	protected function table() : string
417
	{
418
		return 'users';
419
	}
420
}
421