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](); } public static function find(): EmailActivationQuery { return new EmailActivationQuery(static::class); } public function behaviors(): array { return [ [ 'class' => TimestampBehavior::class, 'updatedAtAttribute' => false, ], [ 'class' => PrimaryKeyValueBehavior::class, 'value' => function(): string { return UserFriendlyRandomKey::make(); }, ], ]; } public function getAccount(): AccountQuery { /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->hasOne(Account::class, ['id' => 'account_id']); } public function canResend(): bool { $timeout = $this->getResendTimeout(); if ($timeout === null) { return true; } 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; } return $this->compareTime($duration); } /** * 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); } }