accounts/common/models/EmailActivation.php
ErickSkrauch 50439fdaeb Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами
У EmailActivation добавлено поле $_data и дописано поведение для работы с ним
Упрощено подключение фикстур для EmailActivations
2016-05-16 01:33:19 +03:00

103 lines
2.8 KiB
PHP

<?php
namespace common\models;
use common\behaviors\DataBehavior;
use common\behaviors\EmailActivationExpirationBehavior;
use common\components\UserFriendlyRandomKey;
use Yii;
use yii\base\InvalidConfigException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
/**
* Поля модели:
* @property string $key
* @property integer $account_id
* @property integer $type
* @property string $_data
* @property integer $created_at
*
* Отношения:
* @property Account $account
*
* Поведения:
* @mixin TimestampBehavior
* @mixin EmailActivationExpirationBehavior
* @mixin DataBehavior
*/
class EmailActivation extends ActiveRecord {
const TYPE_REGISTRATION_EMAIL_CONFIRMATION = 0;
const TYPE_FORGOT_PASSWORD_KEY = 1;
const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
const TYPE_NEW_EMAIL_CONFIRMATION = 3;
public static function tableName() {
return '{{%email_activations}}';
}
public function behaviors() {
return [
[
'class' => TimestampBehavior::class,
'updatedAtAttribute' => false,
],
'expirationBehavior' => [
'class' => EmailActivationExpirationBehavior::class,
'repeatTimeout' => 5 * 60,
'expirationTimeout' => -1,
],
'dataBehavior' => [
'class' => DataBehavior::class,
'attribute' => '_data',
],
];
}
public function getAccount() {
return $this->hasOne(Account::class, ['id' => 'account_id']);
}
/**
* @inheritdoc
*/
public static function instantiate($row) {
$type = ArrayHelper::getValue($row, 'type');
if ($type === null) {
return new static;
}
$classMap = self::getClassMap();
if (!isset($classMap[$type])) {
throw new InvalidConfigException('Unexpected type');
}
return new $classMap[$type];
}
public static function getClassMap() {
return [
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
];
}
public function beforeSave($insert) {
if (!parent::beforeSave($insert)) {
return false;
}
if ($this->key === null) {
do {
$this->key = UserFriendlyRandomKey::make();
} while (EmailActivation::find()->andWhere(['key' => $this->key])->exists());
}
return true;
}
}