/** * Actually do the logging. * * @param EventInterface $event */ public function log(EventInterface $event) { $params = $event->getParams(); $method = isset($params[0]) ? $params[0] : ''; $this->logger->debug(get_class($event->getTarget()) . "::{$method} - {$event->getName()}"); }
/** * @return $this * @throws NotifyException */ public function send() { //ID // create an ID for this digest $emailId = $this->getHash(); //TIME RANGE // calculate time range and create from and // to date objects for the range. $from = new DateTime(); $from->add(new DateInterval('P1D')); $to = new DateTime(); $to->add(new DateInterval('P8D')); $this->logger->info("Queue Service says: Fetching upcoming events"); //EVENTS // fetch all events $events = $this->getEvents($from, $to); //NO EVENTS // if there are no events to publish, then it's no need // to keep on processing this script if (count($events) == 0) { $this->logger->info("Digest, no events registered, stop"); return $this; } else { $this->logger->info("Digest, " . count($events) . " events registered."); } //USERS // get all users who want to know // about the upcoming events. $users = $this->getUsers(); $this->logger->info("Digest, " . count($users) . " user will get email "); //VIEW // create and configure view $child = new ViewModel(array('events' => $events, 'from' => $from, 'to' => $to)); $child->setTemplate('news-digest'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'news-digest' => __DIR__ . '/../../../view/email/news-digest.phtml')); $phpRenderer->setResolver($resolver); //QUEUE // try to connect to Queue and send messages to it. // this will try to send messages to mail_queue, that will // send them on it's way via a MailTransport try { $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); foreach ($users as $user) { $child->setVariable('user', $user); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } $result = new Mail(); $result->name = $user->name; $result->email = $user->email; $result->subject = "Vikan framundan | {$from->format('j. n.')} - {$to->format('j. n. Y')}"; $result->body = $phpRenderer->render($layout); $result->id = $emailId; $result->user_id = md5((string) $emailId . $user->email); $result->type = 'Digest'; $result->parameters = 'allir'; $result->test = false; $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]); $channel->basic_publish($msg, '', 'mail_queue'); $this->logger->debug("Queue Service says: Fetching users who want upcoming events, {$user->email} in queue "); } } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $this->closeDataSourceDriver(); } return $this; }
/** * Run the handler. * * @return $this|NotifyInterface * @throws NotifyException */ public function send() { $emailId = $this->getHash(); $users = $this->getUsers($this->params->sender_id, $this->params->recipients, $this->params->test); $this->logger->info("Notify All ({$this->params->recipients})"); //MAIL // now we want to send this to the user/quest via e-mail // so we try to connect to Queue and send a message // to mail_queue try { //QUEUE // create and configure queue $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); //VIEW // create and configure view $child = new ViewModel(array('user' => null, 'body' => call_user_func(new Paragrapher(), $this->params->body))); $child->setTemplate('script'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new Resolver\TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'script' => __DIR__ . '/../../../view/email/letter.phtml')); $phpRenderer->setResolver($resolver); //FOR EVERY USER // for every user, render mail-template // and send to mail-queue foreach ($users as $user) { $child->setVariable('user', $user); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } $result = new MailMessage(); $result->name = $user->name; $result->email = $user->email; $result->subject = $this->params->subject; $result->body = $phpRenderer->render($layout); $result->id = $emailId; $result->user_id = md5((string) $emailId . $user->email); $result->entity_id = null; $result->type = 'All'; $result->parameters = $this->params->recipients; $result->test = $this->params->test; $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]); $this->logger->debug("Notify All via e-mail to user:{$user->email}"); $channel->basic_publish($msg, '', 'mail_queue'); } } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $userService = null; $this->closeDataSourceDriver(); } return $this; }