コード例 #1
0
 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();
     }
 }