Passed
Push — master ( 8ec6bb...4353de )
by Aimeos
20:24 queued 06:36
created

Laravel::getConfigKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
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), 2015-2024
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[] $itemIds 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( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
115
	{
116
		$path = 'mshop/customer/manager/laravel/delete';
117
		return $this->deleteItemsBase( $itemIds, $path )->deleteRefItems( $itemIds );
118
	}
119
120
121
	/**
122
	 * Returns the list attributes that can be used for searching.
123
	 *
124
	 * @param bool $withsub Return also attributes of sub-managers if true
125
	 * @return array List of attribute items implementing \Aimeos\Base\Criteria\Attribute\Iface
126
	 */
127
	public function getSearchAttributes( bool $withsub = true ) : array
128
	{
129
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
130
		$level = $this->context()->config()->get( 'mshop/customer/manager/sitemode', $level );
131
132
		return array_replace( parent::getSearchAttributes( $withsub ), $this->createAttributes( [
133
			'customer.code' => [
134
				'label' => 'Username',
135
				'internalcode' => 'email',
136
			],
137
			'customer.label' => [
138
				'label' => 'Label',
139
				'internalcode' => 'name',
140
			],
141
			'customer.dateverified' => [
142
				'label' => 'Customer verification date',
143
				'internalcode' => 'mcus."email_verified_at"',
144
				'type' => 'date',
145
			],
146
			'customer.ctime' => [
147
				'label' => 'Customer creation time',
148
				'internalcode' => 'mcus."created_at"',
149
				'type' => 'datetime',
150
			],
151
			'customer.mtime' => [
152
				'label' => 'Customer modification time',
153
				'internalcode' => 'mcus."updated_at"',
154
				'type' => 'datetime',
155
			],
156
			'customer:has' => [
157
				'code' => 'customer:has()',
158
				'internalcode' => ':site AND :key AND mcusli."id"',
159
				'internaldeps' => ['LEFT JOIN "users_list" AS mcusli ON ( mcusli."parentid" = mcus."id" )'],
160
				'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
161
				'type' => 'null',
162
				'public' => false,
163
				'function' => function( &$source, array $params ) use ( $level ) {
164
					$keys = [];
165
166
					foreach( (array) ( $params[1] ?? '' ) as $type ) {
167
						foreach( (array) ( $params[2] ?? '' ) as $id ) {
168
							$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
169
						}
170
					}
171
172
					$sitestr = $this->siteString( 'mcusli."siteid"', $level );
173
					$keystr = $this->toExpression( 'mcusli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
174
					$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
175
176
					return $params;
177
				}
178
			],
179
			'customer:prop' => [
180
				'code' => 'customer:prop()',
181
				'internalcode' => ':site AND :key AND mcuspr."id"',
182
				'internaldeps' => ['LEFT JOIN "users_property" AS mcuspr ON ( mcuspr."parentid" = mcus."id" )'],
183
				'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
184
				'type' => 'null',
185
				'public' => false,
186
				'function' => function( &$source, array $params ) use ( $level ) {
187
					$keys = [];
188
					$langs = array_key_exists( 1, $params ) ? ( $params[1] ?? 'null' ) : '';
189
190
					foreach( (array) $langs as $lang ) {
191
						foreach( (array) ( $params[2] ?? '' ) as $val ) {
192
							$keys[] = substr( $params[0] . '|' . ( $lang === null ? 'null|' : ( $lang ? $lang . '|' : '' ) ) . $val, 0, 255 );
193
						}
194
					}
195
196
					$sitestr = $this->siteString( 'mcuspr."siteid"', $level );
197
					$keystr = $this->toExpression( 'mcuspr."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
198
					$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
199
200
					return $params;
201
				}
202
			],
203
		] ) );
204
	}
205
206
207
	/**
208
	 * Saves a customer item object.
209
	 *
210
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
211
	 * @param boolean $fetch True if the new ID should be returned in the item
212
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
213
	 */
214
	protected function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface
215
	{
216
		$item = $this->addGroups( $item );
217
218
		if( !$item->isModified() ) {
219
			return $this->saveDeps( $item, $fetch );
220
		}
221
222
		$context = $this->context();
223
		$conn = $context->db( $this->getResourceName() );
224
225
		$id = $item->getId();
226
		$billingAddress = $item->getPaymentAddress();
227
		$columns = $this->object()->getSaveAttributes();
228
229
		if( $id === null )
230
		{
231
			/** mshop/customer/manager/laravel/insert
232
			 * Inserts a new customer record into the database table
233
			 *
234
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
235
			 * the database and the newly created ID retrieved afterwards
236
			 * using the "newid" SQL statement.
237
			 *
238
			 * The SQL statement must be a string suitable for being used as
239
			 * prepared statement. It must include question marks for binding
240
			 * the values from the customer item to the statement before they are
241
			 * sent to the database server. The number of question marks must
242
			 * be the same as the number of columns listed in the INSERT
243
			 * statement. The order of the columns must correspond to the
244
			 * order in the save() method, so the correct values are
245
			 * bound to the columns.
246
			 *
247
			 * The SQL statement should conform to the ANSI standard to be
248
			 * compatible with most relational database systems. This also
249
			 * includes using double quotes for table and column names.
250
			 *
251
			 * @param string SQL statement for inserting records
252
			 * @since 2015.01
253
			 * @category Developer
254
			 * @see mshop/customer/manager/laravel/update
255
			 * @see mshop/customer/manager/laravel/newid
256
			 * @see mshop/customer/manager/laravel/delete
257
			 * @see mshop/customer/manager/laravel/search
258
			 * @see mshop/customer/manager/laravel/count
259
			 */
260
			$path = 'mshop/customer/manager/laravel/insert';
261
			$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

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