public function testEmailIsSubscribed()
 {
     $newsletterService = $this->getNewsletterService();
     $newsletter = $this->getNewsletter();
     $email = sprintf("*****@*****.**", time());
     $emailEntity = new \ClassCentral\SiteBundle\Entity\Email();
     $emailEntity->setEmail($email);
     $result = $newsletterService->subscribeEmail($newsletter, $emailEntity);
     $this->assertTrue($result, "Failed to subscribe user to a newsletter " . $newsletter->getCode());
     // Unsubscribe this user
     $unsubscribe = $newsletterService->unSubscribeEmail($newsletter, $emailEntity);
     $this->assertTrue($unsubscribe, "Failed to unsubscribe user from a newsletter" . $newsletter->getCode());
 }
 /**
  * Saves the users subscrption for the newsletter
  * @param Request $request
  * @param $code
  */
 public function subscribeToAction(Request $request, $code)
 {
     $em = $this->getDoctrine()->getManager();
     $session = $this->get('session');
     $userSession = $this->get('user_session');
     $newsletterService = $this->get('newsletter');
     $logger = $this->get('logger');
     $newsletter = $em->getRepository('ClassCentralSiteBundle:Newsletter')->findOneByCode($code);
     if (!$newsletter) {
         // TODO: Show error
         return null;
     }
     $user = null;
     $email = null;
     // Check if the user is signed in
     if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
         $user = $this->get('security.context')->getToken()->getUser();
     } else {
         // Get the email for the user
         $email = $request->request->get('email');
         $email = strtolower($email);
         // Validate it
         $emailConstraint = new Email();
         $emailConstraint->message = "Invalid email address";
         $errorList = $this->get('validator')->validateValue($email, $emailConstraint);
         if (count($errorList) > 0) {
             // Invalid email
             $errorMessage = $errorList[0]->getMessage();
             $session->getFlashBag()->add('newsletter_invalid_email', $errorMessage);
             return $this->redirect($this->generateUrl('newsletter_subscribe', array('code' => $code)));
         }
         // email exists. Check if the user has an account
         $user = $em->getRepository('ClassCentralSiteBundle:User')->findOneByEmail($email);
         if (!$user) {
             // Check if the email is the database
             $emailEntity = $em->getRepository('ClassCentralSiteBundle:Email')->findOneByEmail($email);
             if (!$emailEntity) {
                 $emailEntity = new \ClassCentral\SiteBundle\Entity\Email();
                 $emailEntity->setEmail($email);
             }
         }
     }
     if ($user) {
         // Save the subscription prefrences
         $user->subscribe($newsletter);
         $em->persist($user);
         if (!$user->getIsverified()) {
             // Send a email verification message
             $this->sendEmailVerification($user->getEmail());
         } else {
             // Subscribe
             if ($this->container->getParameter('kernel.environment') != 'test') {
                 $subscribed = $newsletterService->subscribeUser($newsletter, $user);
                 $logger->info("subscribeToAction : user newsletter subscription", array('user' => $user->getId(), 'newsletter' => $newsletter->getCode(), 'subscribed' => $subscribed));
             }
         }
     } else {
         // Not a new email or is not verified
         if (!$emailEntity->getId() || !$emailEntity->getIsverified()) {
             // Send a email verification message
             $this->sendEmailVerification($emailEntity->getEmail());
         }
         // Save the subscription preferences
         $emailEntity->subscribe($newsletter);
         $em->persist($emailEntity);
         $userSession->setNewsletterUserEmail($emailEntity->getEmail());
         // If verified add the user to the mailing list
         if ($emailEntity->getIsverified()) {
             // Subscribe
             if ($this->container->getParameter('kernel.environment') != 'test') {
                 $subscribed = $newsletterService->subscribeEmail($newsletter, $emailEntity);
                 $logger->info("subscribeToAction : email newsletter subscription", array('email' => $emailEntity->getId(), 'newsletter' => $newsletter->getCode(), 'subscribed' => $subscribed));
             }
         }
     }
     $em->flush();
     return $this->redirect($this->generateUrl('newsletter_subscribed'));
 }