/**
  * @param LoanOfficer $loanOfficer
  */
 protected function addLoanOfficerUser(LoanOfficer $loanOfficer)
 {
     $loUser = $loanOfficer->getUser();
     $loSite = $loanOfficer->getOfficerSite();
     $loCreateUser = $loanOfficer->getAutoCreateUser();
     if ($loCreateUser && !isset($loUser) && isset($loSite)) {
         $loRole = $this->em->getRepository('SudouxCmsUserBundle:Role')->findOneBy(array('role' => 'ROLE_LOAN_OFFICER'));
         // check if user already exists by email
         $existingUser = $this->em->getRepository('SudouxCmsUserBundle:User')->findOneBy(array('email' => $loanOfficer->getEmail()));
         if (isset($existingUser)) {
             // assign existing user to lo
             $loanOfficer->setUser($existingUser);
             $this->em->persist($loanOfficer);
         } else {
             $user = new User();
             $username = strtolower(substr($loanOfficer->getFirstName(), 0, 1) . $loanOfficer->getLastName());
             $username = str_replace(' ', '', preg_replace("/[^A-Za-z0-9 ]/", '', $username));
             $user->setUsername($this->getLoanOfficerUsername($username));
             $factory = $this->container->get('security.encoder_factory');
             $encoder = $factory->getEncoder($user);
             $password = $encoder->encodePassword($user->generatePassword(), $user->getSalt());
             $user->setPassword($password);
             $user->setEmail($loanOfficer->getEmail());
             $user->setFirstName($loanOfficer->getFirstName());
             $user->setLastName($loanOfficer->getLastName());
             $user->addSite($loanOfficer->getOfficerSite());
             $user->addRole($loRole);
             $user->addToken();
             $user->setTimezone($loanOfficer->getSite()->getTimezone());
             $loanOfficer->setUser($user);
             $this->em->persist($loanOfficer);
             $emailUtil = $this->container->get('sudoux.cms.message.email_util');
             $email = new Email();
             $email->setRecipient($user->getEmail());
             $email->setRecipientName($user->getFullName());
             $email->setSubject($this->container->get('sudoux.cms.site')->getSiteVar('New Website Account', 'loan_officer_user_add_email_subject'));
             $email->setUser($user);
             $email->setSite($loanOfficer->getSite());
             $resetPasswordUrl = sprintf("https://%s%s", $loanOfficer->getOfficerSite()->getPrimaryDomain()->getDomain(), $this->router->generate('sudoux_cms_user_reset_password', array('token' => $user->getToken())));
             // default message
             $message = sprintf('<p>An account has been created for you on your new website. Your username is %s</p>', $user->getUsername());
             $message .= sprintf('<p><a href="%s">Click here</a> to set your password and confirm your account.</p>', $resetPasswordUrl);
             $tokens = array('username' => $user->getUsername(), 'email' => $user->getEmail(), 'reset_password_url' => $resetPasswordUrl, 'first_name' => $user->getFirstName(), 'last_name' => $user->getLastName(), 'website_url' => 'https://' . $loanOfficer->getOfficerSite()->getPrimaryDomain()->getDomain());
             $message = $this->container->get('sudoux.cms.site')->getSiteVar($message, 'loan_officer_user_add_email_message', $tokens);
             $email->setMessage($message);
             $emailUtil->logAndSend($email);
         }
         $this->em->flush($loanOfficer);
     }
 }
 /**
  *
  */
 protected function syncLoanOfficersWithLos($siteId)
 {
     $loanUtil = $this->getContainer()->get('sudoux_mortgage.los_util');
     $response = $loanUtil->getLoanOfficers($this->site);
     $batch = 10;
     //print_r($response['loanOfficers']);exit;
     if ($response['success']) {
         $this->output->writeln(sprintf('processing %s loan officers', count($response['loanOfficers'])));
         $losLoanOfficers = $response['loanOfficers'];
         $i = 0;
         //print_r($response['loanOfficers']); exit;
         foreach ($response['loanOfficers'] as $loData) {
             $loanOfficer = $this->em->getRepository('SudouxMortgageBundle:LoanOfficer')->findOneBySiteAndLosId($this->site, $loData['username']);
             if (!isset($loanOfficer)) {
                 $loanOfficer = new LoanOfficer();
                 $loanOfficer->setSite($this->site);
                 $loanOfficer->setLosId($loData['username']);
             }
             $loanOfficer->setFirstName($loData['firstName']);
             $loanOfficer->setLastName($loData['lastName']);
             $loanOfficer->setEmail($loData['email']);
             $loanOfficer->setPhoneMobile($loData['cellPhone']);
             $loanOfficer->setPhoneOffice($loData['phone']);
             $loanOfficer->setFax($loData['fax']);
             $loanOfficer->setNmlsId($loData['nmlsID']);
             $loanOfficer->setAutoCreateUser(false);
             // this needs to be set after setSite to override default functionality
             $loanOfficer->setActive(true);
             $loanOfficer->setDeleted(false);
             $loSite = $loanOfficer->getOfficerSite();
             if (isset($loSite)) {
                 $loSite->setActive(true);
                 $loSite->setDeleted(false);
                 $this->em->persist($loSite);
             }
             //print_r($loData);
             if (!empty($loData['orgID'])) {
                 // look up branch
                 $branch = $this->em->getRepository('SudouxMortgageBundle:Branch')->findOneBySiteAndLosId($this->site, $loData['orgID']);
                 if (isset($branch)) {
                     $loanOfficer->setBranch($branch);
                     $branchSite = $branch->getBranchSite();
                     if (isset($branchSite) && isset($loSite)) {
                         $loSite->setParentSite($branchSite);
                         $this->em->persist($loSite);
                     }
                 }
             }
             $this->em->persist($loanOfficer);
             if ($i % $batch == 0) {
                 $this->em->flush();
                 $this->em->clear();
                 $this->site = $this->em->getRepository('SudouxCmsSiteBundle:Site')->find($siteId);
                 $this->output->writeln(sprintf('Batch %s of %s complete.', $i, count($response['loanOfficers'])));
             }
             $i++;
         }
         $this->em->flush();
         $this->em->clear();
         $this->output->writeln('Processing complete!');
     } else {
         $this->output->writeln('Failed to get a successful response from the LOS');
     }
 }