-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Customer.php
1385 lines (1244 loc) · 36.7 KB
/
Customer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Indexer\StateInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Math\Random;
/**
* Customer model
*
* @api
* @method int getWebsiteId() getWebsiteId()
* @method Customer setWebsiteId($value)
* @method int getStoreId() getStoreId()
* @method string getEmail() getEmail()
* @method mixed getDisableAutoGroupChange()
* @method Customer setDisableAutoGroupChange($value)
* @method Customer setGroupId($value)
* @method Customer setDefaultBilling($value)
* @method Customer setDefaultShipping($value)
* @method Customer setPasswordHash($string)
* @method string getPasswordHash()
* @method string getConfirmation()
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Customer extends \Magento\Framework\Model\AbstractModel
{
/**
* Configuration paths for email templates and identities
*/
const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';
const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';
const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';
const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';
/**
* @deprecated
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
*/
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';
const XML_PATH_GENERATE_HUMAN_FRIENDLY_ID = 'customer/create_account/generate_human_friendly_id';
const SUBSCRIBED_YES = 'yes';
const SUBSCRIBED_NO = 'no';
const ENTITY = 'customer';
const CUSTOMER_GRID_INDEXER_ID = 'customer_grid';
/**
* Configuration path to expiration period of reset password link
*/
const XML_PATH_CUSTOMER_RESET_PASSWORD_LINK_EXPIRATION_PERIOD = 'customer/password/reset_link_expiration_period';
/**
* Model event prefix
*
* @var string
*/
protected $_eventPrefix = 'customer';
/**
* Name of the event object
*
* @var string
*/
protected $_eventObject = 'customer';
/**
* List of errors
*
* @var array
*/
protected $_errors = [];
/**
* Assoc array of customer attributes
*
* @var array
*/
protected $_attributes;
/**
* Customer addresses collection
*
* @var \Magento\Customer\Model\ResourceModel\Address\Collection
*/
protected $_addressesCollection;
/**
* Is model deletable
*
* @var boolean
*/
protected $_isDeleteable = true;
/**
* Is model readonly
*
* @var boolean
*/
protected $_isReadonly = false;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Eav\Model\Config
*/
protected $_config;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @var Share
*/
protected $_configShare;
/**
* @var AddressFactory
*/
protected $_addressFactory;
/**
* @var CollectionFactory
*/
protected $_addressesFactory;
/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
/**
* @var GroupRepositoryInterface
*/
protected $_groupRepository;
/**
* @var \Magento\Framework\Encryption\EncryptorInterface
*/
protected $_encryptor;
/**
* @var Random
*/
protected $mathRandom;
/**
* @var \Magento\Framework\Stdlib\DateTime
*/
protected $dateTime;
/**
* @var CustomerInterfaceFactory
*/
protected $customerDataFactory;
/**
* @var DataObjectProcessor
*/
protected $dataObjectProcessor;
/**
* @var \Magento\Framework\Api\DataObjectHelper
*/
protected $dataObjectHelper;
/**
* @var \Magento\Customer\Api\CustomerMetadataInterface
*/
protected $metadataService;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
/**
* @var AccountConfirmation
*/
private $accountConfirmation;
/**
* Caching property to store customer address data models by the address ID.
*
* @var array
*/
private $storedAddress;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Eav\Model\Config $config
* @param ScopeConfigInterface $scopeConfig
* @param ResourceCustomer $resource
* @param Share $configShare
* @param AddressFactory $addressFactory
* @param CollectionFactory $addressesFactory
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param GroupRepositoryInterface $groupRepository
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param CustomerInterfaceFactory $customerDataFactory
* @param DataObjectProcessor $dataObjectProcessor
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
* @param CustomerMetadataInterface $metadataService
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @param AccountConfirmation|null $accountConfirmation
* @param Random|null $mathRandom
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Eav\Model\Config $config,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Customer\Model\ResourceModel\Customer $resource,
\Magento\Customer\Model\Config\Share $configShare,
\Magento\Customer\Model\AddressFactory $addressFactory,
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressesFactory,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
GroupRepositoryInterface $groupRepository,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\Stdlib\DateTime $dateTime,
CustomerInterfaceFactory $customerDataFactory,
DataObjectProcessor $dataObjectProcessor,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
\Magento\Customer\Api\CustomerMetadataInterface $metadataService,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
AccountConfirmation $accountConfirmation = null,
Random $mathRandom = null
) {
$this->metadataService = $metadataService;
$this->_scopeConfig = $scopeConfig;
$this->_storeManager = $storeManager;
$this->_config = $config;
$this->_configShare = $configShare;
$this->_addressFactory = $addressFactory;
$this->_addressesFactory = $addressesFactory;
$this->_transportBuilder = $transportBuilder;
$this->_groupRepository = $groupRepository;
$this->_encryptor = $encryptor;
$this->dateTime = $dateTime;
$this->customerDataFactory = $customerDataFactory;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->dataObjectHelper = $dataObjectHelper;
$this->indexerRegistry = $indexerRegistry;
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
->get(AccountConfirmation::class);
$this->mathRandom = $mathRandom ?: ObjectManager::getInstance()->get(Random::class);
parent::__construct(
$context,
$registry,
$resource,
$resourceCollection,
$data
);
}
/**
* Initialize customer model
*
* @return void
*/
public function _construct()
{
$this->_init(\Magento\Customer\Model\ResourceModel\Customer::class);
}
/**
* Retrieve customer model with customer data
*
* @return \Magento\Customer\Api\Data\CustomerInterface
*/
public function getDataModel()
{
$customerData = $this->getData();
$addressesData = [];
/** @var \Magento\Customer\Model\Address $address */
foreach ($this->getAddresses() as $address) {
if (!isset($this->storedAddress[$address->getId()])) {
$this->storedAddress[$address->getId()] = $address->getDataModel();
}
$addressesData[] = $this->storedAddress[$address->getId()];
}
$customerDataObject = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$customerDataObject,
$customerData,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$customerDataObject->setAddresses($addressesData)
->setId($this->getId());
return $customerDataObject;
}
/**
* Update customer data
*
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
* @return $this
*/
public function updateData($customer)
{
$customerDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
$customer,
\Magento\Customer\Api\Data\CustomerInterface::class
);
foreach ($customerDataAttributes as $attributeCode => $attributeData) {
if ($attributeCode == 'password') {
continue;
}
$this->setDataUsingMethod($attributeCode, $attributeData);
}
$customAttributes = $customer->getCustomAttributes();
if ($customAttributes !== null) {
foreach ($customAttributes as $attribute) {
$this->setData($attribute->getAttributeCode(), $attribute->getValue());
}
}
$customerId = $customer->getId();
if ($customerId) {
$this->setId($customerId);
}
return $this;
}
/**
* Retrieve customer sharing configuration model
*
* @return Share
*/
public function getSharingConfig()
{
return $this->_configShare;
}
/**
* Authenticate customer
*
* @param string $login
* @param string $password
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* Use \Magento\Customer\Api\AccountManagementInterface::authenticate
*/
public function authenticate($login, $password)
{
$this->loadByEmail($login);
if ($this->getConfirmation() &&
$this->accountConfirmation->isConfirmationRequired($this->getWebsiteId(), $this->getId(), $this->getEmail())
) {
throw new EmailNotConfirmedException(
__("This account isn't confirmed. Verify and try again.")
);
}
if (!$this->validatePassword($password)) {
throw new InvalidEmailOrPasswordException(
__('Invalid login or password.')
);
}
$this->_eventManager->dispatch(
'customer_customer_authenticated',
['model' => $this, 'password' => $password]
);
return true;
}
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadByEmail($customerEmail)
{
$this->_getResource()->loadByEmail($this, $customerEmail);
return $this;
}
/**
* Change customer password
*
* @param string $newPassword
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function changePassword($newPassword)
{
$this->_getResource()->changePassword($this, $newPassword);
return $this;
}
/**
* Get full customer name
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getName()
{
$name = '';
if ($this->_config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
$name .= $this->getPrefix() . ' ';
}
$name .= $this->getFirstname();
if ($this->_config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
$name .= ' ' . $this->getMiddlename();
}
$name .= ' ' . $this->getLastname();
if ($this->_config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
$name .= ' ' . $this->getSuffix();
}
return $name;
}
/**
* Add address to address collection
*
* @param Address $address
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function addAddress(Address $address)
{
$this->getAddressesCollection()->addItem($address);
return $this;
}
/**
* Retrieve customer address by address id
*
* @param int $addressId
* @return Address
*/
public function getAddressById($addressId)
{
return $this->_createAddressInstance()->load($addressId);
}
/**
* Getting customer address object from collection by identifier
*
* @param int $addressId
* @return Address
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAddressItemById($addressId)
{
return $this->getAddressesCollection()->getItemById($addressId);
}
/**
* Retrieve not loaded address collection
*
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
*/
public function getAddressCollection()
{
return $this->_createAddressCollection();
}
/**
* Customer addresses collection
*
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAddressesCollection()
{
if ($this->_addressesCollection === null) {
$this->_addressesCollection = $this->getAddressCollection()->setCustomerFilter(
$this
)->addAttributeToSelect(
'*'
);
foreach ($this->_addressesCollection as $address) {
$address->setCustomer($this);
}
}
return $this->_addressesCollection;
}
/**
* Retrieve customer address array
*
* @return \Magento\Framework\DataObject[]
*/
public function getAddresses()
{
return $this->getAddressesCollection()->getItems();
}
/**
* Retrieve all customer attributes
*
* @return Attribute[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAttributes()
{
if ($this->_attributes === null) {
$this->_attributes = $this->_getResource()->loadAllAttributes($this)->getSortedAttributes();
}
return $this->_attributes;
}
/**
* Get customer attribute model object
*
* @param string $attributeCode
* @return \Magento\Customer\Model\ResourceModel\Attribute | null
*/
public function getAttribute($attributeCode)
{
$this->getAttributes();
if (isset($this->_attributes[$attributeCode])) {
return $this->_attributes[$attributeCode];
}
return null;
}
/**
* Set plain and hashed password
*
* @param string $password
* @return $this
*/
public function setPassword($password)
{
$this->setData('password', $password);
$this->setPasswordHash($this->hashPassword($password));
return $this;
}
/**
* Hash customer password
*
* @param string $password
* @param bool|int|string $salt
* @return string
*/
public function hashPassword($password, $salt = true)
{
return $this->_encryptor->getHash($password, $salt);
}
/**
* Validate password with salted hash
*
* @param string $password
* @return boolean
* @throws \Exception
*/
public function validatePassword($password)
{
$hash = $this->getPasswordHash();
if (!$hash) {
return false;
}
return $this->_encryptor->validateHash($password, $hash);
}
/**
* Encrypt password
*
* @param string $password
* @return string
*/
public function encryptPassword($password)
{
return $this->_encryptor->encrypt($password);
}
/**
* Decrypt password
*
* @param string $password
* @return string
*/
public function decryptPassword($password)
{
return $this->_encryptor->decrypt($password);
}
/**
* Retrieve default address by type(attribute)
*
* @param string $attributeCode address type attribute code
* @return Address|false
*/
public function getPrimaryAddress($attributeCode)
{
$primaryAddress = $this->getAddressesCollection()->getItemById($this->getData($attributeCode));
return $primaryAddress ? $primaryAddress : false;
}
/**
* Get customer default billing address
*
* @return Address
*/
public function getPrimaryBillingAddress()
{
return $this->getPrimaryAddress('default_billing');
}
/**
* Get customer default billing address
*
* @return Address
*/
public function getDefaultBillingAddress()
{
return $this->getPrimaryBillingAddress();
}
/**
* Get default customer shipping address
*
* @return Address
*/
public function getPrimaryShippingAddress()
{
return $this->getPrimaryAddress('default_shipping');
}
/**
* Get default customer shipping address
*
* @return Address
*/
public function getDefaultShippingAddress()
{
return $this->getPrimaryShippingAddress();
}
/**
* Retrieve ids of default addresses
*
* @return array
*/
public function getPrimaryAddressIds()
{
$ids = [];
if ($this->getDefaultBilling()) {
$ids[] = $this->getDefaultBilling();
}
if ($this->getDefaultShipping()) {
$ids[] = $this->getDefaultShipping();
}
return $ids;
}
/**
* Retrieve all customer default addresses
*
* @return Address[]
*/
public function getPrimaryAddresses()
{
$addresses = [];
$primaryBilling = $this->getPrimaryBillingAddress();
if ($primaryBilling) {
$addresses[] = $primaryBilling;
$primaryBilling->setIsPrimaryBilling(true);
}
$primaryShipping = $this->getPrimaryShippingAddress();
if ($primaryShipping) {
if ($primaryBilling && $primaryBilling->getId() == $primaryShipping->getId()) {
$primaryBilling->setIsPrimaryShipping(true);
} else {
$primaryShipping->setIsPrimaryShipping(true);
$addresses[] = $primaryShipping;
}
}
return $addresses;
}
/**
* Retrieve not default addresses
*
* @return Address[]
*/
public function getAdditionalAddresses()
{
$addresses = [];
$primatyIds = $this->getPrimaryAddressIds();
foreach ($this->getAddressesCollection() as $address) {
if (!in_array($address->getId(), $primatyIds)) {
$addresses[] = $address;
}
}
return $addresses;
}
/**
* Check if address is primary
*
* @param Address $address
* @return boolean
*/
public function isAddressPrimary(Address $address)
{
if (!$address->getId()) {
return false;
}
return $address->getId() == $this->getDefaultBilling() || $address->getId() == $this->getDefaultShipping();
}
/**
* Send email with new account related information
*
* @param string $type
* @param string $backUrl
* @param string $storeId
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeId = '0')
{
$types = $this->getTemplateTypes();
if (!isset($types[$type])) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The transactional account email type is incorrect. Verify and try again.')
);
}
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId($this->getSendemailStoreId());
}
$this->_sendEmailTemplate(
$types[$type],
self::XML_PATH_REGISTER_EMAIL_IDENTITY,
['customer' => $this, 'back_url' => $backUrl, 'store' => $this->getStore()],
$storeId
);
return $this;
}
/**
* Check if accounts confirmation is required in config
*
* @return bool
* @deprecated
* @see AccountConfirmation::isConfirmationRequired
*/
public function isConfirmationRequired()
{
$websiteId = $this->getWebsiteId() ? $this->getWebsiteId() : null;
return $this->accountConfirmation->isConfirmationRequired($websiteId, $this->getId(), $this->getEmail());
}
/**
* Generate random confirmation key
*
* @return string
*/
public function getRandomConfirmationKey()
{
return $this->mathRandom->getRandomString(32);
}
/**
* Send email with new customer password
*
* @return $this
*/
public function sendPasswordReminderEmail()
{
$this->_sendEmailTemplate(
self::XML_PATH_REMIND_EMAIL_TEMPLATE,
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
['customer' => $this, 'store' => $this->getStore()],
$this->getStoreId()
);
return $this;
}
/**
* Send corresponding email template
*
* @param string $template configuration path of email template
* @param string $sender configuration path of email identity
* @param array $templateParams
* @param int|null $storeId
* @return $this
*/
protected function _sendEmailTemplate($template, $sender, $templateParams = [], $storeId = null)
{
/** @var \Magento\Framework\Mail\TransportInterface $transport */
$transport = $this->_transportBuilder->setTemplateIdentifier(
$this->_scopeConfig->getValue($template, ScopeInterface::SCOPE_STORE, $storeId)
)->setTemplateOptions(
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
)->setTemplateVars(
$templateParams
)->setFrom(
$this->_scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId)
)->addTo(
$this->getEmail(),
$this->getName()
)->getTransport();
$transport->sendMessage();
return $this;
}
/**
* Send email with reset password confirmation link
*
* @return $this
*/
public function sendPasswordResetConfirmationEmail()
{
$storeId = $this->getStoreId();
if (!$storeId) {
$storeId = $this->_getWebsiteStoreId();
}
$this->_sendEmailTemplate(
self::XML_PATH_FORGOT_EMAIL_TEMPLATE,
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
['customer' => $this, 'store' => $this->getStore()],
$storeId
);
return $this;
}
/**
* Retrieve customer group identifier
*
* @return int
*/
public function getGroupId()
{
if (!$this->hasData('group_id')) {
$storeId = $this->getStoreId() ? $this->getStoreId() : $this->_storeManager->getStore()->getId();
$groupId = $this->_scopeConfig->getValue(
GroupManagement::XML_PATH_DEFAULT_ID,
ScopeInterface::SCOPE_STORE,
$storeId
);
$this->setData('group_id', $groupId);
}
return $this->getData('group_id');
}
/**
* Retrieve customer tax class identifier
*
* @return int
*/
public function getTaxClassId()
{
if (!$this->getData('tax_class_id')) {
$groupTaxClassId = $this->_groupRepository->getById($this->getGroupId())->getTaxClassId();
$this->setData('tax_class_id', $groupTaxClassId);
}
return $this->getData('tax_class_id');
}
/**
* Retrieve store where customer was created
*
* @return \Magento\Store\Model\Store
*/
public function getStore()
{
return $this->_storeManager->getStore($this->getStoreId());
}
/**
* Retrieve shared store ids
*
* @return array
*/
public function getSharedStoreIds()
{
$ids = $this->_getData('shared_store_ids');
if ($ids === null) {
$ids = [];
if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
$ids = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
} else {
foreach ($this->_storeManager->getStores() as $store) {
$ids[] = $store->getId();
}
}
$this->setData('shared_store_ids', $ids);
}
return $ids;
}
/**
* Retrieve shared website ids
*
* @return int[]
*/
public function getSharedWebsiteIds()
{
$ids = $this->_getData('shared_website_ids');
if ($ids === null) {
$ids = [];
if ((bool)$this->getSharingConfig()->isWebsiteScope()) {
$ids[] = $this->getWebsiteId();
} else {
foreach ($this->_storeManager->getWebsites() as $website) {
$ids[] = $website->getId();
}
}
$this->setData('shared_website_ids', $ids);
}
return $ids;
}
/**
* Retrieve attribute set id for customer.
*
* @return int
*/
public function getAttributeSetId()
{
return parent::getAttributeSetId() ?: CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER;
}
/**
* Set store to customer
*
* @param \Magento\Store\Model\Store $store
* @return $this
*/
public function setStore(\Magento\Store\Model\Store $store)
{
$this->setStoreId($store->getId());
$this->setWebsiteId($store->getWebsite()->getId());