Enhancements to Mailer exception handling fixes #79

This commit is contained in:
Kartik Visweswaran
2017-10-18 18:15:15 +05:30
parent 18ea0c664a
commit 3feae01a17
35 changed files with 302 additions and 38 deletions

View File

@ -20,6 +20,7 @@ class MailService implements ServiceInterface
{
protected $viewPath = '@Da/User/resources/views/mail';
protected $type;
protected $from;
protected $to;
protected $subject;
@ -30,15 +31,17 @@ class MailService implements ServiceInterface
/**
* MailService constructor.
*
* @param string $from
* @param string $to
* @param string $subject
* @param string $view
* @param array $params
* @param BaseMailer|MailerInterface $mailer
* @param string $type the mailer type
* @param string $from from email account
* @param string $to to email account
* @param string $subject the email subject
* @param string $view the view to render mail
* @param array $params view parameters
* @param BaseMailer|MailerInterface $mailer mailer interface
*/
public function __construct($from, $to, $subject, $view, array $params, MailerInterface $mailer)
public function __construct($type, $from, $to, $subject, $view, array $params, MailerInterface $mailer)
{
$this->type = $type;
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
@ -62,6 +65,15 @@ class MailService implements ServiceInterface
return $this;
}
/**
* gets mailer type
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return bool
*/

View File

@ -16,12 +16,15 @@ use Da\User\Factory\TokenFactory;
use Da\User\Model\Token;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Da\User\Traits\MailAwareTrait;
use Exception;
use Yii;
use yii\log\Logger;
class PasswordRecoveryService implements ServiceInterface
{
use MailAwareTrait;
protected $query;
protected $email;
@ -51,7 +54,7 @@ class PasswordRecoveryService implements ServiceInterface
$this->mailService->setViewParam('user', $user);
$this->mailService->setViewParam('token', $token);
if (!$this->mailService->run()) {
if (!$this->sendMail($user)) {
return false;
}

View File

@ -14,10 +14,13 @@ namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory;
use Da\User\Model\User;
use Da\User\Traits\MailAwareTrait;
use yii\log\Logger;
class ResendConfirmationService implements ServiceInterface
{
use MailAwareTrait;
protected $model;
protected $mailService;
protected $logger;
@ -34,8 +37,7 @@ class ResendConfirmationService implements ServiceInterface
if ($this->model && !$this->model->getIsConfirmed()) {
$token = TokenFactory::makeConfirmationToken($this->model->id);
$this->mailService->setViewParam('token', $token);
return $this->mailService->run();
return $this->sendMail($this->model);
}
return false;

View File

@ -15,12 +15,16 @@ use Da\User\Contracts\ServiceInterface;
use Da\User\Event\UserEvent;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\User;
use Da\User\Traits\MailAwareTrait;
use Exception;
use Yii;
use yii\base\InvalidCallException;
use yii\log\Logger;
class UserCreateService implements ServiceInterface
{
use MailAwareTrait;
protected $model;
protected $securityHelper;
protected $mailService;
@ -57,15 +61,23 @@ class UserCreateService implements ServiceInterface
if (!$model->save()) {
$transaction->rollBack();
return false;
}
$model->trigger(UserEvent::EVENT_AFTER_CREATE);
$this->mailService->run();
if (!$this->sendMail($model)) {
Yii::$app->session->setFlash(
'warning',
Yii::t(
'usuario',
'Error sending welcome message to "{email}". Please try again later.',
['email' => $model->email]
)
);
$transaction->rollBack();
return false;
}
$transaction->commit();
return true;
} catch (Exception $e) {
$transaction->rollBack();

View File

@ -16,16 +16,17 @@ use Da\User\Event\UserEvent;
use Da\User\Factory\TokenFactory;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\User;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Traits\MailAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Exception;
use yii\base\InvalidCallException;
use yii\log\Logger;
use Yii;
class UserRegisterService implements ServiceInterface
{
use ModuleAwareTrait;
use ContainerAwareTrait;
use MailAwareTrait;
protected $model;
protected $securityHelper;
@ -61,7 +62,6 @@ class UserRegisterService implements ServiceInterface
if (!$model->save()) {
$transaction->rollBack();
return false;
}
@ -72,8 +72,18 @@ class UserRegisterService implements ServiceInterface
if (isset($token)) {
$this->mailService->setViewParam('token', $token);
}
$this->mailService->run();
if (!$this->sendMail($model)) {
Yii::$app->session->setFlash(
'warning',
Yii::t(
'usuario',
'Error sending registration message to "{email}". Please try again later.',
['email' => $model->email]
)
);
$transaction->rollBack();
return false;
}
$model->trigger(UserEvent::EVENT_AFTER_REGISTER, $event);
$transaction->commit();