Passed
Push — master ( 70aa88...10de15 )
by Aimeos
03:08
created

Typo3   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 489
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 118
c 1
b 0
f 0
dl 0
loc 489
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A clear() 0 9 2
A createItemBase() 0 13 3
A getSearchAttributes() 0 5 1
A getSubManager() 0 3 2
B saveItem() 0 167 8
A search() 0 121 3
A delete() 0 35 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager\Group;
12
13
14
/**
15
 * TYPO3 implementation of the customer group class
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class Typo3
21
	extends \Aimeos\MShop\Customer\Manager\Group\Standard
22
{
23
	private $searchConfig = array(
24
		'customer.group.id' => array(
25
			'code' => 'customer.group.id',
26
			'internalcode' => 't3feg."uid"',
27
			'label' => 'Customer group ID',
28
			'type' => 'integer',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
30
		),
31
		'customer.group.code' => array(
32
			'code' => 'customer.group.code',
33
			'internalcode' => 't3feg."title"',
34
			'label' => 'Customer group code',
35
			'type' => 'string',
36
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
37
		),
38
		'customer.group.label' => array(
39
			'code' => 'customer.group.label',
40
			'internalcode' => 't3feg."description"',
41
			'label' => 'Customer group label',
42
			'type' => 'string',
43
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
44
		),
45
		'customer.group.ctime'=> array(
46
			'code' => 'customer.group.ctime',
47
			'internalcode' => 't3feg."crdate"',
48
			'label' => 'Customer group creation time',
49
			'type' => 'datetime',
50
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
51
		),
52
		'customer.group.mtime'=> array(
53
			'code' => 'customer.group.mtime',
54
			'internalcode' => 't3feg."tstamp"',
55
			'label' => 'Customer group modification time',
56
			'type' => 'datetime',
57
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
58
		),
59
		'customer.group.editor'=> array(
60
			'code' => 'customer.group.editor',
61
			'internalcode' => '1',
62
			'label' => 'Customer group editor',
63
			'type' => 'string',
64
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
65
		),
66
	);
67
68
	private $plugins = [];
69
	private $reverse = [];
70
	private $pid;
71
72
73
	/**
74
	 * Initializes the customer group manager object
75
	 *
76
	 * @param \Aimeos\MShop\Context\Iface $context Context object with required objects
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Context\Iface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
	 */
78
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
79
	{
80
		parent::__construct( $context );
81
82
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Datetime();
83
		$this->plugins['customer.ctime'] = $this->reverse['crdate'] = $plugin;
84
		$this->plugins['customer.mtime'] = $this->reverse['tstamp'] = $plugin;
85
86
		$this->pid = $context->getConfig()->get( 'mshop/customer/manager/typo3/pid-default', 0 );
87
		$this->pid = $context->getConfig()->get( 'mshop/customer/manager/group/typo3/pid-default', $this->pid );
88
	}
89
90
91
	/**
92
	 * Removes old entries from the database
93
	 *
94
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
95
	 * @return \Aimeos\MShop\Common\Manager\Iface Same object for fluent interface
96
	 */
97
	public function clear( array $siteids ) : \Aimeos\MShop\Common\Manager\Iface
98
	{
99
		$path = 'mshop/customer/manager/group/submanagers';
100
101
		foreach( $this->getContext()->getConfig()->get( $path, [] ) as $domain ) {
102
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
103
		}
104
105
		return $this;
106
	}
107
108
109
	/**
110
	 * Removes multiple items.
111
	 *
112
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
113
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
114
	 */
115
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
116
	{
117
		/** mshop/customer/manager/group/typo3/delete/mysql
118
		 * Deletes the items matched by the given IDs from the database
119
		 *
120
		 * @see mshop/customer/manager/group/typo3/delete/ansi
121
		 */
122
123
		/** mshop/customer/manager/group/typo3/delete/ansi
124
		 * Deletes the items matched by the given IDs from the database
125
		 *
126
		 * Removes the records specified by the given IDs from the customer group
127
		 * database. The records must be from the site that is configured via the
128
		 * context item.
129
		 *
130
		 * The ":cond" placeholder is replaced by the name of the ID column and
131
		 * the given ID or list of IDs while the site ID is bound to the question
132
		 * mark.
133
		 *
134
		 * The SQL statement should conform to the ANSI standard to be
135
		 * compatible with most relational database systems. This also
136
		 * includes using double quotes for table and column names.
137
		 *
138
		 * @param string SQL statement for deleting items
139
		 * @since 2015.08
140
		 * @category Developer
141
		 * @see mshop/customer/manager/group/typo3/insert/ansi
142
		 * @see mshop/customer/manager/group/typo3/update/ansi
143
		 * @see mshop/customer/manager/group/typo3/newid/ansi
144
		 * @see mshop/customer/manager/group/typo3/search/ansi
145
		 * @see mshop/customer/manager/group/typo3/count/ansi
146
		 */
147
		$path = 'mshop/customer/manager/group/typo3/delete';
148
149
		return $this->deleteItemsBase( $itemIds, $path, false, 'uid' );
150
	}
151
152
153
	/**
154
	 * Returns the attributes that can be used for searching
155
	 *
156
	 * @param bool $withsub Return attributes of sub-managers too if true
157
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
158
	 */
159
	public function getSearchAttributes( bool $withsub = true ) : array
160
	{
161
		$path = 'mshop/customer/manager/group/submanagers';
162
163
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
164
	}
165
166
167
	/**
168
	 * Returns a new manager for customer group extensions
169
	 *
170
	 * @param string $manager Name of the sub manager type in lower case
171
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
172
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions
173
	 */
174
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
175
	{
176
		return $this->getSubManagerBase( 'customer/group', $manager, ( $name === null ? 'Typo3' : $name ) );
177
	}
178
179
180
	/**
181
	 * Inserts a new or updates an existing customer group item
182
	 *
183
	 * @param \Aimeos\MShop\Customer\Item\Group\Iface $item Customer group item
184
	 * @param boolean $fetch True if the new ID should be returned in the item
185
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface $item Updated item including the generated ID
186
	 */
187
	public function saveItem( \Aimeos\MShop\Customer\Item\Group\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Group\Iface
188
	{
189
		if( !$item->isModified() ) {
190
			return $item;
191
		}
192
193
		$context = $this->getContext();
194
195
		$dbm = $context->getDatabaseManager();
196
		$dbname = $this->getResourceName();
197
		$conn = $dbm->acquire( $dbname );
198
199
		try
200
		{
201
			$id = $item->getId();
202
			$columns = $this->getObject()->getSaveAttributes();
203
204
			if( $id === null )
205
			{
206
				/** mshop/customer/manager/group/typo3/insert/mysql
207
				 * Inserts a new customer group record into the database table
208
				 *
209
				 * @see mshop/customer/manager/group/typo3/insert/ansi
210
				 */
211
212
				/** mshop/customer/manager/group/typo3/insert/ansi
213
				 * Inserts a new customer group record into the database table
214
				 *
215
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
216
				 * the database and the newly created ID retrieved afterwards
217
				 * using the "newid" SQL statement.
218
				 *
219
				 * The SQL statement must be a string suitable for being used as
220
				 * prepared statement. It must include question marks for binding
221
				 * the values from the customer group item to the statement before
222
				 * they are sent to the database server. The number of question
223
				 * marks must be the same as the number of columns listed in the
224
				 * INSERT statement. The order of the columns must correspond to
225
				 * the order in the saveItems() method, so the correct values are
226
				 * bound to the columns.
227
				 *
228
				 * The SQL statement should conform to the ANSI standard to be
229
				 * compatible with most relational database systems. This also
230
				 * includes using double quotes for table and column names.
231
				 *
232
				 * @param string SQL statement for inserting records
233
				 * @since 2015.08
234
				 * @category Developer
235
				 * @see mshop/customer/manager/group/typo3/update/ansi
236
				 * @see mshop/customer/manager/group/typo3/newid/ansi
237
				 * @see mshop/customer/manager/group/typo3/delete/ansi
238
				 * @see mshop/customer/manager/group/typo3/search/ansi
239
				 * @see mshop/customer/manager/group/typo3/count/ansi
240
				 */
241
				$path = 'mshop/customer/manager/group/typo3/insert';
242
				$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

242
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
243
			}
244
			else
245
			{
246
				/** mshop/customer/manager/group/typo3/update/mysql
247
				 * Updates an existing customer group record in the database
248
				 *
249
				 * @see mshop/customer/manager/group/typo3/update/ansi
250
				 */
251
252
				/** mshop/customer/manager/group/typo3/update/ansi
253
				 * Updates an existing customer group record in the database
254
				 *
255
				 * Items which already have an ID (i.e. the ID is not NULL) will
256
				 * be updated in the database.
257
				 *
258
				 * The SQL statement must be a string suitable for being used as
259
				 * prepared statement. It must include question marks for binding
260
				 * the values from the customer group item to the statement before
261
				 * they are sent to the database server. The order of the columns
262
				 * must correspond to the order in the saveItems() method, so the
263
				 * correct values are bound to the columns.
264
				 *
265
				 * The SQL statement should conform to the ANSI standard to be
266
				 * compatible with most relational database systems. This also
267
				 * includes using double quotes for table and column names.
268
				 *
269
				 * @param string SQL statement for updating records
270
				 * @since 2015.08
271
				 * @category Developer
272
				 * @see mshop/customer/manager/group/typo3/insert/ansi
273
				 * @see mshop/customer/manager/group/typo3/newid/ansi
274
				 * @see mshop/customer/manager/group/typo3/delete/ansi
275
				 * @see mshop/customer/manager/group/typo3/search/ansi
276
				 * @see mshop/customer/manager/group/typo3/count/ansi
277
				 */
278
				$path = 'mshop/customer/manager/group/typo3/update';
279
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
280
			}
281
282
			$idx = 1;
283
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
284
285
			foreach( $columns as $name => $entry ) {
286
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
287
			}
288
289
			$stmt->bind( $idx++, $this->pid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
290
			$stmt->bind( $idx++, $item->getCode() );
291
			$stmt->bind( $idx++, $item->getLabel() );
292
			$stmt->bind( $idx++, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // mtime
293
294
			if( $id !== null ) {
295
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
296
				$item->setId( $id );
297
			} else {
298
				$stmt->bind( $idx++, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // ctime
299
			}
300
301
			$stmt->execute()->finish();
302
303
			if( $id === null && $fetch === true )
304
			{
305
				/** mshop/customer/manager/group/typo3/newid/mysql
306
				 * Retrieves the ID generated by the database when inserting a new record
307
				 *
308
				 * @see mshop/customer/manager/group/typo3/newid/ansi
309
				 */
310
311
				/** mshop/customer/manager/group/typo3/newid/ansi
312
				 * Retrieves the ID generated by the database when inserting a new record
313
				 *
314
				 * As soon as a new record is inserted into the database table,
315
				 * the database server generates a new and unique identifier for
316
				 * that record. This ID can be used for retrieving, updating and
317
				 * deleting that specific record from the table again.
318
				 *
319
				 * For MySQL:
320
				 *  SELECT LAST_INSERT_ID()
321
				 * For PostgreSQL:
322
				 *  SELECT currval('seq_mcus_id')
323
				 * For SQL Server:
324
				 *  SELECT SCOPE_IDENTITY()
325
				 * For Oracle:
326
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
327
				 *
328
				 * There's no way to retrive the new ID by a SQL statements that
329
				 * fits for most database servers as they implement their own
330
				 * specific way.
331
				 *
332
				 * @param string SQL statement for retrieving the last inserted record ID
333
				 * @since 2015.08
334
				 * @category Developer
335
				 * @see mshop/customer/manager/group/typo3/insert/ansi
336
				 * @see mshop/customer/manager/group/typo3/update/ansi
337
				 * @see mshop/customer/manager/group/typo3/delete/ansi
338
				 * @see mshop/customer/manager/group/typo3/search/ansi
339
				 * @see mshop/customer/manager/group/typo3/count/ansi
340
				 */
341
				$path = 'mshop/customer/manager/group/typo3/newid';
342
				$item->setId( $this->newId( $conn, $path ) );
343
			}
344
345
			$dbm->release( $conn, $dbname );
346
		}
347
		catch( \Exception $e )
348
		{
349
			$dbm->release( $conn, $dbname );
350
			throw $e;
351
		}
352
353
		return $item;
354
	}
355
356
357
	/**
358
	 * Returns the item objects matched by the given search criteria.
359
	 *
360
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
361
	 * @param array $ref List of domain items that should be fetched too
362
	 * @param int|null &$total Number of items that are available in total
363
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Customer\Item\Group\Iface
364
	 * @throws \Aimeos\MShop\Exception If retrieving items failed
365
	 */
366
	public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
367
	{
368
		$map = [];
369
		$context = $this->getContext();
370
371
		$dbm = $context->getDatabaseManager();
372
		$dbname = $this->getResourceName();
373
		$conn = $dbm->acquire( $dbname );
374
375
		try
376
		{
377
			$required = array( 'customer.group' );
378
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
379
380
			/** mshop/customer/manager/group/typo3/search
381
			 * Retrieves the records matched by the given criteria in the database
382
			 *
383
			 * Fetches the records matched by the given criteria from the customer
384
			 * database. The records must be from one of the sites that are
385
			 * configured via the context item. If the current site is part of
386
			 * a tree of sites, the SELECT statement can retrieve all records
387
			 * from the current site and the complete sub-tree of sites.
388
			 *
389
			 * As the records can normally be limited by criteria from sub-managers,
390
			 * their tables must be joined in the SQL context. This is done by
391
			 * using the "internaldeps" property from the definition of the ID
392
			 * column of the sub-managers. These internal dependencies specify
393
			 * the JOIN between the tables and the used columns for joining. The
394
			 * ":joins" placeholder is then replaced by the JOIN strings from
395
			 * the sub-managers.
396
			 *
397
			 * To limit the records matched, conditions can be added to the given
398
			 * criteria object. It can contain comparisons like column names that
399
			 * must match specific values which can be combined by AND, OR or NOT
400
			 * operators. The resulting string of SQL conditions replaces the
401
			 * ":cond" placeholder before the statement is sent to the database
402
			 * server.
403
			 *
404
			 * If the records that are retrieved should be ordered by one or more
405
			 * columns, the generated string of column / sort direction pairs
406
			 * replaces the ":order" placeholder. In case no ordering is required,
407
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
408
			 * markers is removed to speed up retrieving the records. Columns of
409
			 * sub-managers can also be used for ordering the result set but then
410
			 * no index can be used.
411
			 *
412
			 * The number of returned records can be limited and can start at any
413
			 * number between the begining and the end of the result set. For that
414
			 * the ":size" and ":start" placeholders are replaced by the
415
			 * corresponding values from the criteria object. The default values
416
			 * are 0 for the start and 100 for the size value.
417
			 *
418
			 * The SQL statement should conform to the ANSI standard to be
419
			 * compatible with most relational database systems. This also
420
			 * includes using double quotes for table and column names.
421
			 *
422
			 * @param string SQL statement for searching items
423
			 * @since 2015.08
424
			 * @category Developer
425
			 * @see mshop/customer/manager/group/typo3/count
426
			 */
427
			$cfgPathSearch = 'mshop/customer/manager/group/typo3/search';
428
429
			/** mshop/customer/manager/group/typo3/count
430
			 * Counts the number of records matched by the given criteria in the database
431
			 *
432
			 * Counts all records matched by the given criteria from the customer
433
			 * database. The records must be from one of the sites that are
434
			 * configured via the context item. If the current site is part of
435
			 * a tree of sites, the statement can count all records from the
436
			 * current site and the complete sub-tree of sites.
437
			 *
438
			 * As the records can normally be limited by criteria from sub-managers,
439
			 * their tables must be joined in the SQL context. This is done by
440
			 * using the "internaldeps" property from the definition of the ID
441
			 * column of the sub-managers. These internal dependencies specify
442
			 * the JOIN between the tables and the used columns for joining. The
443
			 * ":joins" placeholder is then replaced by the JOIN strings from
444
			 * the sub-managers.
445
			 *
446
			 * To limit the records matched, conditions can be added to the given
447
			 * criteria object. It can contain comparisons like column names that
448
			 * must match specific values which can be combined by AND, OR or NOT
449
			 * operators. The resulting string of SQL conditions replaces the
450
			 * ":cond" placeholder before the statement is sent to the database
451
			 * server.
452
			 *
453
			 * Both, the strings for ":joins" and for ":cond" are the same as for
454
			 * the "search" SQL statement.
455
			 *
456
			 * Contrary to the "search" statement, it doesn't return any records
457
			 * but instead the number of records that have been found. As counting
458
			 * thousands of records can be a long running task, the maximum number
459
			 * of counted records is limited for performance reasons.
460
			 *
461
			 * The SQL statement should conform to the ANSI standard to be
462
			 * compatible with most relational database systems. This also
463
			 * includes using double quotes for table and column names.
464
			 *
465
			 * @param string SQL statement for counting items
466
			 * @since 2015.08
467
			 * @category Developer
468
			 * @see mshop/customer/manager/group/typo3/search
469
			 */
470
			$cfgPathCount = 'mshop/customer/manager/group/typo3/count';
471
472
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
473
474
			while( ( $row = $results->fetch() ) !== null ) {
475
				$map[(string) $row['customer.group.id']] = $this->createItemBase( $row );
476
			}
477
478
			$dbm->release( $conn, $dbname );
479
		}
480
		catch( \Exception $e )
481
		{
482
			$dbm->release( $conn, $dbname );
483
			throw $e;
484
		}
485
486
		return map( $map );
487
	}
488
489
490
	/**
491
	 * Creates a new customer item.
492
	 *
493
	 * @param array $values List of attributes for customer item
494
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface New customer item
495
	 */
496
	protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Customer\Item\Group\Iface
497
	{
498
		$values['customer.group.siteid'] = $this->getContext()->getLocale()->getSiteId();
499
500
		if( array_key_exists( 'tstamp', $values ) ) {
501
			$values['customer.group.mtime'] = $this->reverse['tstamp']->reverse( $values['tstamp'] );
502
		}
503
504
		if( array_key_exists( 'crdate', $values ) ) {
505
			$values['customer.group.ctime'] = $this->reverse['crdate']->reverse( $values['crdate'] );
506
		}
507
508
		return new \Aimeos\MShop\Customer\Item\Group\Standard( $values );
509
	}
510
}
511