17
17
use Magento \Customer \Model \Config \Share as ConfigShare ;
18
18
use Magento \Customer \Model \Customer as CustomerModel ;
19
19
use Magento \Customer \Model \Customer \CredentialsValidator ;
20
+ use Magento \Customer \Model \ForgotPasswordToken \GetCustomerByToken ;
20
21
use Magento \Customer \Model \Metadata \Validator ;
21
22
use Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory ;
22
23
use Magento \Directory \Model \AllowedCountries ;
44
45
use Magento \Framework \Intl \DateTimeFactory ;
45
46
use Magento \Framework \Mail \Template \TransportBuilder ;
46
47
use Magento \Framework \Math \Random ;
47
- use Magento \Framework \Phrase ;
48
48
use Magento \Framework \Reflection \DataObjectProcessor ;
49
49
use Magento \Framework \Registry ;
50
50
use Magento \Framework \Session \SaveHandlerInterface ;
@@ -345,6 +345,11 @@ class AccountManagement implements AccountManagementInterface
345
345
*/
346
346
private $ allowedCountriesReader ;
347
347
348
+ /**
349
+ * @var GetCustomerByToken
350
+ */
351
+ private $ getByToken ;
352
+
348
353
/**
349
354
* @param CustomerFactory $customerFactory
350
355
* @param ManagerInterface $eventManager
@@ -377,10 +382,12 @@ class AccountManagement implements AccountManagementInterface
377
382
* @param CollectionFactory|null $visitorCollectionFactory
378
383
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
379
384
* @param AddressRegistry|null $addressRegistry
385
+ * @param GetCustomerByToken|null $getByToken
380
386
* @param AllowedCountries|null $allowedCountriesReader
387
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
381
388
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
382
389
* @SuppressWarnings(PHPMD.NPathComplexity)
383
- * @SuppressWarnings(PHPMD.CyclomaticComplexity )
390
+ * @SuppressWarnings(PHPMD.LongVariable )
384
391
*/
385
392
public function __construct (
386
393
CustomerFactory $ customerFactory ,
@@ -414,6 +421,7 @@ public function __construct(
414
421
CollectionFactory $ visitorCollectionFactory = null ,
415
422
SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
416
423
AddressRegistry $ addressRegistry = null ,
424
+ GetCustomerByToken $ getByToken = null ,
417
425
AllowedCountries $ allowedCountriesReader = null
418
426
) {
419
427
$ this ->customerFactory = $ customerFactory ;
@@ -439,23 +447,26 @@ public function __construct(
439
447
$ this ->customerModel = $ customerModel ;
440
448
$ this ->objectFactory = $ objectFactory ;
441
449
$ this ->extensibleDataObjectConverter = $ extensibleDataObjectConverter ;
450
+ $ objectManager = ObjectManager::getInstance ();
442
451
$ this ->credentialsValidator =
443
- $ credentialsValidator ?: ObjectManager:: getInstance () ->get (CredentialsValidator::class);
444
- $ this ->dateTimeFactory = $ dateTimeFactory ?: ObjectManager:: getInstance () ->get (DateTimeFactory::class);
445
- $ this ->accountConfirmation = $ accountConfirmation ?: ObjectManager:: getInstance ()
452
+ $ credentialsValidator ?: $ objectManager ->get (CredentialsValidator::class);
453
+ $ this ->dateTimeFactory = $ dateTimeFactory ?: $ objectManager ->get (DateTimeFactory::class);
454
+ $ this ->accountConfirmation = $ accountConfirmation ?: $ objectManager
446
455
->get (AccountConfirmation::class);
447
456
$ this ->sessionManager = $ sessionManager
448
- ?: ObjectManager:: getInstance () ->get (SessionManagerInterface::class);
457
+ ?: $ objectManager ->get (SessionManagerInterface::class);
449
458
$ this ->saveHandler = $ saveHandler
450
- ?: ObjectManager:: getInstance () ->get (SaveHandlerInterface::class);
459
+ ?: $ objectManager ->get (SaveHandlerInterface::class);
451
460
$ this ->visitorCollectionFactory = $ visitorCollectionFactory
452
- ?: ObjectManager:: getInstance () ->get (CollectionFactory::class);
461
+ ?: $ objectManager ->get (CollectionFactory::class);
453
462
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder
454
- ?: ObjectManager:: getInstance () ->get (SearchCriteriaBuilder::class);
463
+ ?: $ objectManager ->get (SearchCriteriaBuilder::class);
455
464
$ this ->addressRegistry = $ addressRegistry
456
- ?: ObjectManager::getInstance ()->get (AddressRegistry::class);
465
+ ?: $ objectManager ->get (AddressRegistry::class);
466
+ $ this ->getByToken = $ getByToken
467
+ ?: $ objectManager ->get (GetCustomerByToken::class);
457
468
$ this ->allowedCountriesReader = $ allowedCountriesReader
458
- ?: ObjectManager:: getInstance () ->get (AllowedCountries::class);
469
+ ?: $ objectManager ->get (AllowedCountries::class);
459
470
}
460
471
461
472
/**
@@ -521,8 +532,11 @@ public function activateById($customerId, $confirmationKey)
521
532
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
522
533
* @param string $confirmationKey
523
534
* @return \Magento\Customer\Api\Data\CustomerInterface
524
- * @throws \Magento\Framework\Exception\State\InvalidTransitionException
525
- * @throws \Magento\Framework\Exception\State\InputMismatchException
535
+ * @throws InputException
536
+ * @throws InputMismatchException
537
+ * @throws InvalidTransitionException
538
+ * @throws LocalizedException
539
+ * @throws NoSuchEntityException
526
540
*/
527
541
private function activateCustomer ($ customer , $ confirmationKey )
528
542
{
@@ -630,42 +644,6 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
630
644
return false ;
631
645
}
632
646
633
- /**
634
- * Match a customer by their RP token.
635
- *
636
- * @param string $rpToken
637
- * @throws ExpiredException
638
- * @throws NoSuchEntityException
639
- * @return CustomerInterface
640
- * @throws LocalizedException
641
- */
642
- private function matchCustomerByRpToken (string $ rpToken ): CustomerInterface
643
- {
644
- $ this ->searchCriteriaBuilder ->addFilter (
645
- 'rp_token ' ,
646
- $ rpToken
647
- );
648
- $ this ->searchCriteriaBuilder ->setPageSize (1 );
649
- $ found = $ this ->customerRepository ->getList (
650
- $ this ->searchCriteriaBuilder ->create ()
651
- );
652
- if ($ found ->getTotalCount () > 1 ) {
653
- //Failed to generated unique RP token
654
- throw new ExpiredException (
655
- new Phrase ('Reset password token expired. ' )
656
- );
657
- }
658
- if ($ found ->getTotalCount () === 0 ) {
659
- //Customer with such token not found.
660
- throw NoSuchEntityException::singleField (
661
- 'rp_token ' ,
662
- $ rpToken
663
- );
664
- }
665
- //Unique customer found.
666
- return $ found ->getItems ()[0 ];
667
- }
668
-
669
647
/**
670
648
* Handle not supported template
671
649
*
@@ -691,7 +669,7 @@ private function handleUnknownTemplate($template)
691
669
public function resetPassword ($ email , $ resetToken , $ newPassword )
692
670
{
693
671
if (!$ email ) {
694
- $ customer = $ this ->matchCustomerByRpToken ($ resetToken );
672
+ $ customer = $ this ->getByToken -> execute ($ resetToken );
695
673
$ email = $ customer ->getEmail ();
696
674
} else {
697
675
$ customer = $ this ->customerRepository ->get ($ email );
@@ -830,6 +808,8 @@ public function getConfirmationStatus($customerId)
830
808
831
809
/**
832
810
* @inheritdoc
811
+ *
812
+ * @throws LocalizedException
833
813
*/
834
814
public function createAccount (CustomerInterface $ customer , $ password = null , $ redirectUrl = '' )
835
815
{
@@ -852,6 +832,8 @@ public function createAccount(CustomerInterface $customer, $password = null, $re
852
832
853
833
/**
854
834
* @inheritdoc
835
+ *
836
+ * @throws InputMismatchException
855
837
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
856
838
* @SuppressWarnings(PHPMD.NPathComplexity)
857
839
*/
@@ -987,6 +969,8 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
987
969
988
970
/**
989
971
* @inheritdoc
972
+ *
973
+ * @throws InvalidEmailOrPasswordException
990
974
*/
991
975
public function changePassword ($ email , $ currentPassword , $ newPassword )
992
976
{
@@ -1000,6 +984,8 @@ public function changePassword($email, $currentPassword, $newPassword)
1000
984
1001
985
/**
1002
986
* @inheritdoc
987
+ *
988
+ * @throws InvalidEmailOrPasswordException
1003
989
*/
1004
990
public function changePasswordById ($ customerId , $ currentPassword , $ newPassword )
1005
991
{
@@ -1137,12 +1123,14 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
1137
1123
*
1138
1124
* @param int $customerId
1139
1125
* @param string $resetPasswordLinkToken
1126
+ *
1140
1127
* @return bool
1141
- * @throws \Magento\Framework\Exception\State\InputMismatchException If token is mismatched
1142
- * @throws \Magento\Framework\Exception\State\ExpiredException If token is expired
1143
- * @throws \Magento\Framework\Exception\InputException If token or customer id is invalid
1144
- * @throws \Magento\Framework\Exception\NoSuchEntityException If customer doesn't exist
1128
+ * @throws ExpiredException If token is expired
1129
+ * @throws InputException If token or customer id is invalid
1130
+ * @throws InputMismatchException If token is mismatched
1145
1131
* @throws LocalizedException
1132
+ * @throws NoSuchEntityException If customer doesn't exist
1133
+ * @SuppressWarnings(PHPMD.LongVariable)
1146
1134
*/
1147
1135
private function validateResetPasswordToken ($ customerId , $ resetPasswordLinkToken )
1148
1136
{
@@ -1157,7 +1145,8 @@ private function validateResetPasswordToken($customerId, $resetPasswordLinkToken
1157
1145
1158
1146
if ($ customerId === null ) {
1159
1147
//Looking for the customer.
1160
- $ customerId = $ this ->matchCustomerByRpToken ($ resetPasswordLinkToken )
1148
+ $ customerId = $ this ->getByToken
1149
+ ->execute ($ resetPasswordLinkToken )
1161
1150
->getId ();
1162
1151
}
1163
1152
if (!is_string ($ resetPasswordLinkToken ) || empty ($ resetPasswordLinkToken )) {
0 commit comments