Mail   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 10
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B sendTemplateMail() 0 25 4
1
<?php
2
3
namespace BCRM\BackendBundle\Service;
4
5
use BCRM\BackendBundle\Service\Mail\SendTemplateMailCommand;
6
use BCRM\BackendBundle\Content\ContentReader;
7
use Symfony\Bridge\Twig\TwigEngine;
8
use Symfony\Component\Templating\TemplateReference;
9
10
class Mail
11
{
12
    /**
13
     * @var \Swift_Mailer
14
     */
15
    private $mailer;
16
17
    /**
18
     * @var \Symfony\Bridge\Twig\TwigEngine
19
     */
20
    private $templating;
21
22
    /**
23
     * @var string
24
     */
25
    private $mailFromName;
26
27
    /**
28
     * @var string
29
     */
30
    private $mailFromEmail;
31
32
    /**
33
     * @var \BCRM\BackendBundle\Content\ContentReader
34
     */
35
    private $cr;
36
37
    /**
38
     * @param \Swift_Mailer $mailer
39
     * @param               $mailFrom
40
     */
41
    public function __construct(\Swift_Mailer $mailer, TwigEngine $templating, $mailFromEmail, $mailFromName, ContentReader $cr)
42
    {
43
        $this->mailer        = $mailer;
44
        $this->templating    = $templating;
45
        $this->mailFromEmail = $mailFromEmail;
46
        $this->mailFromName  = $mailFromName;
47
        $this->cr            = $cr;
48
    }
49
50
    public function sendTemplateMail(SendTemplateMailCommand $command)
51
    {
52
        $message       = \Swift_Message::newInstance();
53
        $ext           = $command->format === 'text/html' ? 'html' : 'txt';
54
        $tplIdentifier = 'Email/' . $command->template . '.' . $ext;
55
        $template      = $this->cr->getContent($tplIdentifier);
56
        $templateData  = $command->templateData;
57
        if ($command->image !== null) {
58
            $templateData['image'] = $message->embed(\Swift_Image::fromPath($command->image));;
59
        }
60
61
        // Subject
62
        $env     = new \Twig_Environment(new \Twig_Loader_String());
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_String has been deprecated with message: since 1.18.1 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
63
        $subject = $template->getProperties()->containsKey('subject') ? $template->getProperties()->get('subject') : $this->mailFromName;
64
        $subject = $env->render($subject, $templateData);
65
66
        // Body
67
        $body = $this->templating->render('bcrm_content:' . $tplIdentifier, $templateData);
68
        $message->setCharset('UTF-8');
69
        $message->setFrom($this->mailFromEmail, $this->mailFromName)
70
            ->setSubject($subject)
71
            ->setTo($command->email)
72
            ->setBody($body, $command->format);
73
        $this->mailer->send($message);
74
    }
75
}
76