Passed
Push — master ( 7332c2...644fc4 )
by Aimeos
04:27
created

Base::clear()   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 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Manager;
12
13
14
/**
15
 * Provides common methods required by most of the manager classes.
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
abstract class Base implements \Aimeos\Macro\Iface
21
{
22
	use \Aimeos\Macro\Macroable;
23
	use Sub\Traits;
24
	use Methods;
25
	use Site;
26
	use DB;
27
28
29
	private \Aimeos\MShop\ContextIface $context;
30
31
32
	/**
33
	 * Initialization of class.
34
	 *
35
	 * @param \Aimeos\MShop\ContextIface $context Context object
36
	 */
37
	public function __construct( \Aimeos\MShop\ContextIface $context )
38
	{
39
		$this->context = $context;
40
41
		$this->setResourceName( $context->config()->get( 'mshop/' . $this->getDomain() . '/manager/resource', 'db-' . $this->getDomain() ) );
42
	}
43
44
45
	/**
46
	 * Removes old entries from the storage.
47
	 *
48
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
49
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
50
	 */
51
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
52
	{
53
		foreach( $this->context()->config()->get( $this->getConfigKey( 'submanagers' ), [] ) as $domain ) {
54
			$this->object()->getSubManager( $domain )->clear( $siteids );
55
		}
56
57
		return $this->clearBase( $siteids, $this->getConfigKey( 'delete' ) );
58
	}
59
60
61
	/**
62
	 * Creates a new empty item instance
63
	 *
64
	 * @param array $values Values the item should be initialized with
65
	 * @return \Aimeos\MShop\Common\Item\Iface New attribute item object
66
	 */
67
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
68
	{
69
		$prefix = $this->getPrefix();
70
		$values[$prefix . 'siteid'] = $values[$prefix . 'siteid'] ?? $this->context()->locale()->getSiteId();
71
72
		return new \Aimeos\MShop\Common\Item\Base( $prefix, $values );
73
	}
74
75
76
	/**
77
	 * Removes multiple items.
78
	 *
79
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
80
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
81
	 */
82
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
83
	{
84
		return $this->deleteItemsBase( $itemIds, $this->getConfigKey( 'delete' ) );
85
	}
86
87
88
	/**
89
	 * Creates a search critera object
90
	 *
91
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
92
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
93
	 * @return \Aimeos\Base\Criteria\Iface New search criteria object
94
	 */
95
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
96
	{
97
		return $this->filterBase( $this->getDomain() );
98
	}
99
100
101
	/**
102
	 * Returns the attributes item specified by its ID.
103
	 *
104
	 * @param string $id Unique ID of the attribute item in the storage
105
	 * @param string[] $ref List of domains to fetch list items and referenced items for
106
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
107
	 * @return \Aimeos\MShop\Common\Item\Iface Returns the attribute item of the given id
108
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
109
	 */
110
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
111
	{
112
		return $this->getItemBase( $this->getPrefix() . 'id', $id, $ref, $default );
113
	}
114
115
116
	/**
117
	 * Returns the available manager types
118
	 *
119
	 * @param bool $withsub Return also the resource type of sub-managers if true
120
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
121
	 */
122
	public function getResourceType( bool $withsub = true ) : array
123
	{
124
		return $this->getResourceTypeBase( $this->getDomain(), $this->getConfigKey( 'submanagers' ), [], $withsub );
125
	}
126
127
128
	/**
129
	 * Returns the attributes that can be used for searching.
130
	 *
131
	 * @param bool $withsub Return also attributes of sub-managers if true
132
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items
133
	 */
134
	public function getSearchAttributes( bool $withsub = true ) : array
135
	{
136
		$prefix = $this->getPrefix();
137
138
		return array_replace( $this->createAttributes( [
139
			'id' => [
140
				'code' => $prefix . 'id',
141
				'label' => 'ID',
142
				'type' => 'int',
143
				'public' => false,
144
			],
145
			'siteid' => [
146
				'code' => $prefix . 'siteid',
147
				'label' => 'Site ID',
148
				'public' => false,
149
			],
150
			'ctime' => [
151
				'code' => $prefix . 'ctime',
152
				'label' => 'Create date/time',
153
				'type' => 'datetime',
154
				'public' => false,
155
			],
156
			'mtime' => [
157
				'code' => $prefix . 'mtime',
158
				'label' => 'Modification date/time',
159
				'type' => 'datetime',
160
				'public' => false,
161
			],
162
			'editor' => [
163
				'code' => $prefix . 'editor',
164
				'label' => 'Editor',
165
				'public' => false,
166
			],
167
		] ), $this->getSaveAttributes() );
168
	}
169
170
171
	/**
172
	 * Returns a new manager for attribute extensions
173
	 *
174
	 * @param string $manager Name of the sub manager type in lower case
175
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
176
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
177
	 */
178
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
179
	{
180
		return $this->getSubManagerBase( $this->getDomain(), $manager, $name );
181
	}
182
183
184
	/**
185
	 * Iterates over all matched items and returns the found ones
186
	 *
187
	 * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor
188
	 * @param string[] $ref List of domains whose items should be fetched too
189
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
190
	 */
191
	public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map
192
	{
193
		if( $cursor->value() === '' ) {
194
			return null;
195
		}
196
197
		if( ( $first = current( $this->getSearchAttributes() ) ) === false ) {
198
			throw new \Aimeos\MShop\Exception( sprintf( 'No search configuration available for "%1$s"', get_class( $this ) ) );
199
		}
200
201
		$filter = $cursor->filter()->add( $first->getCode(), '>', (int) $cursor->value() )->order( $first->getCode() );
202
		$items = $this->search( $filter, $ref );
203
		$cursor->setValue( $items->lastKey() ?: '' );
204
205
		return !$items->isEmpty() ? $items : null;
206
	}
207
208
209
	/**
210
	 * Adds or updates an item object or a list of them.
211
	 *
212
	 * @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
213
	 * @param bool $fetch True if the new ID should be returned in the item
214
	 * @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items
215
	 */
216
	public function save( $items, bool $fetch = true )
217
	{
218
		foreach( map( $items ) as $item )
219
		{
220
			if( method_exists( $this, 'saveItem' ) ) {
221
				$this->saveItem( $item, $fetch );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Base. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

221
				$this->/** @scrutinizer ignore-call */ 
222
           saveItem( $item, $fetch );
Loading history...
222
			} else {
223
				$this->saveBase( $item, $fetch );
224
			}
225
		}
226
227
		return is_array( $items ) ? map( $items ) : $items;
228
	}
229
230
231
	/**
232
	 * Searches for all items matching the given critera.
233
	 *
234
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
235
	 * @param string[] $ref List of domains to fetch list items and referenced items for
236
	 * @param int &$total Number of items that are available in total
237
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
238
	 */
239
	public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map
240
	{
241
		/** mshop/common/manager/search/mysql
242
		 * Retrieves the records matched by the given criteria in the database
243
		 *
244
		 * @see mshop/common/manager/search/ansi
245
		 */
246
247
		/** mshop/common/manager/search/ansi
248
		 * Retrieves the records matched by the given criteria in the database
249
		 *
250
		 * Fetches the records matched by the given criteria from the
251
		 * database. The records must be from one of the sites that are
252
		 * configured via the context item. If the current site is part of
253
		 * a tree of sites, the SELECT statement can retrieve all records
254
		 * from the current site and the complete sub-tree of sites.
255
		 *
256
		 * As the records can normally be limited by criteria from sub-managers,
257
		 * their tables must be joined in the SQL context. This is done by
258
		 * using the "internaldeps" property from the definition of the ID
259
		 * column of the sub-managers. These internal dependencies specify
260
		 * the JOIN between the tables and the used columns for joining. The
261
		 * ":joins" placeholder is then replaced by the JOIN strings from
262
		 * the sub-managers.
263
		 *
264
		 * To limit the records matched, conditions can be added to the given
265
		 * criteria object. It can contain comparisons like column names that
266
		 * must match specific values which can be combined by AND, OR or NOT
267
		 * operators. The resulting string of SQL conditions replaces the
268
		 * ":cond" placeholder before the statement is sent to the database
269
		 * server.
270
		 *
271
		 * If the records that are retrieved should be ordered by one or more
272
		 * columns, the generated string of column / sort direction pairs
273
		 * replaces the ":order" placeholder. Columns of
274
		 * sub-managers can also be used for ordering the result set but then
275
		 * no index can be used.
276
		 *
277
		 * The number of returned records can be limited and can start at any
278
		 * number between the begining and the end of the result set. For that
279
		 * the ":size" and ":start" placeholders are replaced by the
280
		 * corresponding values from the criteria object. The default values
281
		 * are 0 for the start and 100 for the size value.
282
		 *
283
		 * The SQL statement should conform to the ANSI standard to be
284
		 * compatible with most relational database systems. This also
285
		 * includes using double quotes for table and column names.
286
		 *
287
		 * @param string SQL statement for searching items
288
		 * @since 2023.10
289
		 * @category Developer
290
		 * @see mshop/common/manager/insert/ansi
291
		 * @see mshop/common/manager/update/ansi
292
		 * @see mshop/common/manager/newid/ansi
293
		 * @see mshop/common/manager/delete/ansi
294
		 * @see mshop/common/manager/count/ansi
295
		 */
296
		$cfgPathSearch = 'mshop/common/manager/search';
297
298
		/** mshop/common/manager/count/mysql
299
		 * Counts the number of records matched by the given criteria in the database
300
		 *
301
		 * @see mshop/common/manager/count/ansi
302
		 */
303
304
		/** mshop/common/manager/count/ansi
305
		 * Counts the number of records matched by the given criteria in the database
306
		 *
307
		 * Counts all records matched by the given criteria from the
308
		 * database. The records must be from one of the sites that are
309
		 * configured via the context item. If the current site is part of
310
		 * a tree of sites, the statement can count all records from the
311
		 * current site and the complete sub-tree of sites.
312
		 *
313
		 * As the records can normally be limited by criteria from sub-managers,
314
		 * their tables must be joined in the SQL context. This is done by
315
		 * using the "internaldeps" property from the definition of the ID
316
		 * column of the sub-managers. These internal dependencies specify
317
		 * the JOIN between the tables and the used columns for joining. The
318
		 * ":joins" placeholder is then replaced by the JOIN strings from
319
		 * the sub-managers.
320
		 *
321
		 * To limit the records matched, conditions can be added to the given
322
		 * criteria object. It can contain comparisons like column names that
323
		 * must match specific values which can be combined by AND, OR or NOT
324
		 * operators. The resulting string of SQL conditions replaces the
325
		 * ":cond" placeholder before the statement is sent to the database
326
		 * server.
327
		 *
328
		 * Both, the strings for ":joins" and for ":cond" are the same as for
329
		 * the "search" SQL statement.
330
		 *
331
		 * Contrary to the "search" statement, it doesn't return any records
332
		 * but instead the number of records that have been found. As counting
333
		 * thousands of records can be a long running task, the maximum number
334
		 * of counted records is limited for performance reasons.
335
		 *
336
		 * The SQL statement should conform to the ANSI standard to be
337
		 * compatible with most relational database systems. This also
338
		 * includes using double quotes for table and column names.
339
		 *
340
		 * @param string SQL statement for counting items
341
		 * @since 2023.10
342
		 * @category Developer
343
		 * @see mshop/common/manager/insert/ansi
344
		 * @see mshop/common/manager/update/ansi
345
		 * @see mshop/common/manager/newid/ansi
346
		 * @see mshop/common/manager/delete/ansi
347
		 * @see mshop/common/manager/search/ansi
348
		 */
349
		$cfgPathCount = 'mshop/common/manager/count';
350
351
		$items = [];
352
		$prefix = $this->getPrefix();
353
		$level = $this->getSiteMode();
354
		$required = [$this->getSearchKey()];
355
		$conn = $this->context()->db( $this->getResourceName() );
356
357
		$results = $this->searchItemsBase( $conn, $filter, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
358
359
		while( $row = $results->fetch() )
360
		{
361
			if( $item = $this->applyFilter( $this->create( $row ) ) ) {
362
				$items[$row[$prefix . 'id']] = $item;
363
			}
364
		}
365
366
		return map( $items );
367
	}
368
369
370
	/**
371
	 * Starts a database transaction on the connection identified by the given name
372
	 *
373
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
374
	 */
375
	public function begin() : \Aimeos\MShop\Common\Manager\Iface
376
	{
377
		$this->context->db( $this->getResourceName() )->begin();
378
		return $this;
379
	}
380
381
382
	/**
383
	 * Commits the running database transaction on the connection identified by the given name
384
	 *
385
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
386
	 */
387
	public function commit() : \Aimeos\MShop\Common\Manager\Iface
388
	{
389
		$this->context->db( $this->getResourceName() )->commit();
390
		return $this;
391
	}
392
393
394
	/**
395
	 * Rolls back the running database transaction on the connection identified by the given name
396
	 *
397
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
398
	 */
399
	public function rollback() : \Aimeos\MShop\Common\Manager\Iface
400
	{
401
		$this->context->db( $this->getResourceName() )->rollback();
402
		return $this;
403
	}
404
405
406
	/**
407
	 * Returns the context object.
408
	 *
409
	 * @return \Aimeos\MShop\ContextIface Context object
410
	 */
411
	protected function context() : \Aimeos\MShop\ContextIface
412
	{
413
		return $this->context;
414
	}
415
416
417
	/**
418
	 * Returns the prefix for the item properties and search keys.
419
	 *
420
	 * @return string Prefix for the item properties and search keys
421
	 */
422
	protected function getPrefix() : string
423
	{
424
		return '';
425
	}
426
427
428
	/**
429
	 * Returns the site mode constant for inheritance/aggregation
430
	 *
431
	 * @return int Site mode constant (default: SITE_ALL for inheritance and aggregation)
432
	 */
433
	protected function getSiteMode() : int
434
	{
435
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
436
		return $this->context()->config()->get( $this->getConfigKey( 'sitemode' ), $level );
437
	}
438
}
439