2017-04-21 04:11:43 +05:30
|
|
|
<?php
|
2019-06-07 04:46:13 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-04-24 21:52:24 +05:30
|
|
|
namespace common\emails;
|
2017-04-21 04:11:43 +05:30
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
use Exception;
|
2019-06-17 02:29:19 +05:30
|
|
|
use yii\mail\MailerInterface;
|
2017-04-21 04:11:43 +05:30
|
|
|
use yii\mail\MessageInterface;
|
|
|
|
|
|
|
|
abstract class TemplateWithRenderer extends Template {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
private string $locale = 'en';
|
2017-04-21 04:11:43 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
MailerInterface $mailer,
|
|
|
|
private readonly RendererInterface $renderer,
|
|
|
|
) {
|
2019-06-17 02:29:19 +05:30
|
|
|
parent::__construct($mailer);
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
public function setLocale(string $locale): void {
|
|
|
|
$this->locale = $locale;
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
public function getLocale(): string {
|
|
|
|
return $this->locale;
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-17 02:29:19 +05:30
|
|
|
* This method should return the template's name, which will be rendered.
|
|
|
|
* List of available templates names can be found at https://github.com/elyby/emails-renderer
|
2017-04-21 04:11:43 +05:30
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-25 04:39:59 +05:30
|
|
|
abstract public function getTemplateName(): string;
|
2017-04-21 04:11:43 +05:30
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
final protected function getRenderer(): RendererInterface {
|
|
|
|
return $this->renderer;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final protected function getView(): string {
|
2017-04-21 04:11:43 +05:30
|
|
|
return $this->getTemplateName();
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
/**
|
2019-06-17 02:29:19 +05:30
|
|
|
* @param string|array $for
|
|
|
|
*
|
2019-06-07 04:46:13 +05:30
|
|
|
* @return MessageInterface
|
2019-06-17 02:29:19 +05:30
|
|
|
* @throws \common\emails\exceptions\CannotRenderEmailException
|
2019-06-07 04:46:13 +05:30
|
|
|
*/
|
2019-06-17 02:29:19 +05:30
|
|
|
protected function createMessage($for): MessageInterface {
|
2017-04-21 04:11:43 +05:30
|
|
|
return $this->getMailer()
|
|
|
|
->compose()
|
|
|
|
->setHtmlBody($this->render())
|
2019-06-17 02:29:19 +05:30
|
|
|
->setTo($for)
|
2017-04-21 04:11:43 +05:30
|
|
|
->setFrom($this->getFrom())
|
|
|
|
->setSubject($this->getSubject());
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
/**
|
|
|
|
* @return string
|
2019-06-17 02:29:19 +05:30
|
|
|
* @throws \common\emails\exceptions\CannotRenderEmailException
|
2019-06-07 04:46:13 +05:30
|
|
|
*/
|
2017-04-21 04:11:43 +05:30
|
|
|
private function render(): string {
|
2019-06-07 04:46:13 +05:30
|
|
|
try {
|
|
|
|
return $this->getRenderer()->render($this->getTemplateName(), $this->getLocale(), $this->getParams());
|
|
|
|
} catch (Exception $e) {
|
2019-06-17 02:29:19 +05:30
|
|
|
throw new exceptions\CannotRenderEmailException($e);
|
2019-06-07 04:46:13 +05:30
|
|
|
}
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|