|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Shopsys\FrameworkBundle\Model\ProductQuestion\Mail; |
| 6 | + |
| 7 | +use Shopsys\FrameworkBundle\Component\Mailer\MailerHelper; |
| 8 | +use Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory; |
| 9 | +use Shopsys\FrameworkBundle\Model\Mail\MailTemplate; |
| 10 | +use Shopsys\FrameworkBundle\Model\Mail\MessageData; |
| 11 | +use Shopsys\FrameworkBundle\Model\Mail\Setting\MailSettingFacade; |
| 12 | +use Shopsys\FrameworkBundle\Model\ProductQuestion\ProductQuestionData; |
| 13 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 14 | + |
| 15 | +class ProductQuestionMail |
| 16 | +{ |
| 17 | + public const string CUSTOMER_MAIL_TEMPLATE_NAME = 'product_question_customer'; |
| 18 | + public const string ADMIN_MAIL_TEMPLATE_NAME = 'product_question_admin'; |
| 19 | + |
| 20 | + public const string VARIABLE_CUSTOMER_NAME = '{customerName}'; |
| 21 | + public const string VARIABLE_EMAIL = '{email}'; |
| 22 | + public const string VARIABLE_QUESTION = '{question}'; |
| 23 | + public const string VARIABLE_PRODUCT_NAME = '{productName}'; |
| 24 | + public const string VARIABLE_PRODUCT_URL = '{productUrl}'; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + protected readonly MailSettingFacade $mailSettingFacade, |
| 28 | + protected readonly DomainRouterFactory $domainRouterFactory, |
| 29 | + protected readonly MailerHelper $mailerHelper, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function createMessageForAdmin( |
| 34 | + MailTemplate $template, |
| 35 | + ProductQuestionData $productQuestionData, |
| 36 | + ): MessageData { |
| 37 | + $mainAdminMail = $this->mailSettingFacade->getMainAdminMail($productQuestionData->domainId); |
| 38 | + |
| 39 | + return new MessageData( |
| 40 | + $mainAdminMail, |
| 41 | + $template->getBccEmail(), |
| 42 | + $template->getBody(), |
| 43 | + $template->getSubject(), |
| 44 | + $mainAdminMail, |
| 45 | + $this->mailSettingFacade->getMainAdminMailName($productQuestionData->domainId), |
| 46 | + $this->getBodyVariablesReplacements($productQuestionData), |
| 47 | + $this->getSubjectVariablesReplacements($productQuestionData), |
| 48 | + replyTo: $productQuestionData->email, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public function createMessageForCustomer( |
| 53 | + MailTemplate $template, |
| 54 | + ProductQuestionData $productQuestionData, |
| 55 | + ): MessageData { |
| 56 | + return new MessageData( |
| 57 | + $productQuestionData->email, |
| 58 | + $template->getBccEmail(), |
| 59 | + $template->getBody(), |
| 60 | + $template->getSubject(), |
| 61 | + $this->mailSettingFacade->getMainAdminMail($productQuestionData->domainId), |
| 62 | + $this->mailSettingFacade->getMainAdminMailName($productQuestionData->domainId), |
| 63 | + $this->getBodyVariablesReplacements($productQuestionData), |
| 64 | + $this->getSubjectVariablesReplacements($productQuestionData), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return array<string, \Closure> |
| 70 | + */ |
| 71 | + protected function getSubjectVariablesReplacements(ProductQuestionData $productQuestionData): array |
| 72 | + { |
| 73 | + return [ |
| 74 | + self::VARIABLE_CUSTOMER_NAME => fn () => $this->mailerHelper->escapeString($productQuestionData->customerName), |
| 75 | + self::VARIABLE_EMAIL => fn () => $this->mailerHelper->escapeString($productQuestionData->email), |
| 76 | + self::VARIABLE_PRODUCT_NAME => fn () => $productQuestionData->product->getName(), |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array<string, \Closure> |
| 82 | + */ |
| 83 | + protected function getBodyVariablesReplacements(ProductQuestionData $productQuestionData): array |
| 84 | + { |
| 85 | + return [ |
| 86 | + ...$this->getSubjectVariablesReplacements($productQuestionData), |
| 87 | + self::VARIABLE_QUESTION => fn () => $this->mailerHelper->escapeString($productQuestionData->question), |
| 88 | + self::VARIABLE_PRODUCT_URL => fn () => $this->getProductUrl($productQuestionData), |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + protected function getProductUrl(ProductQuestionData $productQuestionData): string |
| 93 | + { |
| 94 | + return $this->domainRouterFactory->getRouter($productQuestionData->domainId)->generate( |
| 95 | + 'front_product_detail', |
| 96 | + ['id' => $productQuestionData->product->getId()], |
| 97 | + UrlGeneratorInterface::ABSOLUTE_URL, |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments