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 {
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2019-06-17 02:29:19 +05:30
|
|
|
private $locale = 'en';
|
2017-04-21 04:11:43 +05:30
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
public function __construct(MailerInterface $mailer, RendererInterface $renderer) {
|
|
|
|
parent::__construct($mailer);
|
2019-06-07 04:46:13 +05:30
|
|
|
$this->renderer = $renderer;
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|