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); |
|
|
|
|
32
|
|
|
$txtBody = $template->renderBlock('body', $parameters); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (true === $this->twig->getLoader()->exists($htmlTpl)) { |
36
|
|
|
$template = $this->twig->loadTemplate($htmlTpl); |
37
|
|
|
$subject = $subject ?: $template->renderBlock('subject', $parameters); |
|
|
|
|
38
|
|
|
$htmlBody = $template->renderBlock('body', $parameters); |
|
|
|
|
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
|
|
|
|
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.