Completed
Push — master ( 5c4b09...41e5ad )
by Alexandre
04:27
created

Notification::onOrderCreatedEvent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 24
rs 8.9713
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Service;
4
5
use AppBundle\Event\OrderEvent;
6
use AppBundle\Event\OrderEvents;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
class Notification implements EventSubscriberInterface
10
{
11
    private $twig;
12
    private $mailer;
13
    private $twilio;
14
    private $ownerRecipient;
15
    private $sender;
16
    private $twilioFrom;
17
    private $twilioTo;
18
19
    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer, \Services_Twilio $twilio, $ownerRecipient, $sender, $twilioFrom, $twilioTo)
20
    {
21
        $this->twig = $twig;
22
        $this->mailer = $mailer;
23
        $this->twilio = $twilio;
24
        $this->ownerRecipient = $ownerRecipient;
25
        $this->sender = $sender;
26
        $this->twilioFrom = $twilioFrom;
27
        $this->twilioTo = $twilioTo;
28
    }
29
30
    public function onOrderCreatedEvent(OrderEvent $event)
31
    {
32
        $order = $event->getOrder();
33
34
        // notification for owner
35
        $this->sendMail(
36
            $this->ownerRecipient,
37
            '_mail/owner_notification.html.twig',
38
            array('order' => $order)
39
        );
40
41
        // notification for customer
42
        $this->sendMail(
43
            array($order->getEmail() => $order->getFullname()),
44
            '_mail/customer_notification.html.twig',
45
            array('order' => $order)
46
        );
47
48
        // notification for customer
49
        $this->sendSms(
50
            '_sms/owner_notification.html.twig',
51
            array('order' => $order)
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public static function getSubscribedEvents()
59
    {
60
        return array(
61
            OrderEvents::ORDER_CREATED => 'onOrderCreatedEvent'
62
        );
63
    }
64
65
    private function sendMail($recipient, $template, array $parameters = array())
66
    {
67
        $message = $this->getMessage($template, $parameters);
68
        $message->setFrom($this->sender);
69
        $message->setTo($recipient);
70
        $this->mailer->send($message);
71
    }
72
73
    private function sendSms($template, array $parameters = array())
74
    {
75
        $this->twilio->account->messages->sendMessage(
76
            $this->twilioFrom,
77
            $this->twilioTo,
78
            $this->twig->render($template, $parameters)
79
        );
80
    }
81
82
    public function getMessage($template, $parameters = array())
83
    {
84
        $template = $this->twig->loadTemplate($template);
85
86
        $parameters = array_merge($this->twig->getGlobals(), $parameters);
87
88
        $subject  = $template->renderBlock('subject',   $parameters);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
89
        $bodyHtml = $template->renderBlock('body_html', $parameters);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
        $bodyText = $template->renderBlock('body_text', $parameters);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
92
        return \Swift_Message::newInstance()
93
            ->setSubject($subject)
94
            ->setBody($bodyText, 'text/plain')
95
            ->addPart($bodyHtml, 'text/html')
96
        ;
97
    }
98
}
99