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 common\components\EmailsRenderer\RendererInterface;
|
|
|
|
use ErrorException;
|
|
|
|
use Exception;
|
2017-04-21 04:11:43 +05:30
|
|
|
use yii\mail\MessageInterface;
|
|
|
|
|
|
|
|
abstract class TemplateWithRenderer extends Template {
|
|
|
|
|
|
|
|
/**
|
2019-06-07 04:46:13 +05:30
|
|
|
* @var RendererInterface
|
2017-04-21 04:11:43 +05:30
|
|
|
*/
|
2019-06-07 04:46:13 +05:30
|
|
|
private $renderer;
|
2017-04-21 04:11:43 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2019-06-07 04:46:13 +05:30
|
|
|
public function __construct($to, string $locale, RendererInterface $renderer) {
|
2017-04-21 04:11:43 +05:30
|
|
|
parent::__construct($to);
|
|
|
|
$this->locale = $locale;
|
2019-06-07 04:46:13 +05:30
|
|
|
$this->renderer = $renderer;
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getLocale(): string {
|
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
public function getRenderer(): RendererInterface {
|
|
|
|
return $this->renderer;
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Метод должен возвращать имя шаблона, который должен быть использован.
|
|
|
|
* Имена можно взять в репозитории elyby/email-renderer
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-25 04:39:59 +05:30
|
|
|
abstract public function getTemplateName(): string;
|
2017-04-21 04:11:43 +05:30
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
final protected function getView() {
|
2017-04-21 04:11:43 +05:30
|
|
|
return $this->getTemplateName();
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
/**
|
|
|
|
* @return MessageInterface
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
2017-04-21 04:11:43 +05:30
|
|
|
protected function createMessage(): MessageInterface {
|
|
|
|
return $this->getMailer()
|
|
|
|
->compose()
|
|
|
|
->setHtmlBody($this->render())
|
|
|
|
->setTo($this->getTo())
|
|
|
|
->setFrom($this->getFrom())
|
|
|
|
->setSubject($this->getSubject());
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:46:13 +05:30
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
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) {
|
|
|
|
throw new ErrorException('Unable to render the template', 0, 1, __FILE__, __LINE__, $e);
|
|
|
|
}
|
2017-04-21 04:11:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|