Passed
Push — master ( 3311dc...3c99d6 )
by Aimeos
05:26
created

lib/mshoplib/src/MShop/Customer/Manager/Base.php (1 issue)

Severity
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-2018
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\Base
23
{
24
	use \Aimeos\MShop\Common\Manager\ListRef\Traits;
25
26
27
	private $salt;
28
	private $helper;
29
30
31
	/**
32
	 * Initializes a new customer manager object using the given context object.
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
37
	{
38
		parent::__construct( $context );
39
		$this->setResourceName( 'db-customer' );
40
41
		/** mshop/customer/manager/salt
42
		 * Password salt for all customer passwords of the installation
43
		 *
44
		 * The default password salt is used if no user-specific salt can be
45
		 * stored in the database along with the user data. It's highly recommended
46
		 * to set the salt to a random string of at least eight chars using
47
		 * characters, digits and special characters
48
		 *
49
		 * @param string Installation wide password salt
50
		 * @since 2014.03
51
		 * @category Developer
52
		 * @category User
53
		 * @see mshop/customer/manager/password/name
54
		 * @sse mshop/customer/manager/password/options
55
		 */
56
		$this->salt = $context->getConfig()->get( 'mshop/customer/manager/salt', 'mshop' );
57
	}
58
59
60
	/**
61
	 * Creates a criteria object for searching.
62
	 *
63
	 * @param boolean $default Include default criteria like the status
64
	 * @return \Aimeos\MW\Criteria\Iface Search criteria object
65
	 */
66
	public function createSearch( $default = false )
67
	{
68
		if( $default === true ) {
69
			return $this->createSearchBase( 'customer' );
70
		}
71
72
		return parent::createSearch();
73
	}
74
75
76
	/**
77
	 * Returns the item specified by its code and domain/type if necessary
78
	 *
79
	 * @param string $code Code of the item
80
	 * @param string[] $ref List of domains to fetch list items and referenced items for
81
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
82
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
83
	 * @param boolean $default True to add default criteria
84
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
85
	 */
86
	public function findItem( $code, array $ref = [], $domain = null, $type = null, $default = false )
87
	{
88
		return $this->findItemBase( array( 'customer.code' => $code ), $ref, $default );
89
	}
90
91
92
	/**
93
	 * Returns the customer item object specificed by its ID.
94
	 *
95
	 * @param string $id Unique customer ID referencing an existing customer
96
	 * @param string[] $ref List of domains to fetch list items and referenced items for
97
	 * @param boolean $default Add default criteria
98
	 * @return \Aimeos\MShop\Customer\Item\Iface Returns the customer item of the given id
99
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
100
	 */
101
	public function getItem( $id, array $ref = [], $default = false )
102
	{
103
		return $this->getItemBase( 'customer.id', $id, $ref, $default );
104
	}
105
106
107
	/**
108
	 * Adds the customer to the groups listed in the customer item
109
	 *
110
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item
111
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Modified customer item
112
	 */
113
	protected function addGroups( \Aimeos\MShop\Customer\Item\Iface $item )
114
	{
115
		$pos = 0;
116
		$groupIds = [];
0 ignored issues
show
The assignment to $groupIds is dead and can be removed.
Loading history...
117
118
		$manager = $this->getObject()->getSubManager( 'lists' );
119
		$listItem = $manager->createItem()->setType( 'default' );
120
		$listItems = $item->getListItems( 'customer/group', 'default', null, false );
121
122
		foreach( $item->getGroups() as $refId )
123
		{
124
			if( ( $litem = $item->getListItem( 'customer/group', 'default', $refId, false ) ) !== null ) {
125
				unset( $listItems[$litem->getId()] );
126
			} else {
127
				$litem = clone $listItem;
128
			}
129
130
			$item->addListItem( 'customer/group', $litem->setRefId( $refId )->setPosition( $pos++ ) );
131
		}
132
133
		return $item->deleteListItems( $listItems );
134
	}
135
136
137
	/**
138
	 * Creates a new customer item.
139
	 *
140
	 * @param array $values List of attributes for customer item
141
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
142
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
143
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $addrItems List of address items
144
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
145
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
146
	 */
147
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [],
148
		array $addrItems = [], array $propItems = [] )
149
	{
150
		$helper = $this->getPasswordHelper();
151
		$address = new \Aimeos\MShop\Common\Item\Address\Simple( 'customer.', $values );
152
153
		return new \Aimeos\MShop\Customer\Item\Standard(
154
			$address, $values, $listItems, $refItems, $addrItems, $propItems, $helper, $this->salt
155
		);
156
	}
157
158
159
	/**
160
	 * Returns a password helper object based on the configuration.
161
	 *
162
	 * @return \Aimeos\MShop\Common\Helper\Password\Iface Password helper object
163
	 * @throws \Aimeos\MShop\Exception If the name is invalid or the class isn't found
164
	 */
165
	protected function getPasswordHelper()
166
	{
167
		if( $this->helper ) {
168
			return $this->helper;
169
		}
170
171
		$config = $this->getContext()->getConfig();
172
173
		/** mshop/customer/manager/password/name
174
		 * Last part of the name for building the password helper item
175
		 *
176
		 * The password helper encode given passwords and salts using the
177
		 * implemented hashing method in the required format. String format and
178
		 * hash algorithm needs to be the same when comparing the encoded
179
		 * password to the one provided by the user after login.
180
		 *
181
		 * @param string Name of the password helper implementation
182
		 * @since 2015.01
183
		 * @category Developer
184
		 * @see mshop/customer/manager/salt
185
		 * @see mshop/customer/manager/password/options
186
		 */
187
		$name = $config->get( 'mshop/customer/manager/password/name', 'Standard' );
188
189
		/** mshop/customer/manager/password/options
190
		 * List of options used by the password helper classes
191
		 *
192
		 * Each hash method may need an arbitrary number of options specific
193
		 * for the hash method. This may include the number of iterations the
194
		 * method is applied or the separator between salt and password.
195
		 *
196
		 * @param string Associative list of key/value pairs
197
		 * @since 2015.01
198
		 * @category Developer
199
		 * @see mshop/customer/manager/password/name
200
		 * @sse mshop/customer/manager/salt
201
		 */
202
		$options = $config->get( 'mshop/customer/manager/password/options', [] );
203
204
		if( ctype_alnum( $name ) === false )
205
		{
206
			$classname = is_string( $name ) ? '\Aimeos\MShop\Common\Helper\Password\\' . $name : '<not a string>';
207
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
208
		}
209
210
		$classname = '\Aimeos\MShop\Common\Helper\Password\\' . $name;
211
212
		if( class_exists( $classname ) === false ) {
213
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
214
		}
215
216
		$helper = new $classname( $options );
217
218
		self::checkClass( \Aimeos\MShop\Common\Helper\Password\Iface::class, $helper );
219
220
		$this->helper = $helper;
221
222
		return $helper;
223
	}
224
}
225