Добавлено событие о изменении email адреса пользователя, вызовы методов для генерации уведомлений внесены внутрь транзакции бд

This commit is contained in:
ErickSkrauch
2016-07-17 18:42:37 +03:00
parent 6d3db89140
commit dd0c4fcc9e
4 changed files with 55 additions and 13 deletions

View File

@@ -2,10 +2,13 @@
namespace api\models\profile\ChangeEmail;
use api\models\base\KeyConfirmationForm;
use common\helpers\Amqp;
use common\models\Account;
use common\models\amqp\EmailChanged;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;
use yii\base\ErrorException;
use yii\base\Exception;
class ConfirmNewEmailForm extends KeyConfirmationForm {
@@ -38,11 +41,14 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
$activation->delete();
$account = $this->getAccount();
$oldEmail = $account->email;
$account->email = $activation->newEmail;
if (!$account->save()) {
throw new ErrorException('Cannot save new account email value');
}
$this->createTask($account->id, $account->email, $oldEmail);
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
@@ -52,4 +58,23 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
return true;
}
/**
* @param integer $accountId
* @param string $newEmail
* @param string $oldEmail
* @throws \PhpAmqpLib\Exception\AMQPExceptionInterface
*/
public function createTask($accountId, $newEmail, $oldEmail) {
$model = new EmailChanged;
$model->accountId = $accountId;
$model->oldEmail = $oldEmail;
$model->newEmail = $newEmail;
$message = Amqp::getInstance()->prepareMessage($model, [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
]);
Amqp::sendToEventsExchange('accounts.email-changed', $message);
}
}