/**
  * Create and validate referral. Email referred email address if referral is valid, and save the referral to the database.
  *
  * @param bool $automaticUrl        If set to true, the link injected into the email will be to the page the user
  *                                  was on when they sent the referral. If set to false, it will inject the base URL
  *
  * @return \Message\Cog\HTTP\RedirectResponse
  */
 public function referAFriendAction($automaticUrl = false)
 {
     if ($this->get('user.current') instanceof AnonymousUser) {
         throw new \LogicException('User must be logged in to refer a friend!');
     }
     $form = $this->createForm($this->get('refer.form.refer_a_friend'));
     $form->handleRequest();
     if ($form->isValid()) {
         $data = $form->getData();
         $referral = $this->get('refer.referral_builder')->build($data);
         $validator = $this->get('refer.validator_factory')->getValidator();
         if (false === $validator->isValid($referral)) {
             $this->addFlash('error', $this->trans($validator->getMessage()));
             return $this->redirectToReferer();
         }
         $referral = $this->get('refer.referral.create')->save($referral);
         $this->addFlash('success', $this->trans('ms.refer.referral.create.success'));
         $event = new Event\EmailReferralEvent();
         $event->setReferral($referral);
         if ($automaticUrl) {
             $event->setUrl($this->get('request')->headers->get('referer'));
         } else {
             $event->setUrl($this->generateUrl('ms.cms.frontend', [], true));
         }
         try {
             $this->get('event.dispatcher')->dispatch(Event\Events::EMAIL_REFERRED, $event);
         } catch (EmailException $e) {
             $this->addFlash('error', $e->getMessage());
         }
     }
     return $this->redirectToReferer();
 }
 /**
  * Send email to referred email address upon submission of Refer a Friend form
  *
  * @param Event\EmailReferralEvent $event
  */
 public function sendReferralEmail(Event\EmailReferralEvent $event)
 {
     $referral = $event->getReferral();
     $message = $this->get('mail.message');
     $message->setTo($referral->getReferredEmail(), $referral->getReferredName());
     $message->setSubject($this->get('translator')->trans('ms.refer.email.subject', ['{%site%}' => $this->get('cfg')->app->name]));
     $message->setView('Message:Mothership:ReferAFriend::refer_a_friend:email:referral_message', ['toName' => $referral->getReferredName(), 'message' => $referral->getRewardConfig()->getMessage(), 'url' => $event->getUrl(), 'referrer' => $referral->getReferrer()]);
     $dispatcher = $this->get('mail.dispatcher');
     $failedRecipients = [];
     $dispatcher->send($message, $failedRecipients);
     if (count($failedRecipients) > 0) {
         throw new EmailException($this->get('translator')->trans('ms.refer.referral.create.email_not_sent'));
     }
 }