ActiveRecord   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDb() 0 4 1
A beforeSave() 0 9 4
A afterSave() 0 9 4
A afterFind() 0 9 4
1
<?php
2
3
namespace cornernote\dashboard;
4
use yii\db\Connection;
5
6
/**
7
 * ActiveRecord
8
 * @package cornernote\dashboard
9
 */
10
class ActiveRecord extends \yii\db\ActiveRecord
11
{
12
    /**
13
     * @var bool If true, automatically encode and decode the data attribute
14
     */
15
    protected $autoEncode = true;
16
17
    /**
18
     * @var array
19
     */
20
    protected $encodeAttributes = ['options'];
21
22
    /**
23
     * @return Connection
24
     */
25
    public static function getDb()
26
    {
27
        return Module::getInstance()->getDb();
28
    }
29
30
    /**
31
     * @param bool $insert
32
     * @return bool
33
     */
34
    public function beforeSave($insert)
35
    {
36
        if ($this->autoEncode)
37
            foreach ($this->encodeAttributes as $attribute)
38
                if ($this->hasAttribute($attribute))
39
                    $this->$attribute = json_encode($this->$attribute);
40
41
        return parent::beforeSave($insert);
42
    }
43
44
    /**
45
     * @param bool $insert
46
     * @param array $changedAttributes
47
     */
48
    public function afterSave($insert, $changedAttributes)
49
    {
50
        parent::afterSave($insert, $changedAttributes);
51
52
        if ($this->autoEncode)
53
            foreach ($this->encodeAttributes as $attribute)
54
                if ($this->hasAttribute($attribute))
55
                    $this->$attribute = json_decode($this->$attribute, true);
56
    }
57
58
    /**
59
     *
60
     */
61
    public function afterFind()
62
    {
63
        parent::afterFind();
64
65
        if ($this->autoEncode)
66
            foreach ($this->encodeAttributes as $attribute)
67
                if ($this->hasAttribute($attribute))
68
                    $this->$attribute = json_decode($this->$attribute, true);
69
    }
70
}