mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Rework email_activation model, get rid of behaviors, use json column to store additional data
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
namespace common\behaviors;
|
||||
|
||||
use yii\base\Behavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class DataBehavior extends Behavior {
|
||||
|
||||
/**
|
||||
* @var string attribute name to which this behavior will be applied
|
||||
*/
|
||||
public $attribute = '_data';
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
protected function setKey(string $key, $value) {
|
||||
$data = $this->getData();
|
||||
$data[$key] = $value;
|
||||
$this->owner->{$this->attribute} = serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getKey(string $key) {
|
||||
return ArrayHelper::getValue($this->getData(), $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \yii\base\ErrorException Yii2 will catch Notice from the wrong deserialization and turn it
|
||||
* into its own Exception, so that the program can continue to work normally (you still should catch an Exception)
|
||||
*/
|
||||
private function getData() {
|
||||
$data = $this->owner->{$this->attribute};
|
||||
if (is_string($data)) {
|
||||
$data = unserialize($data);
|
||||
} else {
|
||||
$data = [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
namespace common\behaviors;
|
||||
|
||||
use yii\base\Behavior;
|
||||
|
||||
/**
|
||||
* @property \common\models\EmailActivation $owner
|
||||
*/
|
||||
class EmailActivationExpirationBehavior extends Behavior {
|
||||
|
||||
/**
|
||||
* @var int the number of seconds before the code can be sent again
|
||||
* @see EmailActivation::canRepeat()
|
||||
*/
|
||||
public $repeatTimeout;
|
||||
|
||||
/**
|
||||
* @var int the number of seconds before this activation expires
|
||||
* @see EmailActivation::isExpired()
|
||||
*/
|
||||
public $expirationTimeout;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @see EmailActivation::compareTime()
|
||||
* @return bool
|
||||
*/
|
||||
public function canRepeat(): bool {
|
||||
return $this->compareTime($this->repeatTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @see EmailActivation::compareTime()
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired(): bool {
|
||||
return $this->compareTime($this->expirationTimeout);
|
||||
}
|
||||
|
||||
public function canRepeatIn(): int {
|
||||
return $this->calculateTime($this->repeatTimeout);
|
||||
}
|
||||
|
||||
public function expireIn(): int {
|
||||
return $this->calculateTime($this->expirationTimeout);
|
||||
}
|
||||
|
||||
private function compareTime(int $value): bool {
|
||||
if ($value < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($value === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return time() > $this->calculateTime($value);
|
||||
}
|
||||
|
||||
private function calculateTime(int $value): int {
|
||||
return $this->owner->created_at + $value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user