2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
2019-12-14 00:57:13 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
namespace common\models;
|
|
|
|
|
2016-08-21 03:52:14 +05:30
|
|
|
use common\behaviors\PrimaryKeyValueBehavior;
|
2016-05-16 04:03:19 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
2019-12-21 03:53:58 +05:30
|
|
|
use DateInterval;
|
|
|
|
use DateTimeImmutable;
|
2016-05-11 01:10:06 +05:30
|
|
|
use yii\base\InvalidConfigException;
|
2016-01-15 14:51:27 +05:30
|
|
|
use yii\behaviors\TimestampBehavior;
|
2016-05-11 01:10:06 +05:30
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
use yii\helpers\ArrayHelper;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Fields:
|
2019-12-21 03:53:58 +05:30
|
|
|
* @property string $key
|
|
|
|
* @property int $account_id
|
|
|
|
* @property int $type
|
|
|
|
* @property array|null $data
|
|
|
|
* @property int $created_at
|
2016-01-15 14:51:27 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Relations:
|
2016-01-15 14:51:27 +05:30
|
|
|
* @property Account $account
|
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Behaviors:
|
2016-01-15 14:51:27 +05:30
|
|
|
* @mixin TimestampBehavior
|
|
|
|
*/
|
2016-05-11 01:10:06 +05:30
|
|
|
class EmailActivation extends ActiveRecord {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public const TYPE_REGISTRATION_EMAIL_CONFIRMATION = 0;
|
|
|
|
public const TYPE_FORGOT_PASSWORD_KEY = 1;
|
|
|
|
public const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
|
|
|
|
public const TYPE_NEW_EMAIL_CONFIRMATION = 3;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public static function tableName(): string {
|
|
|
|
return 'email_activations';
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
public static function getClassMap(): array {
|
|
|
|
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 static function instantiate($row) {
|
|
|
|
$type = ArrayHelper::getValue($row, 'type');
|
|
|
|
if ($type === null) {
|
|
|
|
return parent::instantiate($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMap = self::getClassMap();
|
|
|
|
if (!isset($classMap[$type])) {
|
|
|
|
throw new InvalidConfigException('Unexpected type');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $classMap[$type]();
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public static function find(): EmailActivationQuery {
|
|
|
|
return new EmailActivationQuery(static::class);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function behaviors(): array {
|
2016-01-15 14:51:27 +05:30
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'updatedAtAttribute' => false,
|
|
|
|
],
|
2016-08-21 03:52:14 +05:30
|
|
|
[
|
|
|
|
'class' => PrimaryKeyValueBehavior::class,
|
2019-12-21 03:53:58 +05:30
|
|
|
'value' => function(): string {
|
2016-08-21 03:52:14 +05:30
|
|
|
return UserFriendlyRandomKey::make();
|
|
|
|
},
|
|
|
|
],
|
2016-01-15 14:51:27 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function getAccount(): AccountQuery {
|
2019-12-21 03:53:58 +05:30
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */
|
2016-01-15 14:51:27 +05:30
|
|
|
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
public function canResend(): bool {
|
|
|
|
$timeout = $this->getResendTimeout();
|
|
|
|
if ($timeout === null) {
|
|
|
|
return true;
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
return $this->compareTime($timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canResendAt(): DateTimeImmutable {
|
|
|
|
return $this->calculateTime($this->getResendTimeout() ?? new DateInterval('PT0S'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isStale(): bool {
|
|
|
|
$duration = $this->getExpireDuration();
|
|
|
|
if ($duration === null) {
|
|
|
|
return false;
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
return $this->compareTime($duration);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
/**
|
|
|
|
* After which time the message for this action type can be resended.
|
|
|
|
* When null returned the message can be sent immediately.
|
|
|
|
*
|
|
|
|
* @return DateInterval|null
|
|
|
|
*/
|
|
|
|
protected function getResendTimeout(): ?DateInterval {
|
|
|
|
return new DateInterval('PT5M');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How long the activation code should be valid.
|
|
|
|
* When null returned the code is never expires
|
|
|
|
*
|
|
|
|
* @return DateInterval|null
|
|
|
|
*/
|
|
|
|
protected function getExpireDuration(): ?DateInterval {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function compareTime(DateInterval $value): bool {
|
|
|
|
return (new DateTimeImmutable()) > $this->calculateTime($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateTime(DateInterval $interval): DateTimeImmutable {
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
return (new DateTimeImmutable('@' . $this->created_at))->add($interval);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|