deblan.tv/vendor/trinity/src/Trinity/.svn/pristine/32/32212c5f411718bea9040007bcfd51d9a8118745.svn-base
2015-03-02 21:57:49 +01:00

75 lines
1.8 KiB
Plaintext

<?php
namespace Trinity\Bundle\NotificationBundle\Notifier;
class EmailNotifier extends AbstractNotifier
{
protected $basicNotifier;
protected $mailer;
public function __construct(BasicNotifier $basicNotifier, \Swift_Mailer $mailer)
{
$this->basicNotifier = $basicNotifier;
$this->mailer = $mailer;
}
public function setBasicNotifier($basicNotifier)
{
$this->basicNotifier = $basicNotifier;
return $this;
}
public function getBasicNotifier()
{
return $this->basicNotifier;
}
public function setMailer(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
return $this;
}
public function getMailer()
{
return $this->mailer;
}
public function notify($template, array $data = array())
{
if (!isset($data['mailer'])) {
throw new \LogicException('You must provide mailer values (from, subject).');
}
if (!isset($data['mailer']['from'])) {
throw new \LogicException('You must provide the sender (from).');
}
if (!isset($data['mailer']['subject'])) {
throw new \LogicException('You must provide the subject (subject).');
}
$data = array_merge(
$this->basicNotifier->getDefaultData(),
parent::getDefaultData(),
$data
);
$message = \Swift_Message::newInstance()
->setSubject($data['mailer']['subject'])
->setFrom($data['mailer']['from'])
->setTo($this->basicNotifier->getUser()->getEmail())
->setBody(
$this->basicNotifier->getTwig()->render($this->loadTemplate($template)->getContent(), $data),
'text/html'
);
$this->mailer->send($message);
$this->basicNotifier->notify($template, $data);
}
}