Passed
Push — master ( 6aab01...0294bc )
by Aimeos
09:44
created

Standard::saveItem()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 106
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 106
rs 9.1768
c 0
b 0
f 0
cc 5
nc 9
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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Manager\Currency;
13
14
15
/**
16
 * Default implementation for managing currencies.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Locale\Manager\Currency\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	private $searchConfig = array(
26
		'locale.currency.id' => array(
27
			'code' => 'locale.currency.id',
28
			'internalcode' => 'mloccu."id"',
29
			'internaldeps' => array( 'LEFT JOIN "mshop_locale_currency" AS mloccu ON (mloc."currencyid" = mloccu."id")' ),
30
			'label' => 'Currency ID',
31
			'type' => 'string',
32
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
33
			'public' => false,
34
		),
35
		'locale.currency.label' => array(
36
			'code' => 'locale.currency.label',
37
			'internalcode' => 'mloccu."label"',
38
			'label' => 'Currency label',
39
			'type' => 'string',
40
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
41
		),
42
		'locale.currency.code' => array(
43
			'code' => 'locale.currency.code',
44
			'internalcode' => 'mloccu."id"',
45
			'label' => 'Currency code',
46
			'type' => 'string',
47
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
48
		),
49
		'locale.currency.status' => array(
50
			'code' => 'locale.currency.status',
51
			'internalcode' => 'mloccu."status"',
52
			'label' => 'Currency status',
53
			'type' => 'integer',
54
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
55
		),
56
		'locale.currency.ctime' => array(
57
			'code' => 'locale.currency.ctime',
58
			'internalcode' => 'mloccu."ctime"',
59
			'label' => 'Currency create date/time',
60
			'type' => 'datetime',
61
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
62
			'public' => false,
63
		),
64
		'locale.currency.mtime' => array(
65
			'code' => 'locale.currency.mtime',
66
			'internalcode' => 'mloccu."mtime"',
67
			'label' => 'Currency modify date/time',
68
			'type' => 'datetime',
69
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
70
			'public' => false,
71
		),
72
		'locale.currency.editor' => array(
73
			'code' => 'locale.currency.editor',
74
			'internalcode' => 'mloccu."editor"',
75
			'label' => 'Currency editor',
76
			'type' => 'string',
77
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
78
			'public' => false,
79
		),
80
	);
81
82
83
	/**
84
	 * Initializes the object.
85
	 *
86
	 * @param \Aimeos\MShop\ContextIface $context Context object
87
	 */
88
	public function __construct( \Aimeos\MShop\ContextIface $context )
89
	{
90
		parent::__construct( $context );
91
		$this->setResourceName( 'db-locale' );
92
	}
93
94
95
	/**
96
	 * Removes old entries from the storage.
97
	 *
98
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
99
	 * @return \Aimeos\MShop\Locale\Manager\Iface Manager object for chaining method calls
100
	 */
101
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
102
	{
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Creates a new empty item instance
109
	 *
110
	 * @param array $values Values the item should be initialized with
111
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface New locale currency item object
112
	 */
113
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
114
	{
115
		try {
116
			$values['locale.currency.siteid'] = $this->context()->locale()->getSiteId();
117
		} catch( \Exception $e ) {
118
			$values['locale.currency.siteid'] = null;
119
		}
120
121
		return $this->createItemBase( $values );
122
	}
123
124
125
	/**
126
	 * Saves a currency item to the storage.
127
	 *
128
	 * @param \Aimeos\MShop\Locale\Item\Currency\Iface $item Currency item to save in the storage
129
	 * @param bool $fetch True if the new ID should be returned in the item
130
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface $item Updated item including the generated ID
131
	 */
132
	public function saveItem( \Aimeos\MShop\Locale\Item\Currency\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Locale\Item\Currency\Iface
133
	{
134
		if( !$item->isModified() ) {
135
			return $item;
136
		}
137
138
		$context = $this->context();
139
		$conn = $context->db( $this->getResourceName() );
140
141
			$id = $item->getId();
142
			$date = date( 'Y-m-d H:i:s' );
143
			$columns = $this->object()->getSaveAttributes();
144
145
			if( $id === null )
146
			{
147
				/** mshop/locale/manager/currency/insert/mysql
148
				 * Inserts a new currency record into the database table
149
				 *
150
				 * @see mshop/locale/manager/currency/insert/ansi
151
				 */
152
153
				/** mshop/locale/manager/currency/insert/ansi
154
				 * Inserts a new currency record into the database table
155
				 *
156
				 * The SQL statement must be a string suitable for being used as
157
				 * prepared statement. It must include question marks for binding
158
				 * the values from the currency item to the statement before they are
159
				 * sent to the database server. The number of question marks must
160
				 * be the same as the number of columns listed in the INSERT
161
				 * statement. The order of the columns must correspond to the
162
				 * order in the save() method, so the correct values are
163
				 * bound to the columns.
164
				 *
165
				 * The SQL statement should conform to the ANSI standard to be
166
				 * compatible with most relational database systems. This also
167
				 * includes using double quotes for table and column names.
168
				 *
169
				 * @param string SQL statement for inserting records
170
				 * @since 2014.03
171
				 * @category Developer
172
				 * @see mshop/locale/manager/currency/update/ansi
173
				 * @see mshop/locale/manager/currency/delete/ansi
174
				 * @see mshop/locale/manager/currency/search/ansi
175
				 * @see mshop/locale/manager/currency/count/ansi
176
				 */
177
				$path = 'mshop/locale/manager/currency/insert';
178
				$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

178
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
179
			}
180
			else
181
			{
182
				/** mshop/locale/manager/currency/update/mysql
183
				 * Updates an existing currency record in the database
184
				 *
185
				 * @see mshop/locale/manager/currency/update/ansi
186
				 */
187
188
				/** mshop/locale/manager/currency/update/ansi
189
				 * Updates an existing currency record in the database
190
				 *
191
				 * The SQL statement must be a string suitable for being used as
192
				 * prepared statement. It must include question marks for binding
193
				 * the values from the currency item to the statement before they are
194
				 * sent to the database server. The order of the columns must
195
				 * correspond to the order in the save() method, so the
196
				 * correct values are bound to the columns.
197
				 *
198
				 * The SQL statement should conform to the ANSI standard to be
199
				 * compatible with most relational database systems. This also
200
				 * includes using double quotes for table and column names.
201
				 *
202
				 * @param string SQL statement for updating records
203
				 * @since 2014.03
204
				 * @category Developer
205
				 * @see mshop/locale/manager/currency/insert/ansi
206
				 * @see mshop/locale/manager/currency/delete/ansi
207
				 * @see mshop/locale/manager/currency/search/ansi
208
				 * @see mshop/locale/manager/currency/count/ansi
209
				 */
210
				$path = 'mshop/locale/manager/currency/update';
211
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
212
			}
213
214
			$idx = 1;
215
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
216
217
			foreach( $columns as $name => $entry ) {
218
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
219
			}
220
221
			$stmt->bind( $idx++, $item->getLabel() );
222
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
223
			$stmt->bind( $idx++, $date ); // mtime
224
			$stmt->bind( $idx++, $context->editor() );
225
			// bind ID but code and id are identical after saveing the stuff
226
			// id is the flag to detect updates or inserts!
227
			$stmt->bind( $idx++, $item->getCode() );
228
229
			if( $id === null ) {
230
				$stmt->bind( $idx++, $date ); // ctime
231
			}
232
233
			$stmt->execute()->finish();
234
235
			$item->setId( $item->getCode() ); // set modified flag to false
236
237
		return $item;
238
	}
239
240
241
	/**
242
	 * Removes multiple items.
243
	 *
244
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
245
	 * @return \Aimeos\MShop\Locale\Manager\Currency\Iface Manager object for chaining method calls
246
	 */
247
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
248
	{
249
		/** mshop/locale/manager/currency/delete/mysql
250
		 * Deletes the items matched by the given IDs from the database
251
		 *
252
		 * @see mshop/locale/manager/currency/delete/ansi
253
		 */
254
255
		/** mshop/locale/manager/currency/delete/ansi
256
		 * Deletes the items matched by the given IDs from the database
257
		 *
258
		 * Removes the language records specified by the given IDs from the
259
		 * locale database. The records must be from the site that is configured
260
		 * via the context item.
261
		 *
262
		 * The ":cond" placeholder is replaced by the name of the ID column and
263
		 * the given ID or list of IDs while the site ID is bound to the question
264
		 * mark.
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 deleting items
271
		 * @since 2014.03
272
		 * @category Developer
273
		 * @see mshop/locale/manager/currency/insert/ansi
274
		 * @see mshop/locale/manager/currency/update/ansi
275
		 * @see mshop/locale/manager/currency/search/ansi
276
		 * @see mshop/locale/manager/currency/count/ansi
277
		 */
278
		$path = 'mshop/locale/manager/currency/delete';
279
280
		return $this->deleteItemsBase( $itemIds, $path, false );
281
	}
282
283
284
	/**
285
	 * Returns the currency object with the given currency ID.
286
	 *
287
	 * @param string $id Currency ID indentifying the currency object
288
	 * @param string[] $ref List of domains to fetch list items and referenced items for
289
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
290
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Returns the currency item of the given id
291
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
292
	 */
293
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
294
	{
295
		return $this->getItemBase( 'locale.currency.id', $id, $ref, $default );
296
	}
297
298
299
	/**
300
	 * Returns the available manager types
301
	 *
302
	 * @param bool $withsub Return also the resource type of sub-managers if true
303
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
304
	 */
305
	public function getResourceType( bool $withsub = true ) : array
306
	{
307
		$path = 'mshop/locale/manager/currency/submanagers';
308
		return $this->getResourceTypeBase( 'locale/currency', $path, [], $withsub );
309
	}
310
311
312
	/**
313
	 * Returns the attributes that can be used for searching.
314
	 *
315
	 * @param bool $withsub Return also attributes of sub-managers if true
316
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
317
	 */
318
	public function getSearchAttributes( bool $withsub = true ) : array
319
	{
320
		/** mshop/locale/manager/currency/submanagers
321
		 * List of manager names that can be instantiated by the locale currency manager
322
		 *
323
		 * Managers provide a generic interface to the underlying storage.
324
		 * Each manager has or can have sub-managers caring about particular
325
		 * aspects. Each of these sub-managers can be instantiated by its
326
		 * parent manager using the getSubManager() method.
327
		 *
328
		 * The search keys from sub-managers can be normally used in the
329
		 * manager as well. It allows you to search for items of the manager
330
		 * using the search keys of the sub-managers to further limit the
331
		 * retrieved list of items.
332
		 *
333
		 * @param array List of sub-manager names
334
		 * @since 2014.03
335
		 * @category Developer
336
		 */
337
		$path = 'mshop/locale/manager/currency/submanagers';
338
339
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
340
	}
341
342
343
	/**
344
	 * Search for currency items matching the given criteria.
345
	 *
346
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
347
	 * @param string[] $ref List of domains to fetch list items and referenced items for
348
	 * @param int|null &$total Number of items that are available in total
349
	 * @return \Aimeos\Map List of items imlementing \Aimeos\MShop\Locale\Item\Currency\Iface with ids as keys
350
	 */
351
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
352
	{
353
		$context = $this->context();
354
		$conn = $context->db( $this->getResourceName() );
355
		$items = [];
356
357
			$attributes = $this->object()->getSearchAttributes();
358
			$translations = $this->getSearchTranslations( $attributes );
359
			$types = $this->getSearchTypes( $attributes );
360
			$columns = $this->object()->getSaveAttributes();
361
			$sortcols = $search->translate( $search->getSortations(), $translations );
362
363
			$colstring = '';
364
			foreach( $columns as $name => $entry ) {
365
				$colstring .= $entry->getInternalCode() . ', ';
366
			}
367
368
			$find = array( ':columns', ':cond', ':order', ':start', ':size' );
369
			$replace = array(
370
				$colstring . ( $sortcols ? join( ', ', $sortcols ) . ', ' : '' ),
371
				$search->getConditionSource( $types, $translations ),
372
				$search->getSortationSource( $types, $translations ),
373
				$search->getOffset(),
374
				$search->getLimit(),
375
			);
376
377
			/** mshop/locale/manager/currency/search/mysql
378
			 * Retrieves the records matched by the given criteria in the database
379
			 *
380
			 * @see mshop/locale/manager/currency/search/ansi
381
			 */
382
383
			/** mshop/locale/manager/currency/search/ansi
384
			 * Retrieves the records matched by the given criteria in the database
385
			 *
386
			 * Fetches the records matched by the given criteria from the attribute
387
			 * database. The records must be from one of the sites that are
388
			 * configured via the context item. If the current site is part of
389
			 * a tree of sites, the SELECT statement can retrieve all records
390
			 * from the current site and the complete sub-tree of sites.
391
			 *
392
			 * As the records can normally be limited by criteria from sub-managers,
393
			 * their tables must be joined in the SQL context. This is done by
394
			 * using the "internaldeps" property from the definition of the ID
395
			 * column of the sub-managers. These internal dependencies specify
396
			 * the JOIN between the tables and the used columns for joining. The
397
			 * ":joins" placeholder is then replaced by the JOIN strings from
398
			 * the sub-managers.
399
			 *
400
			 * To limit the records matched, conditions can be added to the given
401
			 * criteria object. It can contain comparisons like column names that
402
			 * must match specific values which can be combined by AND, OR or NOT
403
			 * operators. The resulting string of SQL conditions replaces the
404
			 * ":cond" placeholder before the statement is sent to the database
405
			 * server.
406
			 *
407
			 * If the records that are retrieved should be ordered by one or more
408
			 * columns, the generated string of column / sort direction pairs
409
			 * replaces the ":order" placeholder. In case no ordering is required,
410
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
411
			 * markers is removed to speed up retrieving the records. Columns of
412
			 * sub-managers can also be used for ordering the result set but then
413
			 * no index can be used.
414
			 *
415
			 * The number of returned records can be limited and can start at any
416
			 * number between the begining and the end of the result set. For that
417
			 * the ":size" and ":start" placeholders are replaced by the
418
			 * corresponding values from the criteria object. The default values
419
			 * are 0 for the start and 100 for the size value.
420
			 *
421
			 * The SQL statement should conform to the ANSI standard to be
422
			 * compatible with most relational database systems. This also
423
			 * includes using double quotes for table and column names.
424
			 *
425
			 * @param string SQL statement for searching items
426
			 * @since 2014.03
427
			 * @category Developer
428
			 * @see mshop/locale/manager/currency/insert/ansi
429
			 * @see mshop/locale/manager/currency/update/ansi
430
			 * @see mshop/locale/manager/currency/delete/ansi
431
			 * @see mshop/locale/manager/currency/count/ansi
432
			 */
433
			$path = 'mshop/locale/manager/currency/search';
434
435
			$sql = $this->getSqlConfig( $path );
436
			$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
437
438
			try
439
			{
440
				while( ( $row = $results->fetch() ) !== null )
441
				{
442
					if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
443
						$items[$row['locale.currency.id']] = $item;
444
					}
445
				}
446
			}
447
			catch( \Exception $e )
448
			{
449
				$results->finish();
450
				throw $e;
451
			}
452
453
			if( $total !== null ) {
454
				$total = $this->getTotal( $conn, $find, $replace );
455
			}
456
457
		return map( $items );
458
	}
459
460
461
	/**
462
	 * Returns a new sub manager of the given type and name.
463
	 *
464
	 * @param string $manager Name of the sub manager type in lower case
465
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
466
	 * @return \Aimeos\MShop\Common\Manager\Iface manager
467
	 */
468
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
469
	{
470
		/** mshop/locale/manager/currency/name
471
		 * Class name of the used locale currency manager implementation
472
		 *
473
		 * Each default locale currency manager can be replaced by an alternative imlementation.
474
		 * To use this implementation, you have to set the last part of the class
475
		 * name as configuration value so the manager factory knows which class it
476
		 * has to instantiate.
477
		 *
478
		 * For example, if the name of the default class is
479
		 *
480
		 *  \Aimeos\MShop\Locale\Manager\Currency\Standard
481
		 *
482
		 * and you want to replace it with your own version named
483
		 *
484
		 *  \Aimeos\MShop\Locale\Manager\Currency\Mycurrency
485
		 *
486
		 * then you have to set the this configuration option:
487
		 *
488
		 *  mshop/locale/manager/currency/name = Mycurrency
489
		 *
490
		 * The value is the last part of your own class name and it's case sensitive,
491
		 * so take care that the configuration value is exactly named like the last
492
		 * part of the class name.
493
		 *
494
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
495
		 * characters are possible! You should always start the last part of the class
496
		 * name with an upper case character and continue only with lower case characters
497
		 * or numbers. Avoid chamel case names like "MyCurrency"!
498
		 *
499
		 * @param string Last part of the class name
500
		 * @since 2014.03
501
		 * @category Developer
502
		 */
503
504
		/** mshop/locale/manager/currency/decorators/excludes
505
		 * Excludes decorators added by the "common" option from the locale currency manager
506
		 *
507
		 * Decorators extend the functionality of a class by adding new aspects
508
		 * (e.g. log what is currently done), executing the methods of the underlying
509
		 * class only in certain conditions (e.g. only for logged in users) or
510
		 * modify what is returned to the caller.
511
		 *
512
		 * This option allows you to remove a decorator added via
513
		 * "mshop/common/manager/decorators/default" before they are wrapped
514
		 * around the locale currency manager.
515
		 *
516
		 *  mshop/locale/manager/currency/decorators/excludes = array( 'decorator1' )
517
		 *
518
		 * This would remove the decorator named "decorator1" from the list of
519
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
520
		 * "mshop/common/manager/decorators/default" for the locale currency manager.
521
		 *
522
		 * @param array List of decorator names
523
		 * @since 2014.03
524
		 * @category Developer
525
		 * @see mshop/common/manager/decorators/default
526
		 * @see mshop/locale/manager/currency/decorators/global
527
		 * @see mshop/locale/manager/currency/decorators/local
528
		 */
529
530
		/** mshop/locale/manager/currency/decorators/global
531
		 * Adds a list of globally available decorators only to the locale currency manager
532
		 *
533
		 * Decorators extend the functionality of a class by adding new aspects
534
		 * (e.g. log what is currently done), executing the methods of the underlying
535
		 * class only in certain conditions (e.g. only for logged in users) or
536
		 * modify what is returned to the caller.
537
		 *
538
		 * This option allows you to wrap global decorators
539
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale currency
540
		 * manager.
541
		 *
542
		 *  mshop/locale/manager/currency/decorators/global = array( 'decorator1' )
543
		 *
544
		 * This would add the decorator named "decorator1" defined by
545
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
546
		 * currency manager.
547
		 *
548
		 * @param array List of decorator names
549
		 * @since 2014.03
550
		 * @category Developer
551
		 * @see mshop/common/manager/decorators/default
552
		 * @see mshop/locale/manager/currency/decorators/excludes
553
		 * @see mshop/locale/manager/currency/decorators/local
554
		 */
555
556
		/** mshop/locale/manager/currency/decorators/local
557
		 * Adds a list of local decorators only to the locale currency manager
558
		 *
559
		 * Decorators extend the functionality of a class by adding new aspects
560
		 * (e.g. log what is currently done), executing the methods of the underlying
561
		 * class only in certain conditions (e.g. only for logged in users) or
562
		 * modify what is returned to the caller.
563
		 *
564
		 * This option allows you to wrap local decorators
565
		 * ("\Aimeos\MShop\Locale\Manager\Currency\Decorator\*") around the locale
566
		 * currency manager.
567
		 *
568
		 *  mshop/locale/manager/currency/decorators/local = array( 'decorator2' )
569
		 *
570
		 * This would add the decorator named "decorator2" defined by
571
		 * "\Aimeos\MShop\Locale\Manager\Currency\Decorator\Decorator2" only to the
572
		 * locale currency manager.
573
		 *
574
		 * @param array List of decorator names
575
		 * @since 2014.03
576
		 * @category Developer
577
		 * @see mshop/common/manager/decorators/default
578
		 * @see mshop/locale/manager/currency/decorators/excludes
579
		 * @see mshop/locale/manager/currency/decorators/global
580
		 */
581
582
		return $this->getSubManagerBase( 'locale', 'currency/' . $manager, $name );
583
	}
584
585
586
	/**
587
	 * Returns the item specified by its code and domain/type if necessary
588
	 *
589
	 * @param string $code Code of the item
590
	 * @param string[] $ref List of domains to fetch list items and referenced items for
591
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
592
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
593
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
594
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
595
	 */
596
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
597
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
598
	{
599
		return $this->findBase( array( 'locale.currency.id' => $code ), $ref, $default );
600
	}
601
602
603
	/**
604
	 * Creates a filter object.
605
	 *
606
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
607
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
608
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
609
	 */
610
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
611
	{
612
		return $this->filterBase( 'locale.currency', $default );
613
	}
614
615
616
	/**
617
	 * Returns the search results for the given SQL statement.
618
	 *
619
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
620
	 * @param string $sql SQL statement
621
	 * @return \Aimeos\Base\DB\Result\Iface Search result object
622
	 */
623
	protected function getSearchResults( \Aimeos\Base\DB\Connection\Iface $conn, string $sql ) : \Aimeos\Base\DB\Result\Iface
624
	{
625
		$time = microtime( true );
626
627
		$stmt = $conn->create( $sql );
628
		$result = $stmt->execute();
629
630
		$msg = 'Time: ' . ( microtime( true ) - $time ) * 1000 . "ms\n"
631
			. 'Class: ' . get_class( $this ) . "\n"
632
			. str_replace( ["\t", "\n\n"], ['', "\n"], trim( (string) $stmt ) );
633
634
		$this->context()->logger()->debug( $msg, 'core/sql' );
635
636
		return $result;
637
	}
638
639
640
	/**
641
	 * Create new item object initialized with given parameters.
642
	 *
643
	 * @param array $data Associative list of item key/value pairs
644
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Currency item object
645
	 */
646
	protected function createItemBase( array $data = [] ) : \Aimeos\MShop\Locale\Item\Currency\Iface
647
	{
648
		return new \Aimeos\MShop\Locale\Item\Currency\Standard( $data );
649
	}
650
651
652
	/**
653
	 * Returns the total number of items found for the conditions
654
	 *
655
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
656
	 * @param string[] $find List of markers that should be replaced in the SQL statement
657
	 * @param string[] $replace List of replacements for the markers in the SQL statement
658
	 * @throws \Aimeos\MShop\Locale\Exception If no total value was found
659
	 * @return int Total number of found items
660
	 */
661
	protected function getTotal( \Aimeos\Base\DB\Connection\Iface $conn, array $find, array $replace ) : int
662
	{
663
		/** mshop/locale/manager/currency/count/mysql
664
		 * Counts the number of records matched by the given criteria in the database
665
		 *
666
		 * @see mshop/locale/manager/currency/count/ansi
667
		 */
668
669
		/** mshop/locale/manager/currency/count/ansi
670
		 * Counts the number of records matched by the given criteria in the database
671
		 *
672
		 * Counts all records matched by the given criteria from the attribute
673
		 * database. The records must be from one of the sites that are
674
		 * configured via the context item. If the current site is part of
675
		 * a tree of sites, the statement can count all records from the
676
		 * current site and the complete sub-tree of sites.
677
		 *
678
		 * As the records can normally be limited by criteria from sub-managers,
679
		 * their tables must be joined in the SQL context. This is done by
680
		 * using the "internaldeps" property from the definition of the ID
681
		 * column of the sub-managers. These internal dependencies specify
682
		 * the JOIN between the tables and the used columns for joining. The
683
		 * ":joins" placeholder is then replaced by the JOIN strings from
684
		 * the sub-managers.
685
		 *
686
		 * To limit the records matched, conditions can be added to the given
687
		 * criteria object. It can contain comparisons like column names that
688
		 * must match specific values which can be combined by AND, OR or NOT
689
		 * operators. The resulting string of SQL conditions replaces the
690
		 * ":cond" placeholder before the statement is sent to the database
691
		 * server.
692
		 *
693
		 * Both, the strings for ":joins" and for ":cond" are the same as for
694
		 * the "search" SQL statement.
695
		 *
696
		 * Contrary to the "search" statement, it doesn't return any records
697
		 * but instead the number of records that have been found. As counting
698
		 * thousands of records can be a long running task, the maximum number
699
		 * of counted records is limited for performance reasons.
700
		 *
701
		 * The SQL statement should conform to the ANSI standard to be
702
		 * compatible with most relational database systems. This also
703
		 * includes using double quotes for table and column names.
704
		 *
705
		 * @param string SQL statement for counting items
706
		 * @since 2014.03
707
		 * @category Developer
708
		 * @see mshop/locale/manager/currency/insert/ansi
709
		 * @see mshop/locale/manager/currency/update/ansi
710
		 * @see mshop/locale/manager/currency/delete/ansi
711
		 * @see mshop/locale/manager/currency/search/ansi
712
		 */
713
		$path = 'mshop/locale/manager/currency/count';
714
715
		$sql = $this->getSqlConfig( $path );
716
		$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
717
718
		$row = $results->fetch();
719
		$results->finish();
720
721
		if( $row === null ) {
722
			throw new \Aimeos\MShop\Locale\Exception( 'No total results value found' );
723
		}
724
725
		return $row['count'];
726
	}
727
}
728