1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\Notification; |
4
|
|
|
|
5
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Contract\NotificationInterface; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
7
|
|
|
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait; |
8
|
|
|
|
9
|
|
|
class MailNotification implements NotificationInterface |
10
|
|
|
{ |
11
|
|
|
use LoggerAwareTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \Swift_Mailer |
15
|
|
|
*/ |
16
|
|
|
protected $mailer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var EngineInterface |
20
|
|
|
*/ |
21
|
|
|
protected $templateEngine; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $settings; |
27
|
|
|
|
28
|
|
|
public function __construct(\Swift_Mailer $mailer, EngineInterface $engine, array $settings) |
29
|
|
|
{ |
30
|
|
|
$this->mailer = $mailer; |
31
|
|
|
$this->templateEngine = $engine; |
32
|
|
|
$this->settings = $settings; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create and send mail notification. |
37
|
|
|
* |
38
|
|
|
* @param array $vars Variables to use when rendering mail body. |
39
|
|
|
* @return MailNotification $this Fluent interface. |
40
|
|
|
*/ |
41
|
|
|
public function notify(array $vars = array()) |
42
|
|
|
{ |
43
|
|
|
$message = \Swift_Message::newInstance() |
44
|
|
|
->setSubject($this->settings['subject']) |
45
|
|
|
->setFrom($this->settings['from']) |
46
|
|
|
->setTo($this->settings['to']) |
47
|
|
|
->setCc($this->settings['cc']) |
48
|
|
|
->setBcc($this->settings['bcc']) |
49
|
|
|
->setBody($this->templateEngine->render($this->settings['template'], $vars), 'text/html'); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$this->mailer->send($message); |
53
|
|
|
|
54
|
|
|
$this->getLogger()->notice('Mail notification successfully sent.', array_merge( |
55
|
|
|
$this->settings, |
56
|
|
|
array('body' => $message->getBody()) |
57
|
|
|
)); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$this->getLogger()->error('Could not send email notification.', array_merge( |
60
|
|
|
$this->settings, |
61
|
|
|
array('body' => $message->getBody()) |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|