Some checks failed
Build / build (ubuntu-latest, 7.4) (push) Has been cancelled
Build / build (ubuntu-latest, 8.0) (push) Has been cancelled
Build / build (ubuntu-latest, 8.1) (push) Has been cancelled
Build / build (ubuntu-latest, 8.2) (push) Has been cancelled
Build / build (ubuntu-latest, 8.3) (push) Has been cancelled
Build / build (ubuntu-latest, 8.4) (push) Has been cancelled
All PHP namespaces and related references have been updated from Da\User to pcrt\User throughout the codebase, including composer.json autoload settings and all source files. This change standardizes the project under the new pcrt\User namespace for improved clarity and maintainability.
165 lines
4.8 KiB
PHP
165 lines
4.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the 2amigos/yii2-usuario project.
|
|
*
|
|
* (c) 2amigOS! <http://2amigos.us/>
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace pcrt\User\Factory;
|
|
|
|
use pcrt\User\Event\MailEvent;
|
|
use pcrt\User\Model\Token;
|
|
use pcrt\User\Model\User;
|
|
use pcrt\User\Module;
|
|
use pcrt\User\Service\MailService;
|
|
use Yii;
|
|
use yii\base\InvalidConfigException;
|
|
|
|
class MailFactory
|
|
{
|
|
/**
|
|
* @param User $user
|
|
* @param bool $showPassword
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*/
|
|
public static function makeWelcomeMailerService(User $user, $showPassword = false)
|
|
{
|
|
/** @var Module $module */
|
|
$module = Yii::$app->getModule('user');
|
|
$to = $user->email;
|
|
$from = $module->mailParams['fromEmail'];
|
|
$subject = $module->mailParams['welcomeMailSubject'];
|
|
$params = [
|
|
'user' => $user,
|
|
'token' => null,
|
|
'module' => $module,
|
|
'showPassword' => $showPassword,
|
|
];
|
|
|
|
return static::makeMailerService(MailEvent::TYPE_WELCOME, $from, $to, $subject, 'welcome', $params);
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @param Token $token
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*/
|
|
public static function makeRecoveryMailerService($email, ?Token $token = null)
|
|
{
|
|
/** @var Module $module */
|
|
$module = Yii::$app->getModule('user');
|
|
$to = $email;
|
|
$from = $module->mailParams['fromEmail'];
|
|
$subject = $module->mailParams['recoveryMailSubject'];
|
|
$params = [
|
|
'user' => $token && $token->user ? $token->user : null,
|
|
'token' => $token,
|
|
];
|
|
|
|
return static::makeMailerService(MailEvent::TYPE_RECOVERY, $from, $to, $subject, 'recovery', $params);
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param Token|null $token
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*/
|
|
public static function makeConfirmationMailerService(User $user, ?Token $token = null)
|
|
{
|
|
/** @var Module $module */
|
|
$module = Yii::$app->getModule('user');
|
|
$to = $user->email;
|
|
$from = $module->mailParams['fromEmail'];
|
|
$subject = $module->mailParams['confirmationMailSubject'];
|
|
$params = [
|
|
'user' => $token && $token->user ? $token->user : null,
|
|
'token' => $token,
|
|
];
|
|
|
|
return static::makeMailerService(MailEvent::TYPE_CONFIRM, $from, $to, $subject, 'confirmation', $params);
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param Token $token
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*/
|
|
public static function makeReconfirmationMailerService(User $user, Token $token)
|
|
{
|
|
/** @var Module $module */
|
|
$module = Yii::$app->getModule('user');
|
|
$to = $token->type === Token::TYPE_CONFIRM_NEW_EMAIL
|
|
? $user->unconfirmed_email
|
|
: $user->email;
|
|
|
|
$from = $module->mailParams['fromEmail'];
|
|
$subject = $module->mailParams['reconfirmationMailSubject'];
|
|
$params = [
|
|
'user' => $token && $token->user ? $token->user : null,
|
|
'token' => $token,
|
|
];
|
|
|
|
return static::makeMailerService(MailEvent::TYPE_RECONFIRM, $from, $to, $subject, 'reconfirmation', $params);
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param String $code
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*/
|
|
public static function makeTwoFactorCodeMailerService(User $user, String $code)
|
|
{
|
|
/** @var Module $module */
|
|
$module = Yii::$app->getModule('user');
|
|
$to = $user->email;
|
|
|
|
$from = $module->mailParams['fromEmail'];
|
|
$subject = $module->mailParams['twoFactorMailSubject'];
|
|
$params = [
|
|
'code' => $code,
|
|
];
|
|
|
|
return static::makeMailerService(MailEvent::TYPE_TWOFACTORCODE, $from, $to, $subject, 'twofactorcode', $params);
|
|
}
|
|
|
|
/**
|
|
* Builds a MailerService.
|
|
*
|
|
* @param string $type
|
|
* @param string|array|\Closure $from
|
|
* @param string $to
|
|
* @param string $subject
|
|
* @param string $view
|
|
* @param array $params
|
|
*
|
|
* @throws InvalidConfigException
|
|
* @return MailService
|
|
*
|
|
*/
|
|
public static function makeMailerService($type, $from, $to, $subject, $view, $params = [])
|
|
{
|
|
if ($from instanceof \Closure) {
|
|
$from = $from($type);
|
|
}
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */
|
|
return Yii::$container->get(
|
|
MailService::class,
|
|
[$type, $from, $to, $subject, $view, $params, Yii::$app->getMailer()]
|
|
);
|
|
}
|
|
}
|