ActiveRecord   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDb() 0 4 1
A beforeSave() 0 12 6
A afterSave() 0 15 5
A afterFind() 0 9 4
1
<?php
2
/**
3
 * Base model for the audit classes containing some helper functions to auto serialize/unserialize the
4
 * raw data attributes.
5
 */
6
7
namespace bedezign\yii2\audit\components\db;
8
9
use bedezign\yii2\audit\Audit;
10
use bedezign\yii2\audit\components\Helper;
11
use Yii;
12
13
/**
14
 * ActiveRecord
15
 * @package bedezign\yii2\audit\models
16
 * @property string $created
17
 */
18
class ActiveRecord extends \yii\db\ActiveRecord
19
{
20
    /** @var bool                   If true, automatically pack and unpack the data attribute */
21
    protected $autoSerialize = true;
22
    /**
23
     * @var array
24
     */
25
    protected $serializeAttributes = ['data'];
26
27
    /**
28
     * @return \yii\db\Connection
29 237
     */
30
    public static function getDb()
31 237
    {
32
        return Audit::getInstance()->getDb();
33
    }
34
35
    /**
36
     * @param bool $insert
37
     * @return bool
38 117
     */
39
    public function beforeSave($insert)
40 117
    {
41 117
        if ($insert && $this->hasAttribute('created'))
42
            $this->created = date('Y-m-d H:i:s');
43 117
44 117
        if ($this->autoSerialize)
45 36
            foreach ($this->serializeAttributes as $attribute)
46 36
                if ($this->hasAttribute($attribute))
47
                    $this->$attribute = [Helper::serialize($this->$attribute, false), \PDO::PARAM_LOB];
48 117
49
        return parent::beforeSave($insert);
50
    }
51
52
    /**
53
     * @param bool  $insert
54
     * @param array $changedAttributes
55 117
     */
56
    public function afterSave($insert, $changedAttributes)
57 117
    {
58
        parent::afterSave($insert, $changedAttributes);
59 117
60 117
        if ($this->autoSerialize)
61 36
            foreach ($this->serializeAttributes as $attribute)
62 36
                if ($this->hasAttribute($attribute)) {
63
                    if (is_array($this->$attribute)) {
64 36
                        // Get rid of the extra PDO parameter type if needed
65 36
                        $array = $this->$attribute;
66 36
                        $this->$attribute = $array[0];
67 36
                    }
68 36
                    $this->$attribute = Helper::unserialize($this->$attribute);
69 117
                }
70
    }
71
72
    /**
73
     *
74 156
     */
75
    public function afterFind()
76 156
    {
77
        parent::afterFind();
78 156
79 156
        if ($this->autoSerialize)
80 153
            foreach ($this->serializeAttributes as $attribute)
81 153
                if ($this->hasAttribute($attribute))
82 156
                    $this->$attribute = Helper::unserialize($this->$attribute);
83
    }
84
}