2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
|
|
|
namespace common\behaviors;
|
|
|
|
|
|
|
|
use yii\base\Behavior;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property \common\models\EmailActivation $owner
|
|
|
|
*/
|
|
|
|
class EmailActivationExpirationBehavior extends Behavior {
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* @var int the number of seconds before the code can be sent again
|
2016-05-11 01:10:06 +05:30
|
|
|
* @see EmailActivation::canRepeat()
|
|
|
|
*/
|
|
|
|
public $repeatTimeout;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* @var int the number of seconds before this activation expires
|
2016-05-11 01:10:06 +05:30
|
|
|
* @see EmailActivation::isExpired()
|
|
|
|
*/
|
|
|
|
public $expirationTimeout;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Is it allowed to resend a message of the current type?
|
|
|
|
* The value of EmailActivation::$repeatTimeout is used for checking as follows:
|
|
|
|
* - <0 will forbid you to resend this activation
|
|
|
|
* - =0 allows you to send messages at any time
|
|
|
|
* - >0 will check how many seconds have passed since the model was created
|
2016-05-11 01:10:06 +05:30
|
|
|
*
|
|
|
|
* @see EmailActivation::compareTime()
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
public function canRepeat(): bool {
|
2016-05-11 01:10:06 +05:30
|
|
|
return $this->compareTime($this->repeatTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Did the code expire?
|
|
|
|
* The value of EmailActivation::$expirationTimeout is used for checking as follows:
|
|
|
|
* - <0 means the code will never expire
|
|
|
|
* - =0 will always say that the code has expired
|
|
|
|
* - >0 will check how many seconds have passed since the model was created
|
2016-05-11 01:10:06 +05:30
|
|
|
*
|
|
|
|
* @see EmailActivation::compareTime()
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
public function isExpired(): bool {
|
2016-05-11 01:10:06 +05:30
|
|
|
return $this->compareTime($this->expirationTimeout);
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public function canRepeatIn(): int {
|
2016-05-11 01:10:06 +05:30
|
|
|
return $this->calculateTime($this->repeatTimeout);
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public function expireIn(): int {
|
2016-05-11 01:10:06 +05:30
|
|
|
return $this->calculateTime($this->expirationTimeout);
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
private function compareTime(int $value): bool {
|
2016-05-11 01:10:06 +05:30
|
|
|
if ($value < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time() > $this->calculateTime($value);
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
private function calculateTime(int $value): int {
|
2016-05-11 01:10:06 +05:30
|
|
|
return $this->owner->created_at + $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|