Completed
Push — 2016.04 ( 690368...e61816 )
by Aimeos
04:37
created

Typo3::searchItems()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 122
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
nc 9
nop 3
dl 0
loc 122
rs 8.2857
c 2
b 0
f 0

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