mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Компонент для email'ов перемещён в common
This commit is contained in:
		
							
								
								
									
										56
									
								
								common/emails/EmailHelper.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								common/emails/EmailHelper.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails;
 | 
			
		||||
 | 
			
		||||
use common\emails\templates\ChangeEmailConfirmCurrentEmail;
 | 
			
		||||
use common\emails\templates\ChangeEmailConfirmNewEmail;
 | 
			
		||||
use common\emails\templates\ForgotPasswordEmail;
 | 
			
		||||
use common\emails\templates\ForgotPasswordParams;
 | 
			
		||||
use common\emails\templates\RegistrationEmail;
 | 
			
		||||
use common\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];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								common/emails/Template.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								common/emails/Template.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails;
 | 
			
		||||
 | 
			
		||||
use common\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());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										66
									
								
								common/emails/TemplateWithRenderer.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								common/emails/TemplateWithRenderer.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
<?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 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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								common/emails/exceptions/CannotSendEmailException.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								common/emails/exceptions/CannotSendEmailException.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails\exceptions;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
class CannotSendEmailException extends Exception {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								common/emails/templates/ChangeEmailConfirmCurrentEmail.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								common/emails/templates/ChangeEmailConfirmCurrentEmail.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails\templates;
 | 
			
		||||
 | 
			
		||||
use common\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' => '@api/mails/current-email-confirmation-html',
 | 
			
		||||
            'text' => '@api/mails/current-email-confirmation-text',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getParams(): array {
 | 
			
		||||
        return [
 | 
			
		||||
            'key' => $this->key,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								common/emails/templates/ChangeEmailConfirmNewEmail.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								common/emails/templates/ChangeEmailConfirmNewEmail.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails\templates;
 | 
			
		||||
 | 
			
		||||
use common\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' => '@api/mails/new-email-confirmation-html',
 | 
			
		||||
            'text' => '@api/mails/new-email-confirmation-text',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getParams(): array {
 | 
			
		||||
        return [
 | 
			
		||||
            'key' => $this->key,
 | 
			
		||||
            'username' => $this->username,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								common/emails/templates/ForgotPasswordEmail.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								common/emails/templates/ForgotPasswordEmail.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails\templates;
 | 
			
		||||
 | 
			
		||||
use common\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(),
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								common/emails/templates/ForgotPasswordParams.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								common/emails/templates/ForgotPasswordParams.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								common/emails/templates/RegistrationEmail.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								common/emails/templates/RegistrationEmail.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\emails\templates;
 | 
			
		||||
 | 
			
		||||
use common\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(),
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								common/emails/templates/RegistrationEmailParams.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								common/emails/templates/RegistrationEmailParams.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace common\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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user