Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Base::getAddressItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Customer
9
 */
10
11
12
namespace Aimeos\MShop\Customer\Manager;
13
14
15
/**
16
 * Base class with common methods for all customer implementations.
17
 *
18
 * @package MShop
19
 * @subpackage Customer
20
 */
21
abstract class Base
22
	extends \Aimeos\MShop\Common\Manager\ListRef\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Customer\Manager\Iface
24
{
25
	private $salt;
26
	private $helper;
27
	private $addressManager;
28
29
30
	/**
31
	 * Initializes a new customer manager object using the given context object.
32
	 *
33
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
34
	 */
35
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
36
	{
37
		parent::__construct( $context );
38
		$this->setResourceName( 'db-customer' );
39
40
		/** mshop/customer/manager/salt
41
		 * Password salt for all customer passwords of the installation
42
		 *
43
		 * The default password salt is used if no user-specific salt can be
44
		 * stored in the database along with the user data. It's highly recommended
45
		 * to set the salt to a random string of at least eight chars using
46
		 * characters, digits and special characters
47
		 *
48
		 * @param string Installation wide password salt
49
		 * @since 2014.03
50
		 * @category Developer
51
		 * @category User
52
		 * @see mshop/customer/manager/password/name
53
		 * @sse mshop/customer/manager/password/options
54
		 */
55
		$this->salt = $context->getConfig()->get( 'mshop/customer/manager/salt', 'mshop' );
56
	}
57
58
59
	/**
60
	 * Creates a criteria object for searching.
61
	 *
62
	 * @param boolean $default Include default criteria like the status
63
	 * @return \Aimeos\MW\Criteria\Iface Search criteria object
64
	 */
65
	public function createSearch( $default = false )
66
	{
67
		if( $default === true ) {
68
			return $this->createSearchBase( 'customer' );
69
		}
70
71
		return parent::createSearch();
72
	}
73
74
75
	/**
76
	 * Returns the item specified by its code and domain/type if necessary
77
	 *
78
	 * @param string $code Code of the item
79
	 * @param string[] $ref List of domains to fetch list items and referenced items for
80
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
81
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
82
	 * @param boolean $default True to add default criteria
83
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
84
	 */
85
	public function findItem( $code, array $ref = [], $domain = null, $type = null, $default = false )
86
	{
87
		return $this->findItemBase( array( 'customer.code' => $code ), $ref, $default );
88
	}
89
90
91
	/**
92
	 * Returns the customer item object specificed by its ID.
93
	 *
94
	 * @param integer $id Unique customer ID referencing an existing customer
95
	 * @param string[] $ref List of domains to fetch list items and referenced items for
96
	 * @param boolean $default Add default criteria
97
	 * @return \Aimeos\MShop\Customer\Item\Iface Returns the customer item of the given id
98
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
99
	 */
100
	public function getItem( $id, array $ref = [], $default = false )
101
	{
102
		return $this->getItemBase( 'customer.id', $id, $ref, $default );
103
	}
104
105
106
	/**
107
	 * Adds the customer to the groups listed in the customer item
108
	 *
109
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item
110
	 */
111
	protected function addGroups( \Aimeos\MShop\Customer\Item\Iface $item )
112
	{
113
		$listMap = [];
114
		$manager = $this->getObject()->getSubManager( 'lists' );
115
116
		$search = $manager->createSearch();
117
		$expr = array(
118
			$search->compare( '==', 'customer.lists.parentid', $item->getId() ),
119
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
120
			$search->compare( '==', 'customer.lists.type.domain', 'customer/group' ),
121
			$search->compare( '==', 'customer.lists.type.code', 'default' ),
122
		);
123
		$search->setConditions( $search->combine( '&&', $expr ) );
124
		$search->setSlice( 0, 0x7fffffff );
125
126
		foreach( $manager->searchItems( $search ) as $listid => $listItem ) {
127
			$listMap[ $listItem->getRefId() ] = $listid;
128
		}
129
130
131
		if( $item->getGroups() !== [] )
132
		{
133
			$typeManager = $manager->getSubManager( 'type' );
134
			$typeId = $typeManager->findItem( 'default', [], 'customer/group', 'default' )->getId();
135
136
			$listItem = $manager->createItem();
137
			$listItem->setParentId( $item->getId() );
138
			$listItem->setDomain( 'customer/group' );
139
			$listItem->setTypeId( $typeId );
140
			$listItem->setStatus( 1 );
141
142
			$pos = count( $listMap ) ;
143
144
			foreach( $item->getGroups() as $gid )
145
			{
146
				if( isset( $listMap[$gid] ) )
147
				{
148
					unset( $listMap[$gid] );
149
					continue;
150
				}
151
152
				$listItem->setId( null );
153
				$listItem->setRefId( $gid );
154
				$listItem->setPosition( $pos++ );
155
156
				$manager->saveItem( $listItem, false );
157
			}
158
		}
159
160
		$manager->deleteItems( $listMap );
161
	}
162
163
164
	/**
165
	 * Creates a new customer item.
166
	 *
167
	 * @param array $values List of attributes for customer item
168
	 * @param array $listItems List items associated to the customer item
169
	 * @param array $refItems Items referenced by the customer item via the list items
170
	 * @param array $addresses List of address items of the customer item
171
	 * @param array $propItems List of property items of the customer item
172
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
173
	 */
174
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [],
175
		array $addresses = [], array $propItems = [] )
176
	{
177
		if( !isset( $this->addressManager ) ) {
178
			$this->addressManager = $this->getObject()->getSubManager( 'address' );
179
		}
180
181
		$helper = $this->getPasswordHelper();
182
		$address = $this->addressManager->createItem();
183
184
		return new \Aimeos\MShop\Customer\Item\Standard(
185
			$address, $values, $listItems, $refItems,
186
			$this->salt, $helper, $addresses, $propItems
187
		);
188
	}
189
190
191
	/**
192
	 * Returns a password helper object based on the configuration.
193
	 *
194
	 * @return \Aimeos\MShop\Common\Item\Helper\Password\Iface Password helper object
195
	 * @throws \Aimeos\MShop\Exception If the name is invalid or the class isn't found
196
	 */
197
	protected function getPasswordHelper()
198
	{
199
		if( $this->helper ) {
200
			return $this->helper;
201
		}
202
203
		$config = $this->getContext()->getConfig();
204
205
		/** mshop/customer/manager/password/name
206
		 * Last part of the name for building the password helper item
207
		 *
208
		 * The password helper encode given passwords and salts using the
209
		 * implemented hashing method in the required format. String format and
210
		 * hash algorithm needs to be the same when comparing the encoded
211
		 * password to the one provided by the user after login.
212
		 *
213
		 * @param string Name of the password helper implementation
214
		 * @since 2015.01
215
		 * @category Developer
216
		 * @see mshop/customer/manager/salt
217
		 * @see mshop/customer/manager/password/options
218
		 */
219
		$name = $config->get( 'mshop/customer/manager/password/name', 'Standard' );
220
221
		/** mshop/customer/manager/password/options
222
		 * List of options used by the password helper classes
223
		 *
224
		 * Each hash method may need an arbitrary number of options specific
225
		 * for the hash method. This may include the number of iterations the
226
		 * method is applied or the separator between salt and password.
227
		 *
228
		 * @param string Associative list of key/value pairs
229
		 * @since 2015.01
230
		 * @category Developer
231
		 * @see mshop/customer/manager/password/name
232
		 * @sse mshop/customer/manager/salt
233
		 */
234
		$options = $config->get( 'mshop/customer/manager/password/options', [] );
235
236
		if( ctype_alnum( $name ) === false )
237
		{
238
			$classname = is_string( $name ) ? '\\Aimeos\\MShop\\Common\\Item\\Helper\\Password\\' . $name : '<not a string>';
239
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
240
		}
241
242
		$iface = '\\Aimeos\\MShop\\Common\\Item\\Helper\\Password\\Iface';
243
		$classname = '\\Aimeos\\MShop\\Common\\Item\\Helper\\Password\\' . $name;
244
245
		if( class_exists( $classname ) === false ) {
246
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
247
		}
248
249
		$helper = new $classname( $options );
250
251
		if( !( $helper instanceof $iface ) ) {
252
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) );
253
		}
254
255
		$this->helper = $helper;
256
257
		return $helper;
258
	}
259
}