accounts/common/emails/TemplateWithRenderer.php
2018-04-17 23:47:25 +03:00

67 lines
1.6 KiB
PHP

<?php
namespace common\emails;
use common\components\EmailRenderer;
use Yii;
use yii\mail\MessageInterface;
abstract class TemplateWithRenderer extends Template {
/**
* @var EmailRenderer
*/
private $emailRenderer;
/**
* @var string
*/
private $locale;
/**
* @inheritdoc
*/
public function __construct($to, string $locale) {
parent::__construct($to);
$this->emailRenderer = Yii::$app->emailRenderer;
$this->locale = $locale;
}
public function getLocale(): string {
return $this->locale;
}
public function getEmailRenderer(): EmailRenderer {
return $this->emailRenderer;
}
/**
* Метод должен возвращать имя шаблона, который должен быть использован.
* Имена можно взять в репозитории elyby/email-renderer
*
* @return string
*/
abstract public function getTemplateName(): string;
final protected function getView() {
return $this->getTemplateName();
}
protected function createMessage(): MessageInterface {
return $this->getMailer()
->compose()
->setHtmlBody($this->render())
->setTo($this->getTo())
->setFrom($this->getFrom())
->setSubject($this->getSubject());
}
private function render(): string {
return $this->getEmailRenderer()
->getTemplate($this->getTemplateName())
->setLocale($this->getLocale())
->setParams($this->getParams())
->render();
}
}