/**
* {@inheritdoc}
*/
public function create($templateName, array $context = array())
{
if (!isset($this->templateConfigs[$templateName])) {
throw new TemplateNotExistsException($templateName);
}
$templateConfig = $this->templateConfigs[$templateName];
if (isset($context['utm'])) {
$templateConfig['utm'] = array_merge($templateConfig['utm'], $context['utm']);
}
$context = $this->twig->mergeGlobals($context);
$template = $this->twig->loadTemplate($templateConfig['template']);
$subject = $template->renderBlock('subject', $context);
$htmlBody = $template->renderBlock('body_html', $context);
$textBody = $template->renderBlock('body_text', $context);
if (empty($subject)) {
throw new TemplatePartRequiredException('subject', $templateConfig['template']);
}
if (empty($htmlBody)) {
throw new TemplatePartRequiredException('body_html', $templateConfig['template']);
}
$htmlBody = $this->cssToInline($htmlBody);
$htmlBody = $this->addUtmParams($htmlBody, $templateConfig['host'], $templateConfig['utm']);
if (empty($textBody) && $templateConfig['generate_text_version']) {
$textBody = $this->htmlToText($htmlBody);
}
$message = \Swift_Message::newInstance()->setSubject($subject)->setBody($htmlBody, 'text/html');
if (!empty($textBody)) {
$message->addPart($textBody, 'text/plain');
}
return $message;
}