Passed
Push — master ( be1898...f642b9 )
by Aimeos
04:39
created

Base::getSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2023
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
		$this->init(); // init support trait
41
	}
42
43
44
	/**
45
	 * Removes old entries from the storage.
46
	 *
47
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
48
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
49
	 */
50
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
51
	{
52
		foreach( $this->context()->config()->get( $this->getConfigKey( 'submanagers' ), [] ) as $domain ) {
53
			$this->object()->getSubManager( $domain )->clear( $siteids );
54
		}
55
56
		return $this->clearBase( $siteids, $this->getConfigKey( 'delete' ) );
57
	}
58
59
60
	/**
61
	 * Creates a new empty item instance
62
	 *
63
	 * @param array $values Values the item should be initialized with
64
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object
65
	 */
66
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
67
	{
68
		$values['siteid'] = $values['siteid'] ?? $this->context()->locale()->getSiteId();
69
70
		return new \Aimeos\MShop\Common\Item\Base( '', $values );
71
	}
72
73
74
	/**
75
	 * Creates a new cursor based on the filter criteria
76
	 *
77
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
78
	 * @return \Aimeos\MShop\Common\Cursor\Iface Cursor object
79
	 */
80
	public function cursor( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Cursor\Iface
81
	{
82
		return new \Aimeos\MShop\Common\Cursor\Standard( $filter );
83
	}
84
85
86
	/**
87
	 * Removes multiple items.
88
	 *
89
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
90
	 * @return \Aimeos\MShop\Attribute\Manager\Iface Manager object for chaining method calls
91
	 */
92
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
93
	{
94
		return $this->deleteItemsBase( $itemIds, $this->getConfigKey( 'delete' ) );
95
	}
96
97
98
	/**
99
	 * Creates a search critera object
100
	 *
101
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
102
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
103
	 * @return \Aimeos\Base\Criteria\Iface New search criteria object
104
	 */
105
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
106
	{
107
		return $this->filterBase( $this->getDomain() );
108
	}
109
110
111
	/**
112
	 * Returns the attributes item specified by its ID.
113
	 *
114
	 * @param string $id Unique ID of the attribute item in the storage
115
	 * @param string[] $ref List of domains to fetch list items and referenced items for
116
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
117
	 * @return \Aimeos\MShop\Attribute\Item\Iface Returns the attribute item of the given id
118
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
119
	 */
120
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
121
	{
122
		return $this->getItemBase( 'id', $id, $ref, $default );
123
	}
124
125
126
	/**
127
	 * Returns the available manager types
128
	 *
129
	 * @param bool $withsub Return also the resource type of sub-managers if true
130
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
131
	 */
132
	public function getResourceType( bool $withsub = true ) : array
133
	{
134
		return $this->getResourceTypeBase( $this->getDomain(), $this->getConfigKey( 'submanagers' ), [], $withsub );
135
	}
136
137
138
	/**
139
	 * Returns the additional column/search definitions
140
	 *
141
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
142
	 */
143
	public function getSaveAttributes() : array
144
	{
145
		return [];
146
	}
147
148
149
	/**
150
	 * Returns the attributes that can be used for searching.
151
	 *
152
	 * @param bool $withsub Return also attributes of sub-managers if true
153
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items
154
	 */
155
	public function getSearchAttributes( bool $withsub = true ) : array
156
	{
157
		return $this->createAttributes( [
158
			'id' => [
159
				'label' => 'ID',
160
				'type' => 'int',
161
				'public' => false,
162
			],
163
			'siteid' => [
164
				'label' => 'Site ID',
165
				'public' => false,
166
			],
167
			'ctime' => [
168
				'label' => 'Create date/time',
169
				'type' => 'datetime',
170
				'public' => false,
171
			],
172
			'mtime' => [
173
				'label' => 'Modification date/time',
174
				'type' => 'datetime',
175
				'public' => false,
176
			],
177
			'editor' => [
178
				'label' => 'Editor',
179
				'public' => false,
180
			],
181
		] ) + $this->getSaveAttributes();
182
	}
183
184
185
	/**
186
	 * Returns a new manager for attribute extensions
187
	 *
188
	 * @param string $manager Name of the sub manager type in lower case
189
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
190
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
191
	 */
192
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
193
	{
194
		return $this->getSubManagerBase( $this->getDomain(), $manager, $name );
195
	}
196
197
198
	/**
199
	 * Iterates over all matched items and returns the found ones
200
	 *
201
	 * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor
202
	 * @param string[] $ref List of domains whose items should be fetched too
203
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
204
	 */
205
	public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map
206
	{
207
		if( $cursor->value() === '' ) {
208
			return null;
209
		}
210
211
		if( ( $first = current( $this->getSearchAttributes() ) ) === false ) {
212
			throw new \Aimeos\MShop\Exception( sprintf( 'No search configuration available for "%1$s"', get_class( $this ) ) );
213
		}
214
215
		$filter = $cursor->filter()->add( $first->getCode(), '>', (int) $cursor->value() )->order( $first->getCode() );
216
		$items = $this->search( $filter, $ref );
217
		$cursor->setValue( $items->lastKey() ?: '' );
218
219
		return !$items->isEmpty() ? $items : null;
220
	}
221
222
223
	/**
224
	 * Adds or updates an item object or a list of them.
225
	 *
226
	 * @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
227
	 * @param bool $fetch True if the new ID should be returned in the item
228
	 * @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items
229
	 */
230
	public function save( $items, bool $fetch = true )
231
	{
232
		foreach( map( $items ) as $item )
233
		{
234
			if( method_exists( $this, 'saveItem' ) ) {
235
				$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

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