Skip to content

Commit

Permalink
[framework] ensure manually sent mail templates are wrapped with the …
Browse files Browse the repository at this point in the history
…GrapesJS body (#3664)
  • Loading branch information
vitek-rostislav authored Dec 17, 2024
2 parents 537faab + c85d615 commit c4e5533
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Model/Administrator/Mail/ResetPasswordMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function sendMail(Administrator $administrator)
{
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
ResetPasswordMail::MAIL_TEMPLATE_NAME,
$this->domain->getId(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function sendAuthCode(TwoFactorInterface $administrator): void
{
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
TwoFactorAuthenticationMail::TWO_FACTOR_AUTHENTICATION_CODE,
$this->domain->getId(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Complaint/Mail/ComplaintMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getMailTemplateByStatusAndDomainId(ComplaintStatus $complaintSta
{
$templateName = ComplaintMail::getMailTemplateNameByStatus($complaintStatus);

return $this->mailTemplateFacade->get($templateName, $domainId);
return $this->mailTemplateFacade->getWrappedWithGrapesJsBody($templateName, $domainId);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Customer/Mail/CustomerMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
*/
public function sendRegistrationMail(CustomerUser $customerUser): void
{
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
MailTemplate::REGISTRATION_CONFIRM_NAME,
$customerUser->getDomainId(),
);
Expand All @@ -46,7 +46,7 @@ public function sendRegistrationMail(CustomerUser $customerUser): void
*/
public function sendActivationMail(CustomerUser $customerUser): void
{
$mailTemplate = $this->mailTemplateFacade->get(CustomerActivationMail::CUSTOMER_ACTIVATION_NAME, $customerUser->getDomainId());
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(CustomerActivationMail::CUSTOMER_ACTIVATION_NAME, $customerUser->getDomainId());
$this->sendActivationMailTemplate($mailTemplate, $customerUser);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Customer/Mail/ResetPasswordMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function sendMail(CustomerUser $customerUser)
{
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
MailTemplate::RESET_PASSWORD_NAME,
$customerUser->getDomainId(),
);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Inquiry/Mail/InquiryMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public function __construct(
*/
public function sendMail(Inquiry $inquiry): void
{
$mailTemplate = $this->mailTemplateFacade->get(InquiryMail::ADMIN_MAIL_TEMPLATE_NAME, $inquiry->getDomainId());
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(InquiryMail::ADMIN_MAIL_TEMPLATE_NAME, $inquiry->getDomainId());
$this->sendMailTemplate($mailTemplate, $inquiry);

$mailTemplate = $this->mailTemplateFacade->get(InquiryMail::CUSTOMER_MAIL_TEMPLATE_NAME, $inquiry->getDomainId());
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(InquiryMail::CUSTOMER_MAIL_TEMPLATE_NAME, $inquiry->getDomainId());
$this->sendMailTemplate($mailTemplate, $inquiry);
}

Expand Down
21 changes: 14 additions & 7 deletions src/Model/Mail/MailTemplateFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ public function __construct(
* @param int $domainId
* @return \Shopsys\FrameworkBundle\Model\Mail\MailTemplate
*/
public function get($templateName, $domainId)
public function getWrappedWithGrapesJsBody(string $templateName, int $domainId): MailTemplate
{
$mailTemplate = $this->mailTemplateRepository->getByNameAndDomainId($templateName, $domainId);

if ($mailTemplate !== null) {
$mailTemplate->setBody($this->mailTemplateBuilder->getMailTemplateWithContent($domainId, $mailTemplate->getBody()));
$this->em->detach($mailTemplate);
}

return $mailTemplate;
return $this->getTemplateWrappedWithGrapesBody($mailTemplate);
}

/**
Expand Down Expand Up @@ -115,4 +110,16 @@ public function existsTemplateWithEnabledSendingHavingEmptyBodyOrSubject()
{
return $this->mailTemplateRepository->existsTemplateWithEnabledSendingHavingEmptyBodyOrSubject();
}

/**
* @param \Shopsys\FrameworkBundle\Model\Mail\MailTemplate $mailTemplate
* @return \Shopsys\FrameworkBundle\Model\Mail\MailTemplate
*/
public function getTemplateWrappedWithGrapesBody(MailTemplate $mailTemplate): MailTemplate
{
$mailTemplate->setBody($this->mailTemplateBuilder->getMailTemplateWithContent($mailTemplate->getDomainId(), $mailTemplate->getBody()));
$this->em->detach($mailTemplate); // detach from entity manager to avoid accidental persisting the changes to the database

return $mailTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
namespace Shopsys\FrameworkBundle\Model\Mail\MailTemplateSender;

use Shopsys\FrameworkBundle\Model\Mail\MailTemplate;
use Shopsys\FrameworkBundle\Model\Mail\MailTemplateFacade;
use Shopsys\FrameworkBundle\Model\Mail\MailTemplateSender\Exception\NoSenderForMailTemplateException;
use Traversable;

class MailTemplateSenderFacade
{
/**
* @param \Traversable<int, \Shopsys\FrameworkBundle\Model\Mail\MailTemplateSender\MailTemplateSenderInterface> $mailTemplateSenders
* @param \Shopsys\FrameworkBundle\Model\Mail\MailTemplateFacade $mailTemplateFacade
*/
public function __construct(
protected readonly Traversable $mailTemplateSenders,
protected readonly MailTemplateFacade $mailTemplateFacade,
) {
}

Expand All @@ -34,7 +37,11 @@ public function getFormLabelForEntityIdentifier(MailTemplate $mailTemplate): ?st
*/
public function sendMail(MailTemplate $mailTemplate, string $mailTo, ?int $entityId): void
{
$this->getTemplateSender($mailTemplate)->sendTemplate($mailTemplate, $mailTo, $entityId);
$this->getTemplateSender($mailTemplate)->sendTemplate(
$this->mailTemplateFacade->getTemplateWrappedWithGrapesBody($mailTemplate),
$mailTo,
$entityId,
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Order/Mail/OrderMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getMailTemplateByStatusAndDomainId(OrderStatus $orderStatus, $do
{
$templateName = OrderMail::getMailTemplateNameByStatus($orderStatus);

return $this->mailTemplateFacade->get($templateName, $domainId);
return $this->mailTemplateFacade->getWrappedWithGrapesJsBody($templateName, $domainId);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/PersonalData/Mail/PersonalDataAccessMailFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function __construct(
public function sendMail(PersonalDataAccessRequest $personalDataAccessRequest): void
{
if ($personalDataAccessRequest->getType() === PersonalDataAccessRequest::TYPE_DISPLAY) {
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
MailTemplate::PERSONAL_DATA_ACCESS_NAME,
$personalDataAccessRequest->getDomainId(),
);
} else {
$mailTemplate = $this->mailTemplateFacade->get(
$mailTemplate = $this->mailTemplateFacade->getWrappedWithGrapesJsBody(
MailTemplate::PERSONAL_DATA_EXPORT_NAME,
$personalDataAccessRequest->getDomainId(),
);
Expand Down

0 comments on commit c4e5533

Please sign in to comment.