mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Refactor emails models objects, rework related tests
This commit is contained in:
10
common/emails/RendererInterface.php
Normal file
10
common/emails/RendererInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\emails;
|
||||
|
||||
interface RendererInterface {
|
||||
|
||||
public function render(string $templateName, string $locale, array $params = []): string;
|
||||
|
||||
}
|
@@ -3,7 +3,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\emails;
|
||||
|
||||
use common\emails\exceptions\CannotSendEmailException;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\mail\MailerInterface;
|
||||
@@ -12,29 +11,12 @@ use yii\mail\MessageInterface;
|
||||
abstract class Template {
|
||||
|
||||
/**
|
||||
* @var \yii\swiftmailer\Mailer
|
||||
* @var MailerInterface
|
||||
*/
|
||||
private $mailer;
|
||||
|
||||
/**
|
||||
* @var string|array
|
||||
*/
|
||||
private $to;
|
||||
|
||||
/**
|
||||
* @param string|array $to message receiver. Can be passed as string (pure email)
|
||||
* or as an array [email => user's name]
|
||||
*/
|
||||
public function __construct($to) {
|
||||
$this->mailer = Yii::$app->mailer;
|
||||
$this->to = $to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function getTo() {
|
||||
return $this->to;
|
||||
public function __construct(MailerInterface $mailer) {
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
abstract public function getSubject(): string;
|
||||
@@ -44,7 +26,7 @@ abstract class Template {
|
||||
* @throws InvalidConfigException
|
||||
*/
|
||||
public function getFrom() {
|
||||
$fromEmail = Yii::$app->params['fromEmail'];
|
||||
$fromEmail = Yii::$app->params['fromEmail'] ?? '';
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||
}
|
||||
@@ -56,13 +38,14 @@ abstract class Template {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getMailer(): MailerInterface {
|
||||
return $this->mailer;
|
||||
}
|
||||
|
||||
public function send(): void {
|
||||
if (!$this->createMessage()->send()) {
|
||||
throw new CannotSendEmailException('Unable send email.');
|
||||
/**
|
||||
* @param string|array $to see \yii\mail\MessageInterface::setTo to know the format.
|
||||
*
|
||||
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||
*/
|
||||
public function send($to): void {
|
||||
if (!$this->createMessage($to)->send()) {
|
||||
throw new exceptions\CannotSendEmailException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,10 +54,14 @@ abstract class Template {
|
||||
*/
|
||||
abstract protected function getView();
|
||||
|
||||
protected function createMessage(): MessageInterface {
|
||||
final protected function getMailer(): MailerInterface {
|
||||
return $this->mailer;
|
||||
}
|
||||
|
||||
protected function createMessage($for): MessageInterface {
|
||||
return $this->getMailer()
|
||||
->compose($this->getView(), $this->getParams())
|
||||
->setTo($this->getTo())
|
||||
->setTo($for)
|
||||
->setFrom($this->getFrom())
|
||||
->setSubject($this->getSubject());
|
||||
}
|
||||
|
@@ -3,9 +3,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\emails;
|
||||
|
||||
use common\components\EmailsRenderer\RendererInterface;
|
||||
use ErrorException;
|
||||
use Exception;
|
||||
use yii\mail\MailerInterface;
|
||||
use yii\mail\MessageInterface;
|
||||
|
||||
abstract class TemplateWithRenderer extends Template {
|
||||
@@ -18,59 +17,61 @@ abstract class TemplateWithRenderer extends Template {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $locale;
|
||||
private $locale = 'en';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __construct($to, string $locale, RendererInterface $renderer) {
|
||||
parent::__construct($to);
|
||||
$this->locale = $locale;
|
||||
public function __construct(MailerInterface $mailer, RendererInterface $renderer) {
|
||||
parent::__construct($mailer);
|
||||
$this->renderer = $renderer;
|
||||
}
|
||||
|
||||
public function setLocale(string $locale): void {
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function getLocale(): string {
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
public function getRenderer(): RendererInterface {
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод должен возвращать имя шаблона, который должен быть использован.
|
||||
* Имена можно взять в репозитории elyby/email-renderer
|
||||
* 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
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getTemplateName(): string;
|
||||
|
||||
final protected function getRenderer(): RendererInterface {
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
final protected function getView() {
|
||||
return $this->getTemplateName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $for
|
||||
*
|
||||
* @return MessageInterface
|
||||
* @throws ErrorException
|
||||
* @throws \common\emails\exceptions\CannotRenderEmailException
|
||||
*/
|
||||
protected function createMessage(): MessageInterface {
|
||||
protected function createMessage($for): MessageInterface {
|
||||
return $this->getMailer()
|
||||
->compose()
|
||||
->setHtmlBody($this->render())
|
||||
->setTo($this->getTo())
|
||||
->setTo($for)
|
||||
->setFrom($this->getFrom())
|
||||
->setSubject($this->getSubject());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws ErrorException
|
||||
* @throws \common\emails\exceptions\CannotRenderEmailException
|
||||
*/
|
||||
private function render(): string {
|
||||
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);
|
||||
throw new exceptions\CannotRenderEmailException($e);
|
||||
}
|
||||
}
|
||||
|
||||
|
15
common/emails/exceptions/CannotRenderEmailException.php
Normal file
15
common/emails/exceptions/CannotRenderEmailException.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\emails\exceptions;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class CannotRenderEmailException extends Exception {
|
||||
|
||||
public function __construct(Throwable $previous = null) {
|
||||
parent::__construct('Unable to render a template', 0, $previous);
|
||||
}
|
||||
|
||||
}
|
@@ -4,7 +4,12 @@ declare(strict_types=1);
|
||||
namespace common\emails\exceptions;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class CannotSendEmailException extends Exception {
|
||||
|
||||
public function __construct(Throwable $previous = null) {
|
||||
parent::__construct('Unable send email', 0, $previous);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,13 +4,16 @@ declare(strict_types=1);
|
||||
namespace common\emails\templates;
|
||||
|
||||
use common\emails\Template;
|
||||
use yii\base\InvalidCallException;
|
||||
|
||||
class ChangeEmailConfirmCurrentEmail extends Template {
|
||||
class ChangeEmail extends Template {
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $key;
|
||||
|
||||
public function __construct($to, string $key) {
|
||||
parent::__construct($to);
|
||||
public function setKey(string $key): void {
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
@@ -19,14 +22,15 @@ class ChangeEmailConfirmCurrentEmail extends Template {
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
if ($this->key === null) {
|
||||
throw new InvalidCallException('You need to set key param first');
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $this->key,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*/
|
||||
protected function getView() {
|
||||
return [
|
||||
'html' => '@common/emails/views/current-email-confirmation-html',
|
@@ -4,16 +4,25 @@ declare(strict_types=1);
|
||||
namespace common\emails\templates;
|
||||
|
||||
use common\emails\Template;
|
||||
use yii\base\InvalidCallException;
|
||||
|
||||
class ChangeEmailConfirmNewEmail extends Template {
|
||||
class ConfirmNewEmail extends Template {
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $key;
|
||||
|
||||
public function __construct($to, string $username, string $key) {
|
||||
parent::__construct($to);
|
||||
public function setUsername(string $username): void {
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function setKey(string $key): void {
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
@@ -22,15 +31,16 @@ class ChangeEmailConfirmNewEmail extends Template {
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
if ($this->username === null || $this->key === null) {
|
||||
throw new InvalidCallException('You need to set username and key params first');
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $this->key,
|
||||
'username' => $this->username,
|
||||
'key' => $this->key,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*/
|
||||
protected function getView() {
|
||||
return [
|
||||
'html' => '@common/emails/views/new-email-confirmation-html',
|
@@ -3,20 +3,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\emails\templates;
|
||||
|
||||
use common\components\EmailsRenderer\RendererInterface;
|
||||
use common\emails\TemplateWithRenderer;
|
||||
use yii\base\InvalidCallException;
|
||||
|
||||
class ForgotPasswordEmail extends TemplateWithRenderer {
|
||||
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @var ForgotPasswordParams|null
|
||||
*/
|
||||
public function __construct($to, string $locale, ForgotPasswordParams $params, RendererInterface $renderer) {
|
||||
parent::__construct($to, $locale, $renderer);
|
||||
$this->params = $params;
|
||||
}
|
||||
private $params;
|
||||
|
||||
public function getSubject(): string {
|
||||
return 'Ely.by Account forgot password';
|
||||
@@ -26,7 +21,15 @@ class ForgotPasswordEmail extends TemplateWithRenderer {
|
||||
return 'forgotPassword';
|
||||
}
|
||||
|
||||
public function setParams(ForgotPasswordParams $params): void {
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
if ($this->params === null) {
|
||||
throw new InvalidCallException('You need to set params first');
|
||||
}
|
||||
|
||||
return [
|
||||
'username' => $this->params->getUsername(),
|
||||
'code' => $this->params->getCode(),
|
||||
|
@@ -3,20 +3,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\emails\templates;
|
||||
|
||||
use common\components\EmailsRenderer\RendererInterface;
|
||||
use common\emails\TemplateWithRenderer;
|
||||
use yii\base\InvalidCallException;
|
||||
|
||||
class RegistrationEmail extends TemplateWithRenderer {
|
||||
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @var RegistrationEmailParams|null
|
||||
*/
|
||||
public function __construct($to, string $locale, RegistrationEmailParams $params, RendererInterface $renderer) {
|
||||
parent::__construct($to, $locale, $renderer);
|
||||
$this->params = $params;
|
||||
}
|
||||
private $params;
|
||||
|
||||
public function getSubject(): string {
|
||||
return 'Ely.by Account registration';
|
||||
@@ -26,7 +21,15 @@ class RegistrationEmail extends TemplateWithRenderer {
|
||||
return 'register';
|
||||
}
|
||||
|
||||
public function setParams(RegistrationEmailParams $params): void {
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
if ($this->params === null) {
|
||||
throw new InvalidCallException('You need to set params first');
|
||||
}
|
||||
|
||||
return [
|
||||
'username' => $this->params->getUsername(),
|
||||
'code' => $this->params->getCode(),
|
||||
|
Reference in New Issue
Block a user