Passed
Branch master (ba77f4)
by Aimeos
03:57
created

Typo3::searchItems()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 121
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 121
rs 9.6666
c 0
b 0
f 0
cc 3
nc 9
nop 3

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-2018
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager\Group;
12
13
14
/**
15
 * TYPO3 implementation of the customer group class
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class Typo3
21
	extends \Aimeos\MShop\Customer\Manager\Group\Standard
22
{
23
	private $searchConfig = array(
24
		'customer.group.id' => array(
25
			'code' => 'customer.group.id',
26
			'internalcode' => 't3feg."uid"',
27
			'label' => 'Customer group ID',
28
			'type' => 'integer',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
30
		),
31
		'customer.group.code' => array(
32
			'code' => 'customer.group.code',
33
			'internalcode' => 't3feg."title"',
34
			'label' => 'Customer group code',
35
			'type' => 'string',
36
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
37
		),
38
		'customer.group.label' => array(
39
			'code' => 'customer.group.label',
40
			'internalcode' => 't3feg."title"',
41
			'label' => 'Customer group label',
42
			'type' => 'string',
43
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
44
		),
45
		'customer.group.ctime'=> array(
46
			'code' => 'customer.group.ctime',
47
			'internalcode' => 't3feg."crdate"',
48
			'label' => 'Customer group creation time',
49
			'type' => 'datetime',
50
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
51
		),
52
		'customer.group.mtime'=> array(
53
			'code' => 'customer.group.mtime',
54
			'internalcode' => 't3feg."tstamp"',
55
			'label' => 'Customer group modification time',
56
			'type' => 'datetime',
57
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
58
		),
59
		'customer.group.editor'=> array(
60
			'code' => 'customer.group.editor',
61
			'internalcode' => '1',
62
			'label' => 'Customer group editor',
63
			'type' => 'string',
64
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
65
		),
66
	);
67
68
	private $plugins = [];
69
	private $reverse = [];
70
71
72
	/**
73
	 * Initializes the customer group manager object
74
	 *
75
	 * @param \Aimeos\MShop\Context\Iface $context Context object with required objects
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Context\Iface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
	 */
77
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
78
	{
79
		parent::__construct( $context );
80
81
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Datetime();
82
		$this->plugins['customer.ctime'] = $this->reverse['crdate'] = $plugin;
83
		$this->plugins['customer.mtime'] = $this->reverse['tstamp'] = $plugin;
84
	}
85
86
87
	/**
88
	 * Removes old entries from the database
89
	 *
90
	 * @param integer[] $siteids List of IDs for sites whose entries should be deleted
91
	 */
92
	public function cleanup( array $siteids )
93
	{
94
		$path = 'mshop/customer/manager/group/submanagers';
95
96
		foreach( $this->getContext()->getConfig()->get( $path, [] ) as $domain ) {
97
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
98
		}
99
	}
100
101
102
	/**
103
	 * Removes multiple items specified by their IDs
104
	 *
105
	 * @param array $ids List of IDs
106
	 */
107
	public function deleteItems( array $ids )
108
	{
109
		throw new \Aimeos\MShop\Customer\Exception( sprintf( 'Deleting groups is not supported, please use the TYPO3 backend' ) );
110
	}
111
112
113
	/**
114
	 * Returns the attributes that can be used for searching
115
	 *
116
	 * @param boolean $withsub Return attributes of sub-managers too if true
117
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
118
	 */
119
	public function getSearchAttributes( $withsub = true )
120
	{
121
		$path = 'mshop/customer/manager/group/submanagers';
122
123
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
124
	}
125
126
127
	/**
128
	 * Returns a new manager for customer group extensions
129
	 *
130
	 * @param string $manager Name of the sub manager type in lower case
131
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
132
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions
133
	 */
134
	public function getSubManager( $manager, $name = null )
135
	{
136
		return $this->getSubManagerBase( 'customer/group', $manager, ( $name === null ? 'Typo3' : $name ) );
137
	}
138
139
140
	/**
141
	 * Inserts a new or updates an existing customer group item
142
	 *
143
	 * @param \Aimeos\MShop\Customer\Item\Group\Iface $item Customer group item
144
	 * @param boolean $fetch True if the new ID should be returned in the item
145
	 */
146
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
147
	{
148
		if( $item->isModified() === true ) {
149
			throw new \Aimeos\MShop\Customer\Exception( sprintf( 'Saving groups is not supported, please use the TYPO3 backend' ) );
150
		}
151
152
		return $item;
153
	}
154
155
156
	/**
157
	 * Returns the item objects matched by the given search criteria.
158
	 *
159
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
160
	 * @param array $ref List of domain items that should be fetched too
161
	 * @param integer &$total Number of items that are available in total
162
	 * @return array List of items implementing \Aimeos\MShop\Customer\Item\Group\Iface
163
	 * @throws \Aimeos\MShop\Exception If retrieving items failed
164
	 */
165
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
166
	{
167
		$map = [];
168
		$context = $this->getContext();
169
170
		$dbm = $context->getDatabaseManager();
171
		$dbname = $this->getResourceName();
172
		$conn = $dbm->acquire( $dbname );
173
174
		try
175
		{
176
			$required = array( 'customer.group' );
177
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
178
179
			/** mshop/customer/manager/group/typo3/search
180
			 * Retrieves the records matched by the given criteria in the database
181
			 *
182
			 * Fetches the records matched by the given criteria from the customer
183
			 * database. The records must be from one of the sites that are
184
			 * configured via the context item. If the current site is part of
185
			 * a tree of sites, the SELECT statement can retrieve all records
186
			 * from the current site and the complete sub-tree of sites.
187
			 *
188
			 * As the records can normally be limited by criteria from sub-managers,
189
			 * their tables must be joined in the SQL context. This is done by
190
			 * using the "internaldeps" property from the definition of the ID
191
			 * column of the sub-managers. These internal dependencies specify
192
			 * the JOIN between the tables and the used columns for joining. The
193
			 * ":joins" placeholder is then replaced by the JOIN strings from
194
			 * the sub-managers.
195
			 *
196
			 * To limit the records matched, conditions can be added to the given
197
			 * criteria object. It can contain comparisons like column names that
198
			 * must match specific values which can be combined by AND, OR or NOT
199
			 * operators. The resulting string of SQL conditions replaces the
200
			 * ":cond" placeholder before the statement is sent to the database
201
			 * server.
202
			 *
203
			 * If the records that are retrieved should be ordered by one or more
204
			 * columns, the generated string of column / sort direction pairs
205
			 * replaces the ":order" placeholder. In case no ordering is required,
206
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
207
			 * markers is removed to speed up retrieving the records. Columns of
208
			 * sub-managers can also be used for ordering the result set but then
209
			 * no index can be used.
210
			 *
211
			 * The number of returned records can be limited and can start at any
212
			 * number between the begining and the end of the result set. For that
213
			 * the ":size" and ":start" placeholders are replaced by the
214
			 * corresponding values from the criteria object. The default values
215
			 * are 0 for the start and 100 for the size value.
216
			 *
217
			 * The SQL statement should conform to the ANSI standard to be
218
			 * compatible with most relational database systems. This also
219
			 * includes using double quotes for table and column names.
220
			 *
221
			 * @param string SQL statement for searching items
222
			 * @since 2015.08
223
			 * @category Developer
224
			 * @see mshop/customer/manager/group/typo3/count
225
			 */
226
			$cfgPathSearch = 'mshop/customer/manager/group/typo3/search';
227
228
			/** mshop/customer/manager/group/typo3/count
229
			 * Counts the number of records matched by the given criteria in the database
230
			 *
231
			 * Counts all records matched by the given criteria from the customer
232
			 * database. The records must be from one of the sites that are
233
			 * configured via the context item. If the current site is part of
234
			 * a tree of sites, the statement can count all records from the
235
			 * current site and the complete sub-tree of sites.
236
			 *
237
			 * As the records can normally be limited by criteria from sub-managers,
238
			 * their tables must be joined in the SQL context. This is done by
239
			 * using the "internaldeps" property from the definition of the ID
240
			 * column of the sub-managers. These internal dependencies specify
241
			 * the JOIN between the tables and the used columns for joining. The
242
			 * ":joins" placeholder is then replaced by the JOIN strings from
243
			 * the sub-managers.
244
			 *
245
			 * To limit the records matched, conditions can be added to the given
246
			 * criteria object. It can contain comparisons like column names that
247
			 * must match specific values which can be combined by AND, OR or NOT
248
			 * operators. The resulting string of SQL conditions replaces the
249
			 * ":cond" placeholder before the statement is sent to the database
250
			 * server.
251
			 *
252
			 * Both, the strings for ":joins" and for ":cond" are the same as for
253
			 * the "search" SQL statement.
254
			 *
255
			 * Contrary to the "search" statement, it doesn't return any records
256
			 * but instead the number of records that have been found. As counting
257
			 * thousands of records can be a long running task, the maximum number
258
			 * of counted records is limited for performance reasons.
259
			 *
260
			 * The SQL statement should conform to the ANSI standard to be
261
			 * compatible with most relational database systems. This also
262
			 * includes using double quotes for table and column names.
263
			 *
264
			 * @param string SQL statement for counting items
265
			 * @since 2015.08
266
			 * @category Developer
267
			 * @see mshop/customer/manager/group/typo3/search
268
			 */
269
			$cfgPathCount = 'mshop/customer/manager/group/typo3/count';
270
271
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
272
273
			while( ( $row = $results->fetch() ) !== false ) {
274
				$map[$row['customer.group.id']] = $this->createItemBase( $row );
275
			}
276
277
			$dbm->release( $conn, $dbname );
278
		}
279
		catch( \Exception $e )
280
		{
281
			$dbm->release( $conn, $dbname );
282
			throw $e;
283
		}
284
285
		return $map;
286
	}
287
288
289
	/**
290
	 * Creates a new customer item.
291
	 *
292
	 * @param array $values List of attributes for customer item
293
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
294
	 */
295
	protected function createItemBase( array $values = [] )
296
	{
297
		$values['customer.group.siteid'] = $this->getContext()->getLocale()->getSiteId();
298
299
		if( array_key_exists( 'tstamp', $values ) ) {
300
			$values['customer.group.mtime'] = $this->reverse['tstamp']->reverse( $values['tstamp'] );
301
		}
302
303
		if( array_key_exists( 'crdate', $values ) ) {
304
			$values['customer.group.ctime'] = $this->reverse['crdate']->reverse( $values['crdate'] );
305
		}
306
307
		return new \Aimeos\MShop\Customer\Item\Group\Standard( $values );
308
	}
309
}
310