2016-05-16 04:03:19 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models\profile\ChangeEmail;
|
|
|
|
|
|
|
|
use api\models\base\KeyConfirmationForm;
|
|
|
|
use common\models\Account;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
use yii\base\Exception;
|
|
|
|
|
|
|
|
class ConfirmNewEmailForm extends KeyConfirmationForm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
private $account;
|
|
|
|
|
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
$this->account = $account;
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account
|
|
|
|
*/
|
2016-06-17 02:02:23 +05:30
|
|
|
public function getAccount() : Account {
|
2016-05-16 04:03:19 +05:30
|
|
|
return $this->account;
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
public function changeEmail() : bool {
|
2016-05-16 04:03:19 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
try {
|
|
|
|
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
|
|
|
$activation = $this->getActivationCodeModel();
|
|
|
|
$activation->delete();
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$account->email = $activation->newEmail;
|
|
|
|
if (!$account->save()) {
|
|
|
|
throw new ErrorException('Cannot save new account email value');
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$transaction->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|