Version   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.86%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A versions() 0 17 3
A lastVersion() 0 5 1
A find() 0 16 4
A findAttribute() 0 10 2
1
<?php
2
namespace bedezign\yii2\audit\components;
3
4
use bedezign\yii2\audit\models\AuditTrail;
5
use yii\base\Component;
6
use yii\db\ActiveRecord;
7
8
/**
9
 * Version
10
 * @package bedezign\yii2\audit
11
 */
12
class Version extends Component
13
{
14
15
    /**
16
     * Get all versions of the model.
17
     *
18
     * @param $class
19
     * @param $id
20
     * @return array
21
     */
22 6
    public static function versions($class, $id)
23
    {
24
        /** @var AuditTrail[] $trails */
25 6
        $trails = AuditTrail::find()
26 6
            ->andWhere(['model' => $class, 'model_id' => $id])
27 6
            ->orderBy(['entry_id' => SORT_ASC, 'id' => SORT_ASC])
28 6
            ->all();
29 6
        $versions = [];
30 6
        foreach ($trails as $trail) {
31 6
            if ($trail->action == 'DELETE') {
32
                $versions[$trail->entry_id][$trail->action] = $trail->action;
33
            } else {
34 6
                $versions[$trail->entry_id][$trail->field] = $trail->new_value;
35
            }
36 6
        }
37 6
        return $versions;
38
    }
39
40
    /**
41
     * Get the last version of the model.
42
     *
43
     * @param $class
44
     * @param $id
45
     * @return mixed
46
     */
47 3
    public static function lastVersion($class, $id)
48
    {
49 3
        $versions = self::versions($class, $id);
50 3
        return key(array_slice($versions, -2, 1, true));
51
    }
52
53
    /**
54
     * Find a model from a version.
55
     *
56
     * @param $class
57
     * @param $id
58
     * @param $version
59
     * @return ActiveRecord
60
     */
61 3
    public static function find($class, $id, $version = null)
62
    {
63 3
        if (!$version) {
64
            $version = self::lastVersion($class, $id);
65
        }
66
        /** @var ActiveRecord $model */
67 3
        $model = call_user_func_array([$class, 'findOne'], [$id]);
68 3
        if (!$model) {
69
            // if it has been deleted, load a new one
70
            $model = new $class;
71
        }
72 3
        foreach ($model->attributes() as $attribute) {
73 3
            $model->setAttribute($attribute, self::findAttribute($class, $id, $attribute, $version));
74 3
        }
75 3
        return $model;
76
    }
77
78
    /**
79
     * Find an attribute from a version.
80
     *
81
     * @param $class
82
     * @param $id
83
     * @param $attribute
84
     * @param $version
85
     * @return null|integer|float|string
86
     */
87 3
    public static function findAttribute($class, $id, $attribute, $version)
88
    {
89
        /** @var AuditTrail $trail */
90 3
        $trail = AuditTrail::find()
91 3
            ->andWhere(['model' => $class, 'model_id' => $id, 'field' => $attribute])
92 3
            ->andWhere(['<=', 'entry_id', $version])
93 3
            ->orderBy(['id' => SORT_DESC])
94 3
            ->one();
95 3
        return $trail ? $trail->new_value : null;
96
    }
97
98
}