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
*
@@ -674,15 +652,17 @@ private function matchCustomerByRpToken(string $rpToken): CustomerInterface
674
652
*/
675
653
private function handleUnknownTemplate ($ template )
676
654
{
677
- throw new InputException (__ (
678
- 'Invalid value of "%value" provided for the %fieldName field. Possible values: %template1 or %template2. ' ,
679
- [
680
- 'value ' => $ template ,
681
- 'fieldName ' => 'template ' ,
682
- 'template1 ' => AccountManagement::EMAIL_REMINDER ,
683
- 'template2 ' => AccountManagement::EMAIL_RESET
684
- ]
685
- ));
655
+ throw new InputException (
656
+ __ (
657
+ 'Invalid value of "%value" provided for the %fieldName field. Possible values: %template1 or %template2. ' ,
658
+ [
659
+ 'value ' => $ template ,
660
+ 'fieldName ' => 'template ' ,
661
+ 'template1 ' => AccountManagement::EMAIL_REMINDER ,
662
+ 'template2 ' => AccountManagement::EMAIL_RESET
663
+ ]
664
+ )
665
+ );
686
666
}
687
667
688
668
/**
@@ -691,7 +671,7 @@ private function handleUnknownTemplate($template)
691
671
public function resetPassword ($ email , $ resetToken , $ newPassword )
692
672
{
693
673
if (!$ email ) {
694
- $ customer = $ this ->matchCustomerByRpToken ($ resetToken );
674
+ $ customer = $ this ->getByToken -> execute ($ resetToken );
695
675
$ email = $ customer ->getEmail ();
696
676
} else {
697
677
$ customer = $ this ->customerRepository ->get ($ email );
@@ -830,6 +810,8 @@ public function getConfirmationStatus($customerId)
830
810
831
811
/**
832
812
* @inheritdoc
813
+ *
814
+ * @throws LocalizedException
833
815
*/
834
816
public function createAccount (CustomerInterface $ customer , $ password = null , $ redirectUrl = '' )
835
817
{
@@ -852,6 +834,8 @@ public function createAccount(CustomerInterface $customer, $password = null, $re
852
834
853
835
/**
854
836
* @inheritdoc
837
+ *
838
+ * @throws InputMismatchException
855
839
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
856
840
* @SuppressWarnings(PHPMD.NPathComplexity)
857
841
*/
@@ -987,6 +971,8 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
987
971
988
972
/**
989
973
* @inheritdoc
974
+ *
975
+ * @throws InvalidEmailOrPasswordException
990
976
*/
991
977
public function changePassword ($ email , $ currentPassword , $ newPassword )
992
978
{
@@ -1000,6 +986,8 @@ public function changePassword($email, $currentPassword, $newPassword)
1000
986
1001
987
/**
1002
988
* @inheritdoc
989
+ *
990
+ * @throws InvalidEmailOrPasswordException
1003
991
*/
1004
992
public function changePasswordById ($ customerId , $ currentPassword , $ newPassword )
1005
993
{
@@ -1137,12 +1125,14 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
1137
1125
*
1138
1126
* @param int $customerId
1139
1127
* @param string $resetPasswordLinkToken
1128
+ *
1140
1129
* @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
1130
+ * @throws ExpiredException If token is expired
1131
+ * @throws InputException If token or customer id is invalid
1132
+ * @throws InputMismatchException If token is mismatched
1145
1133
* @throws LocalizedException
1134
+ * @throws NoSuchEntityException If customer doesn't exist
1135
+ * @SuppressWarnings(PHPMD.LongVariable)
1146
1136
*/
1147
1137
private function validateResetPasswordToken ($ customerId , $ resetPasswordLinkToken )
1148
1138
{
@@ -1157,7 +1147,8 @@ private function validateResetPasswordToken($customerId, $resetPasswordLinkToken
1157
1147
1158
1148
if ($ customerId === null ) {
1159
1149
//Looking for the customer.
1160
- $ customerId = $ this ->matchCustomerByRpToken ($ resetPasswordLinkToken )
1150
+ $ customerId = $ this ->getByToken
1151
+ ->execute ($ resetPasswordLinkToken )
1161
1152
->getId ();
1162
1153
}
1163
1154
if (!is_string ($ resetPasswordLinkToken ) || empty ($ resetPasswordLinkToken )) {
@@ -1325,13 +1316,20 @@ protected function sendEmailTemplate(
1325
1316
}
1326
1317
1327
1318
$ transport = $ this ->transportBuilder ->setTemplateIdentifier ($ templateId )
1328
- ->setTemplateOptions (['area ' => Area::AREA_FRONTEND , 'store ' => $ storeId ])
1319
+ ->setTemplateOptions (
1320
+ [
1321
+ 'area ' => Area::AREA_FRONTEND ,
1322
+ 'store ' => $ storeId
1323
+ ]
1324
+ )
1329
1325
->setTemplateVars ($ templateParams )
1330
- ->setFrom ($ this ->scopeConfig ->getValue (
1331
- $ sender ,
1332
- ScopeInterface::SCOPE_STORE ,
1333
- $ storeId
1334
- ))
1326
+ ->setFrom (
1327
+ $ this ->scopeConfig ->getValue (
1328
+ $ sender ,
1329
+ ScopeInterface::SCOPE_STORE ,
1330
+ $ storeId
1331
+ )
1332
+ )
1335
1333
->addTo ($ email , $ this ->customerViewHelper ->getCustomerName ($ customer ))
1336
1334
->getTransport ();
1337
1335
0 commit comments