1717use Magento \Customer \Model \Config \Share as ConfigShare ;
1818use Magento \Customer \Model \Customer as CustomerModel ;
1919use Magento \Customer \Model \Customer \CredentialsValidator ;
20+ use Magento \Customer \Model \ForgotPasswordToken \GetCustomerByToken ;
2021use Magento \Customer \Model \Metadata \Validator ;
2122use Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory ;
2223use Magento \Directory \Model \AllowedCountries ;
4445use Magento \Framework \Intl \DateTimeFactory ;
4546use Magento \Framework \Mail \Template \TransportBuilder ;
4647use Magento \Framework \Math \Random ;
47- use Magento \Framework \Phrase ;
4848use Magento \Framework \Reflection \DataObjectProcessor ;
4949use Magento \Framework \Registry ;
5050use Magento \Framework \Session \SaveHandlerInterface ;
@@ -345,6 +345,11 @@ class AccountManagement implements AccountManagementInterface
345345 */
346346 private $ allowedCountriesReader ;
347347
348+ /**
349+ * @var GetCustomerByToken
350+ */
351+ private $ getByToken ;
352+
348353 /**
349354 * @param CustomerFactory $customerFactory
350355 * @param ManagerInterface $eventManager
@@ -377,10 +382,12 @@ class AccountManagement implements AccountManagementInterface
377382 * @param CollectionFactory|null $visitorCollectionFactory
378383 * @param SearchCriteriaBuilder|null $searchCriteriaBuilder
379384 * @param AddressRegistry|null $addressRegistry
385+ * @param GetCustomerByToken|null $getByToken
380386 * @param AllowedCountries|null $allowedCountriesReader
387+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
381388 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
382389 * @SuppressWarnings(PHPMD.NPathComplexity)
383- * @SuppressWarnings(PHPMD.CyclomaticComplexity )
390+ * @SuppressWarnings(PHPMD.LongVariable )
384391 */
385392 public function __construct (
386393 CustomerFactory $ customerFactory ,
@@ -414,6 +421,7 @@ public function __construct(
414421 CollectionFactory $ visitorCollectionFactory = null ,
415422 SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
416423 AddressRegistry $ addressRegistry = null ,
424+ GetCustomerByToken $ getByToken = null ,
417425 AllowedCountries $ allowedCountriesReader = null
418426 ) {
419427 $ this ->customerFactory = $ customerFactory ;
@@ -439,23 +447,26 @@ public function __construct(
439447 $ this ->customerModel = $ customerModel ;
440448 $ this ->objectFactory = $ objectFactory ;
441449 $ this ->extensibleDataObjectConverter = $ extensibleDataObjectConverter ;
450+ $ objectManager = ObjectManager::getInstance ();
442451 $ 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
446455 ->get (AccountConfirmation::class);
447456 $ this ->sessionManager = $ sessionManager
448- ?: ObjectManager:: getInstance () ->get (SessionManagerInterface::class);
457+ ?: $ objectManager ->get (SessionManagerInterface::class);
449458 $ this ->saveHandler = $ saveHandler
450- ?: ObjectManager:: getInstance () ->get (SaveHandlerInterface::class);
459+ ?: $ objectManager ->get (SaveHandlerInterface::class);
451460 $ this ->visitorCollectionFactory = $ visitorCollectionFactory
452- ?: ObjectManager:: getInstance () ->get (CollectionFactory::class);
461+ ?: $ objectManager ->get (CollectionFactory::class);
453462 $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder
454- ?: ObjectManager:: getInstance () ->get (SearchCriteriaBuilder::class);
463+ ?: $ objectManager ->get (SearchCriteriaBuilder::class);
455464 $ this ->addressRegistry = $ addressRegistry
456- ?: ObjectManager::getInstance ()->get (AddressRegistry::class);
465+ ?: $ objectManager ->get (AddressRegistry::class);
466+ $ this ->getByToken = $ getByToken
467+ ?: $ objectManager ->get (GetCustomerByToken::class);
457468 $ this ->allowedCountriesReader = $ allowedCountriesReader
458- ?: ObjectManager:: getInstance () ->get (AllowedCountries::class);
469+ ?: $ objectManager ->get (AllowedCountries::class);
459470 }
460471
461472 /**
@@ -521,8 +532,11 @@ public function activateById($customerId, $confirmationKey)
521532 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
522533 * @param string $confirmationKey
523534 * @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
526540 */
527541 private function activateCustomer ($ customer , $ confirmationKey )
528542 {
@@ -630,42 +644,6 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
630644 return false ;
631645 }
632646
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-
669647 /**
670648 * Handle not supported template
671649 *
@@ -674,15 +652,17 @@ private function matchCustomerByRpToken(string $rpToken): CustomerInterface
674652 */
675653 private function handleUnknownTemplate ($ template )
676654 {
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+ );
686666 }
687667
688668 /**
@@ -691,7 +671,7 @@ private function handleUnknownTemplate($template)
691671 public function resetPassword ($ email , $ resetToken , $ newPassword )
692672 {
693673 if (!$ email ) {
694- $ customer = $ this ->matchCustomerByRpToken ($ resetToken );
674+ $ customer = $ this ->getByToken -> execute ($ resetToken );
695675 $ email = $ customer ->getEmail ();
696676 } else {
697677 $ customer = $ this ->customerRepository ->get ($ email );
@@ -830,6 +810,8 @@ public function getConfirmationStatus($customerId)
830810
831811 /**
832812 * @inheritdoc
813+ *
814+ * @throws LocalizedException
833815 */
834816 public function createAccount (CustomerInterface $ customer , $ password = null , $ redirectUrl = '' )
835817 {
@@ -852,6 +834,8 @@ public function createAccount(CustomerInterface $customer, $password = null, $re
852834
853835 /**
854836 * @inheritdoc
837+ *
838+ * @throws InputMismatchException
855839 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
856840 * @SuppressWarnings(PHPMD.NPathComplexity)
857841 */
@@ -987,6 +971,8 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
987971
988972 /**
989973 * @inheritdoc
974+ *
975+ * @throws InvalidEmailOrPasswordException
990976 */
991977 public function changePassword ($ email , $ currentPassword , $ newPassword )
992978 {
@@ -1000,6 +986,8 @@ public function changePassword($email, $currentPassword, $newPassword)
1000986
1001987 /**
1002988 * @inheritdoc
989+ *
990+ * @throws InvalidEmailOrPasswordException
1003991 */
1004992 public function changePasswordById ($ customerId , $ currentPassword , $ newPassword )
1005993 {
@@ -1137,12 +1125,14 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
11371125 *
11381126 * @param int $customerId
11391127 * @param string $resetPasswordLinkToken
1128+ *
11401129 * @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
11451133 * @throws LocalizedException
1134+ * @throws NoSuchEntityException If customer doesn't exist
1135+ * @SuppressWarnings(PHPMD.LongVariable)
11461136 */
11471137 private function validateResetPasswordToken ($ customerId , $ resetPasswordLinkToken )
11481138 {
@@ -1157,7 +1147,8 @@ private function validateResetPasswordToken($customerId, $resetPasswordLinkToken
11571147
11581148 if ($ customerId === null ) {
11591149 //Looking for the customer.
1160- $ customerId = $ this ->matchCustomerByRpToken ($ resetPasswordLinkToken )
1150+ $ customerId = $ this ->getByToken
1151+ ->execute ($ resetPasswordLinkToken )
11611152 ->getId ();
11621153 }
11631154 if (!is_string ($ resetPasswordLinkToken ) || empty ($ resetPasswordLinkToken )) {
@@ -1325,13 +1316,20 @@ protected function sendEmailTemplate(
13251316 }
13261317
13271318 $ 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+ )
13291325 ->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+ )
13351333 ->addTo ($ email , $ this ->customerViewHelper ->getCustomerName ($ customer ))
13361334 ->getTransport ();
13371335
0 commit comments