From 61085a13620188fa46479ce96a7ea3722e8f40e8 Mon Sep 17 00:00:00 2001 From: iIronside Date: Wed, 18 Oct 2023 13:12:02 +0300 Subject: [PATCH] switching to email service --- common/models/email/Email.php | 23 +++++++++++++ common/models/email/RegistrationEmail.php | 20 +++++++++++ common/services/EmailService.php | 33 +++++++++++++++++++ frontend/models/SignupForm.php | 24 +------------- .../api/controllers/RegisterController.php | 12 ++++++- 5 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 common/models/email/Email.php create mode 100644 common/models/email/RegistrationEmail.php create mode 100644 common/services/EmailService.php diff --git a/common/models/email/Email.php b/common/models/email/Email.php new file mode 100644 index 0000000..f6b0e0d --- /dev/null +++ b/common/models/email/Email.php @@ -0,0 +1,23 @@ +sendTo = $user->email; + $this->subject = 'Account registration at ' . Yii::$app->name; + $this->mailLayout = ['html' => 'signup-html', 'text' => 'signup-text']; + $this->params = ['user' => $user]; + } +} \ No newline at end of file diff --git a/common/services/EmailService.php b/common/services/EmailService.php new file mode 100644 index 0000000..f031fce --- /dev/null +++ b/common/services/EmailService.php @@ -0,0 +1,33 @@ +sendFrom = [Yii::$app->params['senderEmail'] => Yii::$app->name . ' robot']; + + } + + /** + * @param Email $email + * @return bool + */ + public function sendEmail(Email $email): bool + { + return Yii::$app->mailer->compose( + $email->mailLayout, + $email->params, + ) + ->setFrom($this->sendFrom) + ->setTo($email->sendTo) + ->setSubject($email->subject) + ->send(); + } +} \ No newline at end of file diff --git a/frontend/models/SignupForm.php b/frontend/models/SignupForm.php index 79c34a9..8fa8cd8 100755 --- a/frontend/models/SignupForm.php +++ b/frontend/models/SignupForm.php @@ -60,28 +60,6 @@ class SignupForm extends Model $authorRole = $auth->getRole('user'); $auth->assign($authorRole, $user->id); - if ($user->save()) { - $this->sendEmail($user); - return $user; - } else { - return null; - } - } - - /** - * Sends an email with a link, for resetting the password. - * - * @return bool whether the email was send - */ - private function sendEmail(User $user) - { - return Yii::$app->mailer->compose( - ['html' => 'signup-html', 'text' => 'signup-text'], - ['user' => $this] - ) - ->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->name . ' robot']) - ->setTo($user->email) - ->setSubject('Account registration at ' . Yii::$app->name) - ->send(); + return $user->save() ? $user : null; } } diff --git a/frontend/modules/api/controllers/RegisterController.php b/frontend/modules/api/controllers/RegisterController.php index 4c759dd..5a9e697 100644 --- a/frontend/modules/api/controllers/RegisterController.php +++ b/frontend/modules/api/controllers/RegisterController.php @@ -2,8 +2,9 @@ namespace frontend\modules\api\controllers; -use common\classes\Debug; +use common\models\email\RegistrationEmail; use common\models\User; +use common\services\EmailService; use frontend\models\SignupForm; use Yii; @@ -16,6 +17,14 @@ class RegisterController extends ApiController return $newBehavior; } + private EmailService $emailService; + + public function __construct($id, $module, EmailService $emailService, $config = []) + { + $this->emailService = $emailService; + parent::__construct($id, $module, $config); + } + /** * * @OA\Post(path="/register/sign-up", @@ -60,6 +69,7 @@ class RegisterController extends ApiController if ($model->load(Yii::$app->getRequest()->getBodyParams(), '')) { /** @var User $user */ if ($user = $model->signup()) { + $this->emailService->sendEmail(new RegistrationEmail($user)); return [ 'id' => $user->id, ];