new-support.webeffector.ru/common/components/user/handlers/UserCreateEventHandler.php

88 lines
2.6 KiB
PHP

<?php
namespace common\components\user\handlers;
use Yii;
use Exception;
use yii\base\Event;
use yii\web\Application;
use common\helpers\UserHelper;
use common\components\user\models\User;
use common\components\user\models\UserAdmin;
use common\components\user\models\UserCustomer;
use common\components\user\models\UserEmployee;
class UserCreateEventHandler
{
static public function run(Event $event)
{
if (Yii::$app instanceof Application) {
/** @var User $user */
$user = $event->sender;
if (!$user || !$user->email) {
try {
self::sendTelegram($user, self::errorMessage());
} catch (Exception $e) {
}
return;
}
$userMeta = new UserCustomer();
$auth = Yii::$app->authManager;
if ($user->type == UserHelper::TYPE_ADMIN) {
$userMeta = new UserAdmin();
} elseif ($user->type == UserHelper::TYPE_EMPLOYEE) {
$userMeta = new UserEmployee();
} elseif ($user->type == UserHelper::TYPE_EMPLOYEE) {
$userMeta = new UserEmployee();
}
$authRole = $auth->getRole($user->role);
$auth->assign($authRole, $user->getId());
$userMeta->first_name = $user->email;
$userMeta->link('user', $user);
$verifyLink = Yii::$app->urlManager->createAbsoluteUrl(['/verify-email', 'token' => $user->verification_token]);
if(!YII_ENV_PROD) {
Yii::$app->telegram->sendMessage(Yii::$app->params['telegram']['accountExpirationChatId'], "Ссылка для верификации (test): " . $verifyLink);
}
try {
self::sendTelegram($user, self::successMessage($user));
} catch (Exception $e) {
//todo mock
}
}
}
static private function sendEmail($email): bool
{
// todo mock
return true;
}
static private function sendTelegram(User $user, $message)
{
Yii::$app->telegram->sendMessage(Yii::$app->params['telegram']['accountExpirationChatId'], $message);
}
static private function errorMessage(): string
{
return Yii::t('user', 'Ошибка при создании пользователя');
}
static private function successMessage(User $user): string
{
return Yii::t('user', 'Новый аккаунт {role} создан: {email}', [
'role' => UserHelper::roleNameList($user->role),
'email' => $user->email,
]);
}
}