2424/**
2525 * @author Daniel West <daniel@silverback.is>
2626 */
27- class UserDataProcessor
27+ readonly class UserDataProcessor
2828{
2929 public function __construct (
30- private readonly UserPasswordHasherInterface $ passwordHasher ,
31- private readonly UserRepositoryInterface $ userRepository ,
32- private readonly PasswordHasherFactoryInterface $ passwordHasherFactory ,
33- private readonly bool $ initialEmailVerifiedState ,
34- private readonly bool $ verifyEmailOnRegister ,
35- private readonly bool $ verifyEmailOnChange ,
36- private readonly int $ tokenTtl = 8600 ,
30+ private UserPasswordHasherInterface $ passwordHasher ,
31+ private UserRepositoryInterface $ userRepository ,
32+ private PasswordHasherFactoryInterface $ passwordHasherFactory ,
33+ private bool $ initialEmailVerifiedState ,
34+ private bool $ verifyEmailOnRegister ,
35+ private bool $ verifyEmailOnChange ,
36+ private int $ tokenTtl = 8600 ,
3737 ) {
3838 }
3939
@@ -83,23 +83,21 @@ public function processChanges(AbstractUser $user, ?AbstractUser $previousUser):
8383 if (!$ previousUser ) {
8484 $ user ->setEmailAddressVerified ($ this ->initialEmailVerifiedState );
8585 if (!$ this ->initialEmailVerifiedState && $ this ->verifyEmailOnRegister ) {
86- $ user ->setEmailAddressVerifyToken ($ this ->passwordHasher ->hashPassword ($ user , $ token = TokenGenerator::generateToken ()));
87- $ user ->plainEmailAddressVerifyToken = $ token ;
86+ $ this ->setEmailAddressVerifyToken ($ user );
8887 }
8988 }
9089
9190 if ($ previousUser ) {
9291 if ($ this ->verifyEmailOnChange && $ user ->getEmailAddress () !== $ previousUser ->getEmailAddress ()) {
93- $ user ->setEmailAddressVerifyToken ($ this ->passwordHasher ->hashPassword ($ user , $ token = TokenGenerator::generateToken ()));
94- $ user ->plainEmailAddressVerifyToken = $ token ;
92+ $ this ->setEmailAddressVerifyToken ($ user );
9593 }
9694 if (($ newEmail = $ user ->getNewEmailAddress ()) !== $ previousUser ->getNewEmailAddress ()) {
9795 if ($ newEmail ) {
98- $ user ->setNewEmailConfirmationToken ($ this ->passwordHasher ->hashPassword ($ user , $ token = TokenGenerator::generateToken ()));
99- $ user ->plainNewEmailConfirmationToken = $ token ;
96+ $ this ->setNewEmailConfirmationToken ($ user );
10097 } else {
10198 // invalidate any existing requests
10299 $ user ->setNewEmailConfirmationToken (null );
100+ // revert any tokens for the new primary saved email address as this was converted from the update
103101 if ($ previousUser ->getNewEmailAddress () === $ user ->getEmailAddress () && $ user ->plainEmailAddressVerifyToken ) {
104102 $ user ->plainEmailAddressVerifyToken = null ;
105103 $ user ->setEmailAddressVerifyToken (null );
@@ -109,6 +107,18 @@ public function processChanges(AbstractUser $user, ?AbstractUser $previousUser):
109107 }
110108 }
111109
110+ public function setNewEmailConfirmationToken (AbstractUser $ user ): void
111+ {
112+ $ user ->setNewEmailConfirmationToken ($ this ->passwordHasher ->hashPassword ($ user , $ token = TokenGenerator::generateToken ()));
113+ $ user ->plainNewEmailConfirmationToken = $ token ;
114+ }
115+
116+ public function setEmailAddressVerifyToken (AbstractUser $ user ): void
117+ {
118+ $ user ->setEmailAddressVerifyToken ($ this ->passwordHasher ->hashPassword ($ user , $ token = TokenGenerator::generateToken ()));
119+ $ user ->plainEmailAddressVerifyToken = $ token ;
120+ }
121+
112122 private function hashPassword (AbstractUser $ entity ): void
113123 {
114124 if (!$ entity ->getPlainPassword ()) {
0 commit comments