Completed
Push — master ( 3543bf...07ac7e )
by Nikola
12:28
created

onFetchError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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
    private $twig;
26
27
    /**
28
     * @var \Swift_Mailer
29
     */
30
    private $mailer;
31
32
    /**
33
     * FetchEventsEmailNotificationListener constructor.
34
     *
35
     * @param \Twig_Environment $twig
36
     * @param \Swift_Mailer $mailer
37
     */
38
    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer)
39
    {
40
        $this->twig = $twig;
41
        $this->mailer = $mailer;
42
    }
43
44
    /**
45
     * Success event handler
46
     *
47
     * @param FetchSuccessEvent $successEvent
48
     */
49
    public function onFetchSuccess(FetchSuccessEvent $successEvent)
50
    {
51
        $message = $this->buildSwiftMessage('@ExchangeRate/mail/success.html.twig', [
52
            'date' => $successEvent->getDate(),
53
            'rates' => $successEvent->getRates()
54
        ]);
55
56
        $this->mailer->send($message);
57
    }
58
59
    /**
60
     * Error event handler
61
     *
62
     * @param FetchErrorEvent $errorEvent
63
     */
64
    public function onFetchError(FetchErrorEvent $errorEvent)
65
    {
66
        $message = $this->buildSwiftMessage('@ExchangeRate/mail/error.html.twig', [
67
            'date' => $errorEvent->getDate(),
68
            'errors' => $errorEvent->getErrors()
69
        ]);
70
71
        $this->mailer->send($message);
72
    }
73
74
    /**
75
     * Builds swift message
76
     *
77
     * @param string $template
78
     * @param array $context
79
     *
80
     * @return \Swift_Message
81
     */
82
    private function buildSwiftMessage($template, array $context = [])
83
    {
84
        $message = new \Swift_Message();
85
86
        $message
87
            ->setSubject($this->renderBlock($template, 'subject', $context))
88
            ->setTo($this->renderBlock($template, 'to', $context))
89
            ->setBody($this->renderBlock($template, 'body', $context), 'text/html');
90
91
        return $message;
92
    }
93
94
    /**
95
     * Render twig template single block
96
     *
97
     * @param string $template
98
     * @param string $block
99
     * @param array $context
100
     *
101
     * @return string
102
     */
103
    private function renderBlock($template, $block, array $context = [])
104
    {
105
        /** @var $template \Twig_Template */
106
        $template = $this->twig->loadTemplate($template);
107
        $context = $this->twig->mergeGlobals($context);
108
109
        return $template->renderParentBlock($block, $context);
110
    }
111
}
112