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