Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Customer.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Framework\Validator\Exception as ValidatorException;
13 
21 class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
22 {
26  protected $_validatorFactory;
27 
33  protected $_scopeConfig;
34 
38  protected $dateTime;
39 
43  protected $storeManager;
44 
48  private $notificationStorage;
49 
60  public function __construct(
61  \Magento\Eav\Model\Entity\Context $context,
62  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
63  \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite,
64  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
65  \Magento\Framework\Validator\Factory $validatorFactory,
66  \Magento\Framework\Stdlib\DateTime $dateTime,
68  $data = []
69  ) {
70  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data);
71  $this->_scopeConfig = $scopeConfig;
72  $this->_validatorFactory = $validatorFactory;
73  $this->dateTime = $dateTime;
74  $this->storeManager = $storeManager;
75  $this->setType('customer');
76  $this->setConnection('customer_read', 'customer_write');
77  }
78 
84  protected function _getDefaultAttributes()
85  {
86  return [
87  'created_at',
88  'updated_at',
89  'increment_id',
90  'store_id',
91  'website_id'
92  ];
93  }
94 
104  protected function _beforeSave(\Magento\Framework\DataObject $customer)
105  {
107  if ($customer->getStoreId() === null) {
108  $customer->setStoreId($this->storeManager->getStore()->getId());
109  }
110  $customer->getGroupId();
111 
112  parent::_beforeSave($customer);
113 
114  if (!$customer->getEmail()) {
115  throw new ValidatorException(__('The customer email is missing. Enter and try again.'));
116  }
117 
118  $connection = $this->getConnection();
119  $bind = ['email' => $customer->getEmail()];
120 
121  $select = $connection->select()->from(
122  $this->getEntityTable(),
123  [$this->getEntityIdField()]
124  )->where(
125  'email = :email'
126  );
127  if ($customer->getSharingConfig()->isWebsiteScope()) {
128  $bind['website_id'] = (int)$customer->getWebsiteId();
129  $select->where('website_id = :website_id');
130  }
131  if ($customer->getId()) {
132  $bind['entity_id'] = (int)$customer->getId();
133  $select->where('entity_id != :entity_id');
134  }
135 
136  $result = $connection->fetchOne($select, $bind);
137  if ($result) {
138  throw new AlreadyExistsException(
139  __('A customer with the same email address already exists in an associated website.')
140  );
141  }
142 
143  // set confirmation key logic
144  if ($customer->getForceConfirmed() || $customer->getPasswordHash() == '') {
145  $customer->setConfirmation(null);
146  } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
147  $customer->setConfirmation($customer->getRandomConfirmationKey());
148  }
149  // remove customer confirmation key from database, if empty
150  if (!$customer->getConfirmation()) {
151  $customer->setConfirmation(null);
152  }
153 
154  $this->_validate($customer);
155 
156  return $this;
157  }
158 
166  protected function _validate($customer)
167  {
168  $validator = $this->_validatorFactory->createValidator('customer', 'save');
169 
170  if (!$validator->isValid($customer)) {
171  throw new ValidatorException(
172  null,
173  null,
174  $validator->getMessages()
175  );
176  }
177  }
178 
184  private function getNotificationStorage()
185  {
186  if ($this->notificationStorage === null) {
187  $this->notificationStorage = ObjectManager::getInstance()->get(NotificationStorage::class);
188  }
189  return $this->notificationStorage;
190  }
191 
198  protected function _afterSave(\Magento\Framework\DataObject $customer)
199  {
200  $this->getNotificationStorage()->add(
202  $customer->getId()
203  );
204  return parent::_afterSave($customer);
205  }
206 
214  protected function _getLoadRowSelect($object, $rowId)
215  {
216  $select = parent::_getLoadRowSelect($object, $rowId);
217  if ($object->getWebsiteId() && $object->getSharingConfig()->isWebsiteScope()) {
218  $select->where('website_id =?', (int)$object->getWebsiteId());
219  }
220 
221  return $select;
222  }
223 
233  {
234  $connection = $this->getConnection();
235  $bind = ['customer_email' => $email];
236  $select = $connection->select()->from(
237  $this->getEntityTable(),
238  [$this->getEntityIdField()]
239  )->where(
240  'email = :customer_email'
241  );
242 
243  if ($customer->getSharingConfig()->isWebsiteScope()) {
244  if (!$customer->hasData('website_id')) {
245  throw new \Magento\Framework\Exception\LocalizedException(
246  __("A customer website ID wasn't specified. The ID must be specified to use the website scope.")
247  );
248  }
249  $bind['website_id'] = (int)$customer->getWebsiteId();
250  $select->where('website_id = :website_id');
251  }
252 
253  $customerId = $connection->fetchOne($select, $bind);
254  if ($customerId) {
255  $this->load($customer, $customerId);
256  } else {
257  $customer->setData([]);
258  }
259 
260  return $this;
261  }
262 
270  public function changePassword(\Magento\Customer\Model\Customer $customer, $newPassword)
271  {
272  $customer->setPassword($newPassword);
273  return $this;
274  }
275 
281  public function findEmailDuplicates()
282  {
283  $connection = $this->getConnection();
284  $select = $connection->select()->from(
285  $this->getTable('customer_entity'),
286  ['email', 'cnt' => 'COUNT(*)']
287  )->group(
288  'email'
289  )->order(
290  'cnt DESC'
291  )->limit(
292  1
293  );
294  $lookup = $connection->fetchRow($select);
295  if (empty($lookup)) {
296  return false;
297  }
298  return $lookup['cnt'] > 1;
299  }
300 
307  public function checkCustomerId($customerId)
308  {
309  $connection = $this->getConnection();
310  $bind = ['entity_id' => (int)$customerId];
311  $select = $connection->select()->from(
312  $this->getTable('customer_entity'),
313  'entity_id'
314  )->where(
315  'entity_id = :entity_id'
316  )->limit(
317  1
318  );
319 
320  $result = $connection->fetchOne($select, $bind);
321  if ($result) {
322  return true;
323  }
324  return false;
325  }
326 
333  public function getWebsiteId($customerId)
334  {
335  $connection = $this->getConnection();
336  $bind = ['entity_id' => (int)$customerId];
337  $select = $connection->select()->from(
338  $this->getTable('customer_entity'),
339  'website_id'
340  )->where(
341  'entity_id = :entity_id'
342  );
343 
344  return $connection->fetchOne($select, $bind);
345  }
346 
353  public function setNewIncrementId(\Magento\Framework\DataObject $object)
354  {
355  if ($this->_scopeConfig->getValue(
357  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
358  )
359  ) {
360  parent::setNewIncrementId($object);
361  }
362  return $this;
363  }
364 
374  public function changeResetPasswordLinkToken(\Magento\Customer\Model\Customer $customer, $passwordLinkToken)
375  {
376  if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
377  $customer->setRpToken($passwordLinkToken);
378  $customer->setRpTokenCreatedAt(
379  (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
380  );
381  }
382  return $this;
383  }
384 }
loadByEmail(\Magento\Customer\Model\Customer $customer, $email)
Definition: Customer.php:232
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
changeResetPasswordLinkToken(\Magento\Customer\Model\Customer $customer, $passwordLinkToken)
Definition: Customer.php:374
load($object, $entityId, $attributes=[])
changePassword(\Magento\Customer\Model\Customer $customer, $newPassword)
Definition: Customer.php:270
__()
Definition: __.php:13
_afterSave(\Magento\Framework\DataObject $customer)
Definition: Customer.php:198
__construct(\Magento\Eav\Model\Entity\Context $context, \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Validator\Factory $validatorFactory, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Store\Model\StoreManagerInterface $storeManager, $data=[])
Definition: Customer.php:60
$connection
Definition: bulk.php:13
setNewIncrementId(\Magento\Framework\DataObject $object)
Definition: Customer.php:353