FetchEventsEmailNotificationListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 98
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onFetchSuccess() 0 9 1
A onFetchError() 0 9 1
A buildSwiftMessage() 0 11 1
A renderBlock() 0 8 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\Listener;
11
12
use RunOpenCode\Bundle\ExchangeRate\Event\FetchErrorEvent;
13
use RunOpenCode\Bundle\ExchangeRate\Event\FetchSuccessEvent;
14
15
/**
16
 * Class FetchEventsEmailNotificationListener
17
 *
18
 * @package RunOpenCode\Bundle\ExchangeRate\Listener
19
 */
20
class FetchEventsEmailNotificationListener
21
{
22
    /**
23
     * @var \Twig_Environment
24
     */
25
    protected $twig;
26
27
    /**
28
     * @var \Swift_Mailer
29
     */
30
    protected $mailer;
31
32
    /**
33
     * @var string[]
34
     */
35
    protected $recipients;
36
37
    /**
38
     * FetchEventsEmailNotificationListener constructor.
39
     *
40
     * @param \Twig_Environment $twig
41
     * @param \Swift_Mailer $mailer
42
     */
43
    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer, array $recipients)
44
    {
45
        $this->twig = $twig;
46
        $this->mailer = $mailer;
47
        $this->recipients = $recipients;
48
    }
49
50
    /**
51
     * Success event handler
52
     *
53
     * @param FetchSuccessEvent $successEvent
54
     */
55
    public function onFetchSuccess(FetchSuccessEvent $successEvent)
56
    {
57
        $message = $this->buildSwiftMessage('@ExchangeRate/mail/success.html.twig', [
58
            'date' => $successEvent->getDate(),
59
            'rates' => $successEvent->getRates()
60
        ]);
61
62
        $this->mailer->send($message);
63
    }
64
65
    /**
66
     * Error event handler
67
     *
68
     * @param FetchErrorEvent $errorEvent
69
     */
70
    public function onFetchError(FetchErrorEvent $errorEvent)
71
    {
72
        $message = $this->buildSwiftMessage('@ExchangeRate/mail/error.html.twig', [
73
            'date' => $errorEvent->getDate(),
74
            'errors' => $errorEvent->getErrors()
75
        ]);
76
77
        $this->mailer->send($message);
78
    }
79
80
    /**
81
     * Builds swift message
82
     *
83
     * @param string $template
84
     * @param array $context
85
     *
86
     * @return \Swift_Message
87
     */
88
    private function buildSwiftMessage($template, array $context = [])
89
    {
90
        $message = new \Swift_Message();
91
92
        $message
93
            ->setSubject($this->renderBlock($template, 'subject', $context))
94
            ->setTo($this->recipients)
95
            ->setBody($this->renderBlock($template, 'body', $context), 'text/html');
96
97
        return $message;
98
    }
99
100
    /**
101
     * Render twig template single block
102
     *
103
     * @param string $template
104
     * @param string $block
105
     * @param array $context
106
     *
107
     * @return string
108
     */
109
    private function renderBlock($template, $block, array $context = [])
110
    {
111
        /** @var $template \Twig_Template */
112
        $template = $this->twig->loadTemplate($template);
113
        $context = $this->twig->mergeGlobals($context);
114
115
        return $template->renderBlock($block, $context);
116
    }
117
}
118