email === null || trim($request->email) === '') { throw new BadRequestException('email is required'); } try { $email = new EmailAddress($request->email); } catch (InvalidArgumentException) { throw new BadRequestException('email must be valid'); } $user = $this->findOrCreatePendingUser($email); $token = $this->createToken->execute( new CreateEmailConfirmationTokenRequest( user: $user, minuteOffset: 10, ), ); $body = $this->emailFactory->makeConfirmationEmail( $token->getToken(), ); $this->emailer->send( $user->getEmail(), 'Confirm your Attainly email', $body, ); } /** * @throws DomainException */ private function findOrCreatePendingUser(EmailAddress $email): User { $user = $this->userRepository->findByEmail($email); if ($user === null) { return $this->userRepository->create(new CreateUserDto( email: $email, passwordHash: null, )); } if ($user->getPasswordHash() !== null) { throw new DomainException( "{$email->value()} already has an account", ); } return $user; } }