87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?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'
|
|
);
|
|
|
|
if (isset($data['attachments']) && is_array($data['attachments']) && !empty($data['attachments'])) {
|
|
foreach ($data['attachments'] as $file) {
|
|
if (is_object($file) && $file instanceof \Swift_Attachment) {
|
|
$message->attach($file);
|
|
} elseif (is_string($file) && file_exists($file) && is_readable($file) && !is_dir($file)) {
|
|
$message->attach(\Swift_Attachment::fromPath($file));
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->mailer->send($message);
|
|
|
|
$this->basicNotifier->notify($template, $data);
|
|
|
|
return $this;
|
|
}
|
|
}
|