Guest User

Untitled

a guest
Oct 13th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. interface UserRegistrationService {
  2.     void isEmailAlreadyUsed(String email);
  3. }
  4.  
  5. interface PasswordHashService {
  6.     String hash(String plainTextPassword);
  7. }
  8.  
  9. class User {
  10.     private String email;
  11.     private String passwordHash;
  12.  
  13.     private User(String email, String passwordHash) {
  14.         this.email = email;
  15.         this.passwordHash = passwordHash;
  16.     }
  17.  
  18.     public static User registerNew(
  19.         String email,
  20.         String passwordInPlainText,
  21.         UserRegistrationService userRegistrationService,
  22.         PasswordHashService passwordHashService
  23.     ) {
  24.         if (userRegistrationService.isEmailAlreadyUsed(email) {
  25.             throw new DomainException(...);
  26.         }
  27.  
  28.         return new User(email, passwordHashService.hash(passwordInPlainText));
  29.     }
  30. }
  31.  
  32. class RegisterNewUserCommand {
  33.     public String email;
  34.     public String passwordInPlainText;
  35. }
  36.  
  37. class RegisterNewUserCommandHandler {
  38.     private UserRegistrationService userRegistrationService;
  39.     private PasswordHashService passwordHashService;
  40.     private UserRepository userRepository;
  41.  
  42.     public RegisterNewUserCommandHandler(
  43.         UserRegistrationService userRegistrationService,
  44.         PasswordHashService passwordHashService,
  45.         UserRepository userRepository
  46.     ) {
  47.         this.userRegistrationService = userRegistrationService;
  48.         this.passwordHashService = passwordHashService;
  49.         this.userRepository = userRepository;
  50.     }
  51.  
  52.     public void handle(RegisterNewUserCommand command) {
  53.         User registeredUser = User.registerNew(
  54.             command.email,
  55.             command.passwordInPlainText,
  56.             userRegistrationService,
  57.             passwordHashService
  58.         );
  59.  
  60.         userRepository.save(registeredUser);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment