Passed
Push — master ( 1defac...346e9e )
by Aimeos
43:18 queued 29:48
created

Typo3::saveItem()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 154
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 154
rs 8.5066
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, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 * @package MShop
7
 * @subpackage Group
8
 */
9
10
11
namespace Aimeos\MShop\Group\Manager;
12
13
14
/**
15
 * TYPO3 implementation of the group class
16
 *
17
 * @package MShop
18
 * @subpackage Group
19
 */
20
class Typo3
21
	extends \Aimeos\MShop\Group\Manager\Standard
22
{
23
	private array $searchConfig = array(
24
		'group.id' => array(
25
			'code' => 'group.id',
26
			'internalcode' => 'mgro."uid"',
27
			'label' => 'Group ID',
28
			'type' => 'int',
29
		),
30
		'group.code' => array(
31
			'code' => 'group.code',
32
			'internalcode' => 'mgro."title"',
33
			'label' => 'Group code',
34
			'type' => 'string',
35
		),
36
		'group.label' => array(
37
			'code' => 'group.label',
38
			'internalcode' => 'mgro."description"',
39
			'label' => 'Group label',
40
			'type' => 'string',
41
		),
42
		'group.ctime'=> array(
43
			'code' => 'group.ctime',
44
			'internalcode' => 'mgro."crdate"',
45
			'label' => 'Group creation time',
46
			'type' => 'datetime',
47
		),
48
		'group.mtime'=> array(
49
			'code' => 'group.mtime',
50
			'internalcode' => 'mgro."tstamp"',
51
			'label' => 'Group modification time',
52
			'type' => 'datetime',
53
		),
54
		'group.editor'=> array(
55
			'code' => 'group.editor',
56
			'internalcode' => '1',
57
			'label' => 'Group editor',
58
			'type' => 'string',
59
		),
60
	);
61
62
	private array $plugins = [];
63
	private array $reverse = [];
64
	private int $pid;
65
66
67
	/**
68
	 * Initializes the group manager object
69
	 *
70
	 * @param \Aimeos\MShop\ContextIface $context Context object with required objects
71
	 */
72
	public function __construct( \Aimeos\MShop\ContextIface $context )
73
	{
74
		parent::__construct( $context );
75
76
		$plugin = new \Aimeos\Base\Criteria\Plugin\T3Datetime();
77
		$this->plugins['ctime'] = $this->reverse['crdate'] = $plugin;
78
		$this->plugins['mtime'] = $this->reverse['tstamp'] = $plugin;
79
80
		/** mshop/manager/typo3/pid-default
81
		 * Page ID the group records are assigned to
82
		 *
83
		 * In TYPO3, you can assign fe_group records to different sysfolders based
84
		 * on their page ID. These sysfolders can be use for user authorization and
85
		 * therefore, you need to assign the correct page ID to groups
86
		 * created or modified by the Aimeos admin backend.
87
		 *
88
		 * @param int TYPO3 page ID
89
		 * @since 2018.10
90
		 * @see mshop/customer/manager/typo3/pid-default
91
		 */
92
		$this->pid = $context->config()->get( 'mshop/customer/manager/typo3/pid-default', 0 );
93
		$this->pid = $context->config()->get( 'mshop/group/manager/typo3/pid-default', $this->pid );
94
	}
95
96
97
	/**
98
	 * Removes old entries from the database
99
	 *
100
	 * @param integer[] $siteids List of IDs for sites whose entries should be deleted
101
	 * @return \Aimeos\MShop\Common\Manager\Iface Same object for fluent interface
102
	 */
103
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
104
	{
105
		$path = 'mshop/group/manager/submanagers';
106
107
		foreach( $this->context()->config()->get( $path, [] ) as $domain ) {
108
			$this->object()->getSubManager( $domain )->clear( $siteids );
109
		}
110
111
		return $this;
112
	}
113
114
115
	/**
116
	 * Removes multiple items.
117
	 *
118
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
119
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
120
	 */
121
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
122
	{
123
		/** mshop/group/manager/typo3/delete/mysql
124
		 * Deletes the items matched by the given IDs from the database
125
		 *
126
		 * @see mshop/group/manager/typo3/delete/ansi
127
		 */
128
129
		/** mshop/group/manager/typo3/delete/ansi
130
		 * Deletes the items matched by the given IDs from the database
131
		 *
132
		 * Removes the records specified by the given IDs from the group
133
		 * database. The records must be from the site that is configured via the
134
		 * context item.
135
		 *
136
		 * The ":cond" placeholder is replaced by the name of the ID column and
137
		 * the given ID or list of IDs while the site ID is bound to the question
138
		 * mark.
139
		 *
140
		 * The SQL statement should conform to the ANSI standard to be
141
		 * compatible with most relational database systems. This also
142
		 * includes using double quotes for table and column names.
143
		 *
144
		 * @param string SQL statement for deleting items
145
		 * @since 2015.08
146
		 * @category Developer
147
		 * @see mshop/group/manager/typo3/insert/ansi
148
		 * @see mshop/group/manager/typo3/update/ansi
149
		 * @see mshop/group/manager/typo3/newid/ansi
150
		 * @see mshop/group/manager/typo3/search/ansi
151
		 * @see mshop/group/manager/typo3/count/ansi
152
		 */
153
		$path = 'mshop/group/manager/typo3/delete';
154
155
		return $this->deleteItemsBase( $itemIds, $path, false, 'uid' );
156
	}
157
158
159
	/**
160
	 * Returns the attributes that can be used for searching
161
	 *
162
	 * @param bool $withsub Return attributes of sub-managers too if true
163
	 * @return array List of attribute items implementing \Aimeos\Base\Criteria\Attribute\Iface
164
	 */
165
	public function getSearchAttributes( bool $withsub = true ) : array
166
	{
167
		$path = 'mshop/group/manager/submanagers';
168
169
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
170
	}
171
172
173
	/**
174
	 * Returns a new manager for group extensions
175
	 *
176
	 * @param string $manager Name of the sub manager type in lower case
177
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
178
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions
179
	 */
180
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
181
	{
182
		return $this->getSubManagerBase( 'group/group', $manager, ( $name === null ? 'Typo3' : $name ) );
183
	}
184
185
186
	/**
187
	 * Inserts a new or updates an existing group item
188
	 *
189
	 * @param \Aimeos\MShop\Group\Item\Iface $item Group item
190
	 * @param boolean $fetch True if the new ID should be returned in the item
191
	 * @return \Aimeos\MShop\Group\Item\Iface $item Updated item including the generated ID
192
	 */
193
	protected function saveItem( \Aimeos\MShop\Group\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Group\Item\Iface
194
	{
195
		if( !$item->isModified() ) {
196
			return $item;
197
		}
198
199
		$context = $this->context();
200
		$conn = $context->db( $this->getResourceName() );
201
202
			$id = $item->getId();
203
			$columns = $this->object()->getSaveAttributes();
204
205
			if( $id === null )
206
			{
207
				/** mshop/group/manager/typo3/insert/mysql
208
				 * Inserts a new group record into the database table
209
				 *
210
				 * @see mshop/group/manager/typo3/insert/ansi
211
				 */
212
213
				/** mshop/group/manager/typo3/insert/ansi
214
				 * Inserts a new group record into the database table
215
				 *
216
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
217
				 * the database and the newly created ID retrieved afterwards
218
				 * using the "newid" SQL statement.
219
				 *
220
				 * The SQL statement must be a string suitable for being used as
221
				 * prepared statement. It must include question marks for binding
222
				 * the values from the group item to the statement before
223
				 * they are sent to the database server. The number of question
224
				 * marks must be the same as the number of columns listed in the
225
				 * INSERT statement. The order of the columns must correspond to
226
				 * the order in the save() method, so the correct values are
227
				 * bound to the columns.
228
				 *
229
				 * The SQL statement should conform to the ANSI standard to be
230
				 * compatible with most relational database systems. This also
231
				 * includes using double quotes for table and column names.
232
				 *
233
				 * @param string SQL statement for inserting records
234
				 * @since 2015.08
235
				 * @category Developer
236
				 * @see mshop/group/manager/typo3/update/ansi
237
				 * @see mshop/group/manager/typo3/newid/ansi
238
				 * @see mshop/group/manager/typo3/delete/ansi
239
				 * @see mshop/group/manager/typo3/search/ansi
240
				 * @see mshop/group/manager/typo3/count/ansi
241
				 */
242
				$path = 'mshop/group/manager/typo3/insert';
243
				$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

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