function it_does_not_sign_up_if_user_email_already_exists(SignUpUserCommand $command, UserRepository $repository, User $user) { $command->id()->shouldBeCalled()->willReturn('user-id'); $id = new UserId('user-id'); $repository->userOfId($id)->shouldBeCalled()->willReturn(null); $command->email()->shouldBeCalled()->willReturn('*****@*****.**'); $email = new UserEmail('*****@*****.**'); $repository->userOfEmail($email)->shouldBeCalled()->willReturn($user); $this->shouldThrow(UserAlreadyExistException::class)->during__invoke($command); }
/** * Handles the given command. * * @param SignUpUserCommand $aCommand The command * * @throws UserAlreadyExistException when the user id is already exists */ public function __invoke(SignUpUserCommand $aCommand) { $id = new UserId($aCommand->id()); if (null !== $this->repository->userOfId($id)) { throw new UserAlreadyExistException(); } $email = new UserEmail($aCommand->email()); if (null !== $this->repository->userOfEmail($email)) { throw new UserAlreadyExistException(); } $userRoles = array_map(function ($role) { return new UserRole($role); }, $aCommand->roles()); $user = $this->factory->build($id, $email, UserPassword::fromPlain($aCommand->password(), $this->encoder), $userRoles); $user->enableAccount(); $this->repository->persist($user); }