Example #1
0
 /**
  * @param HookEvent $hookEvent
  */
 public function onHookRequest(HookEvent $hookEvent)
 {
     $hook = $hookEvent->getHook();
     // update letter and snapshot
     $letter = $this->letterManager->updateByHook($hook);
     //update campaign
     if ($letter instanceof Letter) {
         $tag = $letter->getTags();
     }
     if (isset($tag)) {
         $campRepo = $this->em->getRepository("GroslabMailerBundle:Campaign");
         $camp = $campRepo->findOneBy(array('referer' => $tag));
     }
     if (isset($camp) && $camp instanceof Campaign) {
         if ($hook->getEvent() === HookEvents::DELIVERED) {
             $camp->addCountDelivered();
         }
         if ($hook->getEvent() === HookEvents::SPAMREPORT) {
             // todo:
         }
         if ($hook->getEvent() === HookEvents::OPEN) {
             $camp->addCountOpened();
         }
         if ($hook->getEvent() === HookEvents::CLICK) {
             $camp->addCountClicked();
         }
         if ($hook->getEvent() === HookEvents::BOUNCE) {
             //todo:
         }
         $this->em->persist($camp);
         $this->em->flush($camp);
     }
 }
 public function onRequestForSendLetter(RequestForSendLetterEvent $event)
 {
     /** @var DataForSendLetterInterface $letter */
     $letter = $event->getData();
     $name = $letter->getName();
     if (!isset($this->bundleConfig['templates'][$name])) {
         throw new \Exception(sprintf("MailType with name '%s' is not found", $name));
     }
     if (!isset($this->bundleConfig['templates'][$name]['subject'])) {
         throw new \Exception("Mail subject is undefined");
     }
     if (!isset($this->bundleConfig['templates'][$name]['from']['name'])) {
         $this->bundleConfig['templates'][$name]['from']['name'] = $this->bundleConfig['defaults']['from']['name'];
     }
     if (!isset($this->bundleConfig['templates'][$name]['from']['email'])) {
         $this->bundleConfig['templates'][$name]['from']['email'] = $this->bundleConfig['defaults']['from']['email'];
     }
     $mailType = $this->mailTypeManager->get($this->bundleConfig['templates'][$name], $name);
     $snapshot = $this->mailTypeManager->getUpdatedSnapshot($mailType);
     $template = $this->twig->createTemplate($snapshot->getBody());
     $body = $template->render(array('data' => $letter->getData()));
     /** @var SendResponse $result */
     $result = $this->provider->send($body, $letter, $this->bundleConfig['templates'][$name]);
     if ($result->isSuccess()) {
         /** @var Letter $letterStats */
         $letterStats = $this->letterManager->create($letter->getEmail(), $snapshot, false);
         $letterStats->setIsSent(true)->setSentAt(new \DateTime())->setHash($result->getHash());
         $tags = $letter->getTags();
         if (count($tags) > 0) {
             //TODO: ?? [0]
             $letterStats->setTags($tags[0]);
         }
         $snapshot->addCountSent();
         $this->em->persist($letterStats);
         $this->em->persist($snapshot);
         $this->em->flush();
     }
 }