Completed
Push — master ( 845964...864a7b )
by Louis
16s
created

MailerService::sendAdmissible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 5
1
<?php
2
3
namespace KI\UserBundle\Service;
4
5
use KI\UserBundle\Entity\User;
6
use Swift_Attachment;
7
use Swift_Mailer;
8
use Swift_Message;
9
use Symfony\Bundle\TwigBundle\TwigEngine;
10
11
class MailerService
12
{
13
    protected $mailer;
14
    protected $templating;
15
16
    public function __construct(Swift_Mailer $mailer, TwigEngine $templating)
17
    {
18
        $this->mailer     = $mailer;
19
        $this->templating = $templating;
20
    }
21
22
    public function send(User $from, array $to, $title, $template, $vars, $attachments = [])
23
    {
24
        $message = Swift_Message::newInstance()
25
            ->setSubject($title)
26
            ->setFrom('[email protected]')
27
            ->setReplyTo([$from->getEmail() => $from->getFirstName().' '.$from->getLastName()])
28
        ;
29
30
        foreach($attachments as $attachment){
31
            $message->attach(Swift_Attachment::fromPath($attachment['path'])->setFilename($attachment['name']));
32
        }
33
34
        foreach ($to as $user) {
35
            $vars['username'] = $user->getUsername();
36
            $message
37
                ->setTo([$user->getEmail() => $user->getFirstName().' '.$user->getLastName()])
38
                ->setBody($this->templating->render($template, $vars), 'text/html')
39
            ;
40
            $this->mailer->send($message);
41
        }
42
    }
43
}
44