Компонент для email'ов перемещён в common

This commit is contained in:
ErickSkrauch
2017-04-24 19:22:24 +03:00
parent c0780736ca
commit 1d5e0ce2c6
15 changed files with 30 additions and 30 deletions

View File

@@ -1,56 +0,0 @@
<?php
namespace api\emails;
use api\emails\templates\ChangeEmailConfirmCurrentEmail;
use api\emails\templates\ChangeEmailConfirmNewEmail;
use api\emails\templates\ForgotPasswordEmail;
use api\emails\templates\ForgotPasswordParams;
use api\emails\templates\RegistrationEmail;
use api\emails\templates\RegistrationEmailParams;
use common\models\Account;
use common\models\confirmations\CurrentEmailConfirmation;
use common\models\confirmations\ForgotPassword;
use common\models\confirmations\NewEmailConfirmation;
use common\models\confirmations\RegistrationConfirmation;
use Yii;
class EmailHelper {
public static function registration(RegistrationConfirmation $emailActivation): void {
$account = $emailActivation->account;
$locale = $account->lang;
$params = new RegistrationEmailParams(
$account->username,
$emailActivation->key,
Yii::$app->request->getHostInfo() . '/activation/' . $emailActivation->key
);
(new RegistrationEmail(self::buildTo($account), $locale, $params))->send();
}
public static function forgotPassword(ForgotPassword $emailActivation): void {
$account = $emailActivation->account;
$locale = $account->lang;
$params = new ForgotPasswordParams(
$account->username,
$emailActivation->key,
Yii::$app->request->getHostInfo() . '/recover-password/' . $emailActivation->key
);
(new ForgotPasswordEmail(self::buildTo($account), $locale, $params))->send();
}
public static function changeEmailConfirmCurrent(CurrentEmailConfirmation $emailActivation): void {
(new ChangeEmailConfirmCurrentEmail(self::buildTo($emailActivation->account), $emailActivation->key))->send();
}
public static function changeEmailConfirmNew(NewEmailConfirmation $emailActivation): void {
$account = $emailActivation->account;
(new ChangeEmailConfirmNewEmail(self::buildTo($account), $account->username, $emailActivation->key))->send();
}
public static function buildTo(Account $account): array {
return [$account->email => $account->username];
}
}

View File

@@ -1,79 +0,0 @@
<?php
namespace api\emails;
use api\emails\exceptions\CannotSendEmailException;
use Yii;
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
abstract class Template {
/**
* @var \yii\swiftmailer\Mailer
*/
private $mailer;
/**
* @var string|array
*/
private $to;
/**
* @param string|array $to получатель письма. Задаётся как Email или как массив [email => name]
*/
public function __construct($to) {
$this->mailer = Yii::$app->mailer;
$this->to = $to;
}
/**
* @return array|string
*/
public function getTo() {
return $this->to;
}
abstract public function getSubject(): string;
/**
* @return array|string
* @throws InvalidConfigException
*/
public function getFrom() {
$fromEmail = Yii::$app->params['fromEmail'];
if (!$fromEmail) {
throw new InvalidConfigException('Please specify fromEmail app in app params');
}
return [$fromEmail => 'Ely.by Accounts'];
}
public function getParams(): array {
return [];
}
public function getMailer(): MailerInterface {
return $this->mailer;
}
public function send(): void {
if (!$this->createMessage()->send()) {
throw new CannotSendEmailException('Unable send email.');
}
}
/**
* @return string|array
*/
abstract protected function getView();
protected function createMessage(): MessageInterface {
return $this->getMailer()
->compose($this->getView(), $this->getParams())
->setTo($this->getTo())
->setFrom($this->getFrom())
->setSubject($this->getSubject());
}
}

View File

@@ -1,66 +0,0 @@
<?php
namespace api\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 protected function getTemplateName(): string;
protected final 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();
}
}

View File

@@ -1,8 +0,0 @@
<?php
namespace api\emails\exceptions;
use Exception;
class CannotSendEmailException extends Exception {
}

View File

@@ -1,35 +0,0 @@
<?php
namespace api\emails\templates;
use api\emails\Template;
class ChangeEmailConfirmCurrentEmail extends Template {
private $key;
public function __construct($to, string $key) {
parent::__construct($to);
$this->key = $key;
}
public function getSubject(): string {
return 'Ely.by Account change E-mail confirmation';
}
/**
* @return string|array
*/
protected function getView() {
return [
'html' => '@app/mails/current-email-confirmation-html',
'text' => '@app/mails/current-email-confirmation-text',
];
}
public function getParams(): array {
return [
'key' => $this->key,
];
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace api\emails\templates;
use api\emails\Template;
class ChangeEmailConfirmNewEmail extends Template {
private $username;
private $key;
public function __construct($to, string $username, string $key) {
parent::__construct($to);
$this->username = $username;
$this->key = $key;
}
public function getSubject(): string {
return 'Ely.by Account new E-mail confirmation';
}
/**
* @return string|array
*/
protected function getView() {
return [
'html' => '@app/mails/new-email-confirmation-html',
'text' => '@app/mails/new-email-confirmation-text',
];
}
public function getParams(): array {
return [
'key' => $this->key,
'username' => $this->username,
];
}
}

View File

@@ -1,34 +0,0 @@
<?php
namespace api\emails\templates;
use api\emails\TemplateWithRenderer;
class ForgotPasswordEmail extends TemplateWithRenderer {
private $params;
/**
* @inheritdoc
*/
public function __construct($to, string $locale, ForgotPasswordParams $params) {
TemplateWithRenderer::__construct($to, $locale);
$this->params = $params;
}
public function getSubject(): string {
return 'Ely.by Account forgot password';
}
protected function getTemplateName(): string {
return 'forgotPassword';
}
public function getParams(): array {
return [
'username' => $this->params->getUsername(),
'code' => $this->params->getCode(),
'link' => $this->params->getLink(),
];
}
}

View File

@@ -1,30 +0,0 @@
<?php
namespace api\emails\templates;
class ForgotPasswordParams {
private $username;
private $code;
private $link;
public function __construct(string $username, string $code, string $link) {
$this->username = $username;
$this->code = $code;
$this->link = $code;
}
public function getUsername(): string {
return $this->username;
}
public function getCode(): string {
return $this->code;
}
public function getLink(): string {
return $this->link;
}
}

View File

@@ -1,34 +0,0 @@
<?php
namespace api\emails\templates;
use api\emails\TemplateWithRenderer;
class RegistrationEmail extends TemplateWithRenderer {
private $params;
/**
* @inheritdoc
*/
public function __construct($to, string $locale, RegistrationEmailParams $params) {
TemplateWithRenderer::__construct($to, $locale);
$this->params = $params;
}
public function getSubject(): string {
return 'Ely.by Account registration';
}
protected function getTemplateName(): string {
return 'register';
}
public function getParams(): array {
return [
'username' => $this->params->getUsername(),
'code' => $this->params->getCode(),
'link' => $this->params->getLink(),
];
}
}

View File

@@ -1,30 +0,0 @@
<?php
namespace api\emails\templates;
class RegistrationEmailParams {
private $username;
private $code;
private $link;
public function __construct(string $username, string $code, string $link) {
$this->username = $username;
$this->code = $code;
$this->link = $code;
}
public function getUsername(): string {
return $this->username;
}
public function getCode(): string {
return $this->code;
}
public function getLink(): string {
return $this->link;
}
}

View File

@@ -1,7 +1,7 @@
<?php
namespace api\models\authentication;
use api\emails\EmailHelper;
use common\emails\EmailHelper;
use api\models\base\ApiForm;
use api\validators\TotpValidator;
use common\helpers\Error as E;

View File

@@ -2,7 +2,7 @@
namespace api\models\authentication;
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use api\emails\EmailHelper;
use common\emails\EmailHelper;
use api\models\base\ApiForm;
use common\helpers\Error as E;
use common\components\UserFriendlyRandomKey;

View File

@@ -2,7 +2,7 @@
namespace api\models\authentication;
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use api\emails\EmailHelper;
use common\emails\EmailHelper;
use api\models\base\ApiForm;
use common\helpers\Error as E;
use common\components\UserFriendlyRandomKey;

View File

@@ -1,7 +1,7 @@
<?php
namespace api\models\profile\ChangeEmail;
use api\emails\EmailHelper;
use common\emails\EmailHelper;
use api\models\base\ApiForm;
use api\validators\PasswordRequiredValidator;
use common\helpers\Error as E;

View File

@@ -1,7 +1,7 @@
<?php
namespace api\models\profile\ChangeEmail;
use api\emails\EmailHelper;
use common\emails\EmailHelper;
use api\models\base\ApiForm;
use api\validators\EmailActivationKeyValidator;
use common\models\Account;