Passed
Push — master ( 994c79...baa5ed )
by Aimeos
05:05
created

Base::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
		$alias = $this->getAlias();
137
		$prefix = $this->getPrefix();
138
139
		return array_replace( $this->createAttributes( [
140
			'id' => [
141
				'code' => $prefix . 'id',
142
				'internalcode' => $alias . '."id"',
143
				'label' => 'ID',
144
				'type' => 'int',
145
				'public' => false,
146
			],
147
			'siteid' => [
148
				'code' => $prefix . 'siteid',
149
				'internalcode' => $alias . '."siteid"',
150
				'label' => 'Site ID',
151
				'public' => false,
152
			],
153
			'ctime' => [
154
				'code' => $prefix . 'ctime',
155
				'internalcode' => $alias . '."ctime"',
156
				'label' => 'Create date/time',
157
				'type' => 'datetime',
158
				'public' => false,
159
			],
160
			'mtime' => [
161
				'code' => $prefix . 'mtime',
162
				'internalcode' => $alias . '."mtime"',
163
				'label' => 'Modification date/time',
164
				'type' => 'datetime',
165
				'public' => false,
166
			],
167
			'editor' => [
168
				'code' => $prefix . 'editor',
169
				'internalcode' => $alias . '."editor"',
170
				'label' => 'Editor',
171
				'public' => false,
172
			],
173
		] ), $this->getSaveAttributes() );
174
	}
175
176
177
	/**
178
	 * Returns a new manager for attribute extensions
179
	 *
180
	 * @param string $manager Name of the sub manager type in lower case
181
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
182
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
183
	 */
184
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
185
	{
186
		return $this->getSubManagerBase( $this->getDomain(), $manager, $name );
187
	}
188
189
190
	/**
191
	 * Iterates over all matched items and returns the found ones
192
	 *
193
	 * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor
194
	 * @param string[] $ref List of domains whose items should be fetched too
195
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
196
	 */
197
	public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map
198
	{
199
		if( $cursor->value() === '' ) {
200
			return null;
201
		}
202
203
		if( ( $first = current( $this->getSearchAttributes() ) ) === false ) {
204
			throw new \Aimeos\MShop\Exception( sprintf( 'No search configuration available for "%1$s"', get_class( $this ) ) );
205
		}
206
207
		$filter = $cursor->filter()->add( $first->getCode(), '>', (int) $cursor->value() )->order( $first->getCode() );
208
		$items = $this->search( $filter, $ref );
209
		$cursor->setValue( $items->lastKey() ?: '' );
210
211
		return !$items->isEmpty() ? $items : null;
212
	}
213
214
215
	/**
216
	 * Adds or updates an item object or a list of them.
217
	 *
218
	 * @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
219
	 * @param bool $fetch True if the new ID should be returned in the item
220
	 * @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items
221
	 */
222
	public function save( $items, bool $fetch = true )
223
	{
224
		foreach( map( $items ) as $item )
225
		{
226
			if( method_exists( $this, 'saveItem' ) ) {
227
				$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

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