| @@ 30-99 (lines=70) @@ | ||
| 27 | * @author Beñat Espiña <[email protected]> |
|
| 28 | * @author Gorka Laucirica <[email protected]> |
|
| 29 | */ |
|
| 30 | class SignUpUserHandler |
|
| 31 | { |
|
| 32 | /** |
|
| 33 | * The user password encoder. |
|
| 34 | * |
|
| 35 | * @var UserPasswordEncoder |
|
| 36 | */ |
|
| 37 | private $encoder; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * The user factory. |
|
| 41 | * |
|
| 42 | * @var UserFactory |
|
| 43 | */ |
|
| 44 | private $factory; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * The user repository. |
|
| 48 | * |
|
| 49 | * @var UserRepository |
|
| 50 | */ |
|
| 51 | private $repository; |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Constructor. |
|
| 55 | * |
|
| 56 | * @param UserRepository $aRepository The user repository |
|
| 57 | * @param UserPasswordEncoder $anEncoder The password encoder |
|
| 58 | * @param UserFactory $aFactory The user factory |
|
| 59 | */ |
|
| 60 | public function __construct(UserRepository $aRepository, UserPasswordEncoder $anEncoder, UserFactory $aFactory) |
|
| 61 | { |
|
| 62 | $this->repository = $aRepository; |
|
| 63 | $this->encoder = $anEncoder; |
|
| 64 | $this->factory = $aFactory; |
|
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Handles the given command. |
|
| 69 | * |
|
| 70 | * @param SignUpUserCommand $aCommand The command |
|
| 71 | * |
|
| 72 | * @throws UserAlreadyExistException when the user id is already exists |
|
| 73 | */ |
|
| 74 | public function __invoke(SignUpUserCommand $aCommand) |
|
| 75 | { |
|
| 76 | $id = new UserId($aCommand->id()); |
|
| 77 | if (null !== $this->repository->userOfId($id)) { |
|
| 78 | throw new UserAlreadyExistException(); |
|
| 79 | } |
|
| 80 | $email = new UserEmail($aCommand->email()); |
|
| 81 | if (null !== $this->repository->userOfEmail($email)) { |
|
| 82 | throw new UserAlreadyExistException(); |
|
| 83 | } |
|
| 84 | ||
| 85 | $userRoles = array_map(function ($role) { |
|
| 86 | return new UserRole($role); |
|
| 87 | }, $aCommand->roles()); |
|
| 88 | ||
| 89 | $user = $this->factory->register( |
|
| 90 | $id, |
|
| 91 | $email, |
|
| 92 | UserPassword::fromPlain($aCommand->password(), $this->encoder), |
|
| 93 | $userRoles |
|
| 94 | ); |
|
| 95 | $user->enableAccount(); |
|
| 96 | ||
| 97 | $this->repository->persist($user); |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| @@ 30-98 (lines=69) @@ | ||
| 27 | * @author Beñat Espiña <[email protected]> |
|
| 28 | * @author Gorka Laucirica <[email protected]> |
|
| 29 | */ |
|
| 30 | class WithConfirmationSignUpUserHandler |
|
| 31 | { |
|
| 32 | /** |
|
| 33 | * The user password encoder. |
|
| 34 | * |
|
| 35 | * @var UserPasswordEncoder |
|
| 36 | */ |
|
| 37 | private $encoder; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * The user factory. |
|
| 41 | * |
|
| 42 | * @var UserFactory |
|
| 43 | */ |
|
| 44 | private $factory; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * The user repository. |
|
| 48 | * |
|
| 49 | * @var UserRepository |
|
| 50 | */ |
|
| 51 | private $repository; |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Constructor. |
|
| 55 | * |
|
| 56 | * @param UserRepository $aRepository The user repository |
|
| 57 | * @param UserPasswordEncoder $anEncoder The password encoder |
|
| 58 | * @param UserFactory $aFactory The user factory |
|
| 59 | */ |
|
| 60 | public function __construct(UserRepository $aRepository, UserPasswordEncoder $anEncoder, UserFactory $aFactory) |
|
| 61 | { |
|
| 62 | $this->repository = $aRepository; |
|
| 63 | $this->encoder = $anEncoder; |
|
| 64 | $this->factory = $aFactory; |
|
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Handles the given command. |
|
| 69 | * |
|
| 70 | * @param WithConfirmationSignUpUserCommand $aCommand The command |
|
| 71 | * |
|
| 72 | * @throws UserAlreadyExistException when the user id is already exists |
|
| 73 | */ |
|
| 74 | public function __invoke(WithConfirmationSignUpUserCommand $aCommand) |
|
| 75 | { |
|
| 76 | $id = new UserId($aCommand->id()); |
|
| 77 | if (null !== $this->repository->userOfId($id)) { |
|
| 78 | throw new UserAlreadyExistException(); |
|
| 79 | } |
|
| 80 | $email = new UserEmail($aCommand->email()); |
|
| 81 | if (null !== $this->repository->userOfEmail($email)) { |
|
| 82 | throw new UserAlreadyExistException(); |
|
| 83 | } |
|
| 84 | ||
| 85 | $userRoles = array_map(function ($role) { |
|
| 86 | return new UserRole($role); |
|
| 87 | }, $aCommand->roles()); |
|
| 88 | ||
| 89 | $user = $this->factory->register( |
|
| 90 | $id, |
|
| 91 | $email, |
|
| 92 | UserPassword::fromPlain($aCommand->password(), $this->encoder), |
|
| 93 | $userRoles |
|
| 94 | ); |
|
| 95 | ||
| 96 | $this->repository->persist($user); |
|
| 97 | } |
|
| 98 | } |
|
| 99 | ||