MessageFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D createMessage() 0 46 10
1
<?php
2
3
namespace Knp\RadBundle\Mailer;
4
5
use Swift_Mailer;
6
use Twig_Environment;
7
use Knp\RadBundle\AppBundle\BundleGuesser;
8
9
class MessageFactory
10
{
11
    private $mailer;
12
    private $twig;
13
    private $bundleGuesser;
14
15
    public function __construct(Swift_Mailer $mailer, Twig_Environment $twig, BundleGuesser $bundleGuesser)
16
    {
17
        $this->mailer = $mailer;
18
        $this->twig = $twig;
19
        $this->bundleGuesser = $bundleGuesser;
20
    }
21
22
    public function createMessage($class, $name, array $parameters)
23
    {
24
        $subject = $txtBody = $htmlBody = null;
25
        $bundle  = $this->bundleGuesser->getBundleForClass($class);
26
        $txtTpl  = sprintf('%s:Mails:%s.txt.twig', $bundle->getName(), $name);
27
        $htmlTpl = sprintf('%s:Mails:%s.html.twig', $bundle->getName(), $name);
28
29
        if (true === $this->twig->getLoader()->exists($txtTpl)) {
30
            $template = $this->twig->loadTemplate($txtTpl);
31
            $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...
32
            $txtBody  = $template->renderBlock('body', $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...
33
        }
34
35
        if (true === $this->twig->getLoader()->exists($htmlTpl)) {
36
            $template = $this->twig->loadTemplate($htmlTpl);
37
            $subject  = $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...
38
            $htmlBody = $template->renderBlock('body', $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...
39
        }
40
41
        if (!$subject) {
42
            throw new \Twig_Error_Loader(sprintf(
43
                'Can not find mail subject in "%s" or "%s".', $txtTpl, $htmlTpl
44
            ));
45
        }
46
47
        if (!$txtBody && !$htmlBody) {
48
            throw new \Twig_Error_Loader(sprintf(
49
                'Can not find mail body in "%s" or "%s".', $txtTpl, $htmlTpl
50
            ));
51
        }
52
53
        $message = $this->mailer->createMessage();
54
        $message->setSubject($subject);
55
56
        if ($txtBody) {
57
            $message->setBody($txtBody, 'text/plain');
58
        } else {
59
            $message->setBody($htmlBody, 'text/html');
60
        }
61
62
        if ($txtBody && $htmlBody) {
63
            $message->addPart($htmlBody, 'text/html');
64
        }
65
66
        return $message;
67
    }
68
}
69