/**
  * Sends an email to invite emailee to join the project
  * @param string $projectId
  * @param string $inviterUserId
  * @param Website $website
  * @param string $toEmail
  * @param DeliveryInterface $delivery
  * @throws \Exception
  * @return string $userId
  */
 public static function sendInvite($projectId, $inviterUserId, $website, $toEmail, DeliveryInterface $delivery = null)
 {
     $newUser = new UserModel();
     $inviterUser = new UserModel($inviterUserId);
     $project = new ProjectModel($projectId);
     $newUser->emailPending = $toEmail;
     // Check if email already exists in an account
     $identityCheck = UserCommands::checkIdentity('', $toEmail, $website);
     if ($identityCheck->emailExists) {
         $newUser->readByProperty('email', $toEmail);
     }
     // Make sure the user exists on the site
     if (!$newUser->hasRoleOnSite($website)) {
         $newUser->siteRole[$website->domain] = $website->userDefaultSiteRole;
     }
     // Determine if user is already a member of the project
     if ($project->userIsMember($newUser->id->asString())) {
         return $newUser->id;
     }
     // Add the user to the project
     $newUser->addProject($project->id->asString());
     $userId = $newUser->write();
     $project->addUser($userId, ProjectRoles::CONTRIBUTOR);
     $project->write();
     if (!$identityCheck->emailExists) {
         // Email communication with new user
         Communicate::sendInvite($inviterUser, $newUser, $project, $website, $delivery);
     } else {
         // Tell existing user they're now part of the project
         Communicate::sendAddedToProject($inviterUser, $newUser, $project, $website, $delivery);
     }
     return $userId;
 }