ActiveRecord   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A afterFind() 0 12 3
A afterSave() 0 15 3
A beforeSave() 0 11 3
A isExpired() 0 18 4
A beforeDelete() 0 13 2
1
<?php
2
3
namespace yrc\redis;
4
5
use yii\redis\ActiveRecord as YiiRedisActiveRecord;
6
use Yii;
7
use yii\helpers\Json;
8
9
abstract class ActiveRecord extends YiiRedisActiveRecord
10
{
11
    public $isExpired = false;
12
13
    /**
14
     * After find reconstitute the keypairs
15
     */
16
    public function afterFind()
17
    {
18
        // If the object is expired, delete it
19
        if ($this->isExpired()) {
20
            $this->isExpired = true;
21
        }
22
        
23
        if ($this->hasAttribute('data')) {
24
            $this->data = Json::decode($this->data);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        }
26
27
        return parent::afterFind();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::afterFind() targeting yii\db\BaseActiveRecord::afterFind() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
    }
29
30
    /**
31
     * Return true if the token is expired
32
     * @return boolean
33
     */
34
    public function isExpired()
35
    {
36
        if ($this->isExpired) {
37
            return true;
38
        }
39
        
40
        if (empty($this->expires_at)) {
0 ignored issues
show
Bug Best Practice introduced by
The property expires_at does not exist on yrc\redis\ActiveRecord. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
            return false;
42
        }
43
        
44
        // Handle token expiration by actually deleting the token
45
        if ($this->expires_at < time()) {
46
            $this->delete();
47
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function afterSave($insert, $changedAttributes)
58
    {
59
        parent::afterSave($insert, $changedAttributes);
60
61
        // If an expiration date is set in the model attributes, tell Redis to automatically handle it
62
        if (!empty($this->expires_at)) {
0 ignored issues
show
Bug Best Practice introduced by
The property expires_at does not exist on yrc\redis\ActiveRecord. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
            $db = static::getDb();
64
            $pk = [];
65
            foreach ($this->primaryKey() as $key) {
66
                $pk[$key] = $this->getAttribute($key);
67
            }
68
69
            $pk = static::buildKey($pk);
70
            $key = static::keyPrefix() . ':a:' . $pk;
71
            $result = $db->executeCommand('EXPIREAT', [$key, $this->expires_at]);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
72
        }
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function beforeSave($insert)
79
    {
80
        if (parent::beforeSave($insert)) {
81
            if ($this->hasAttribute('data')) {
82
                $this->data = Json::encode($this->data);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
            }
84
85
            return true;
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function beforeDelete()
95
    {
96
        if (parent::beforeDelete()) {
97
            // Log that the code was deleted
98
            Yii::debug([
99
                'message' => 'Deleting redis object',
100
                'code_id' => $this->id
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on yrc\redis\ActiveRecord. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
            ], 'yrc/redis/ActiveRecord:delete');
102
103
            return true;
104
        }
105
106
        return false;
107
    }
108
}
109