Completed
Push — master ( b8ee64...bf1b53 )
by Nikola
04:04
created

MailNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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