2016-05-16 04:03:19 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models\profile\ChangeEmail;
|
|
|
|
|
2017-04-21 04:11:43 +05:30
|
|
|
use api\emails\EmailHelper;
|
2016-12-23 03:52:51 +05:30
|
|
|
use api\models\base\ApiForm;
|
|
|
|
use api\validators\EmailActivationKeyValidator;
|
2016-05-16 04:03:19 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\confirmations\NewEmailConfirmation;
|
|
|
|
use common\models\EmailActivation;
|
2016-11-02 02:27:42 +05:30
|
|
|
use common\validators\EmailValidator;
|
2016-05-16 04:03:19 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
class NewEmailForm extends ApiForm {
|
|
|
|
|
|
|
|
public $key;
|
2016-05-16 04:03:19 +05:30
|
|
|
|
|
|
|
public $email;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
private $account;
|
|
|
|
|
|
|
|
public function rules() {
|
2016-12-23 03:52:51 +05:30
|
|
|
return [
|
|
|
|
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION],
|
2016-11-02 02:27:42 +05:30
|
|
|
['email', EmailValidator::class],
|
2016-12-23 03:52:51 +05:30
|
|
|
];
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public function getAccount(): Account {
|
2016-11-02 02:27:42 +05:30
|
|
|
return $this->account;
|
2016-05-16 04:03:19 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public function sendNewEmailConfirmation(): 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\CurrentEmailConfirmation $previousActivation */
|
|
|
|
$previousActivation = $this->key;
|
|
|
|
$previousActivation->delete();
|
2016-05-16 04:03:19 +05:30
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
$activation = $this->createCode();
|
2017-04-21 04:11:43 +05:30
|
|
|
|
|
|
|
EmailHelper::changeEmailConfirmNew($activation);
|
2016-12-23 03:52:51 +05:30
|
|
|
|
|
|
|
$transaction->commit();
|
2016-05-16 04:03:19 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return NewEmailConfirmation
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
|
|
|
public function createCode() {
|
|
|
|
$emailActivation = new NewEmailConfirmation();
|
|
|
|
$emailActivation->account_id = $this->getAccount()->id;
|
|
|
|
$emailActivation->newEmail = $this->email;
|
|
|
|
if (!$emailActivation->save()) {
|
|
|
|
throw new ErrorException('Cannot save email activation model');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $emailActivation;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|