2016-05-16 04:03:19 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models\profile\ChangeEmail;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
use api\models\base\ApiForm;
|
|
|
|
use api\validators\EmailActivationKeyValidator;
|
2016-07-17 21:12:37 +05:30
|
|
|
use common\helpers\Amqp;
|
2016-05-16 04:03:19 +05:30
|
|
|
use common\models\Account;
|
2016-07-17 21:12:37 +05:30
|
|
|
use common\models\amqp\EmailChanged;
|
2016-12-23 03:52:51 +05:30
|
|
|
use common\models\EmailActivation;
|
2016-07-17 21:12:37 +05:30
|
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
2016-05-16 04:03:19 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
class ConfirmNewEmailForm extends ApiForm {
|
|
|
|
|
|
|
|
public $key;
|
2016-05-16 04:03:19 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
private $account;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public function rules() {
|
|
|
|
return [
|
|
|
|
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION],
|
|
|
|
];
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account
|
|
|
|
*/
|
2016-12-23 03:52:51 +05:30
|
|
|
public function getAccount(): Account {
|
2016-05-16 04:03:19 +05:30
|
|
|
return $this->account;
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public function changeEmail(): bool {
|
2016-05-16 04:03:19 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
2016-12-23 03:52:51 +05:30
|
|
|
|
|
|
|
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
|
|
|
$activation = $this->key;
|
|
|
|
$activation->delete();
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$oldEmail = $account->email;
|
|
|
|
$account->email = $activation->newEmail;
|
|
|
|
if (!$account->save()) {
|
|
|
|
throw new ErrorException('Cannot save new account email value');
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
$this->createTask($account->id, $account->email, $oldEmail);
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
2016-05-16 04:03:19 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:12:37 +05:30
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
$this->account = $account;
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|