Completed
Push — master ( 41e5ad...f2b4de )
by Alexandre
02:39
created

Notification::sendSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace AppBundle\Service;
4
5
use AppBundle\Event\OrderEvent;
6
use AppBundle\Event\OrderEvents;
7
use AppBundle\Service\Twilio;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class Notification implements EventSubscriberInterface
11
{
12
    private $twig;
13
    private $mailer;
14
    private $twilio;
15
    private $ownerRecipient;
16
    private $sender;
17
18
    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer, Twilio $twilio, $ownerRecipient, $sender)
19
    {
20
        $this->twig = $twig;
21
        $this->mailer = $mailer;
22
        $this->twilio = $twilio;
23
        $this->ownerRecipient = $ownerRecipient;
24
        $this->sender = $sender;
25
    }
26
27
    public function onOrderCreatedEvent(OrderEvent $event)
28
    {
29
        $order = $event->getOrder();
30
31
        // email for owner
32
        $this->sendMail(
33
            $this->ownerRecipient,
34
            '_mail/owner_notification.html.twig',
35
            array('order' => $order)
36
        );
37
38
        // email for customer
39
        $this->sendMail(
40
            array($order->getEmail() => $order->getFullname()),
41
            '_mail/customer_notification.html.twig',
42
            array('order' => $order)
43
        );
44
45
        // sms for owner
46
        $this->twilio->notifyOwner($this->twig->render(
47
            '_sms/owner_notification.html.twig',
48
            array('order' => $order)
49
        ));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public static function getSubscribedEvents()
56
    {
57
        return array(
58
            OrderEvents::ORDER_CREATED => 'onOrderCreatedEvent'
59
        );
60
    }
61
62
    private function sendMail($recipient, $template, array $parameters = array())
63
    {
64
        $message = $this->getMessage($template, $parameters);
65
        $message->setFrom($this->sender);
66
        $message->setTo($recipient);
67
        $this->mailer->send($message);
68
    }
69
70
    public function getMessage($template, $parameters = array())
71
    {
72
        $template = $this->twig->loadTemplate($template);
73
74
        $parameters = array_merge($this->twig->getGlobals(), $parameters);
75
76
        $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...
77
        $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...
78
        $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...
79
80
        return \Swift_Message::newInstance()
81
            ->setSubject($subject)
82
            ->setBody($bodyText, 'text/plain')
83
            ->addPart($bodyHtml, 'text/html')
84
        ;
85
    }
86
}
87