Completed
Push — master ( 580028...d7e9e8 )
by Charles
02:19
created

ActiveRecord::afterFind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace yrc\redis;
4
5
use yii\redis\ActiveRecord as YiiRedisActiveRecord;
6
use Yii;
7
8
abstract class ActiveRecord extends YiiRedisActiveRecord
9
{
10
    /**
11
     * After find reconstitute the keypairs
12
     */
13
    public function afterFind()
14
    {
15
        // If the object is expired, delete it
16
        if ($this->isExpired()) {
17
            throw new \yii\base\Exception(Yii::t('yrc', 'Element has expired.'));
18
        }
19
        
20
        return parent::afterFind();
21
    }
22
23
    /**
24
     * Return true if the token is expired
25
     * @return boolean
26
     */
27
    public function isExpired()
28
    {
29
        if (empty($this->expires_at)) {
30
            return false;
31
        }
32
        
33
        // Handle token expiration by actually deleting the token
34
        if ($this->expires_at < time()) {
35
            $this->delete();
36
37
            return true;
38
        }
39
40
        return false;
41
    }
42
}