Completed
Push — master ( 210e4d...401f4c )
by Henry
02:14
created

Versioning::beforeVersionedSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Divergence\Models;
3
4
use Exception;
5
6
use Divergence\Helpers\Util;
7
use Divergence\IO\Database\MySQL as DB;
8
9
trait Versioning
10
{
11
    public $wasDirty = false;
12
13
    public static $versioningFields = [
14
        'RevisionID' => [
15
            'columnName' => 'RevisionID',
16
            'type' => 'integer',
17
            'unsigned' => true,
18
            'notnull' => false,
19
        ],
20
    ];
21
    
22
    public static $versioningRelationships = [
23
        'History' => [
24
            'type' => 'history',
25
            'order' => ['RevisionID' => 'DESC'],
26
        ],
27
    ];
28
    
29
    
30
    public static function getHistoryTable()
31
    {
32
        if (!static::$historyTable) {
33
            throw new Exception('Static variable $historyTable must be defined to use model versioning.');
34
        }
35
        
36
        return static::$historyTable;
37
    }
38
    
39
    /*
40
     * Implement specialized getters
41
     */
42
    public static function getRevisionsByID($ID, $options = [])
43
    {
44
        $options['conditions'][static::getPrimaryKey()] = $ID;
45
        
46
        return static::getRevisions($options);
47
    }
48
49
    public static function getRevisions($options = [])
50
    {
51
        return static::instantiateRecords(static::getRevisionRecords($options));
52
    }
53
    
54
    public static function getRevisionRecords($options = [])
55
    {
56
        $options = Util::prepareOptions($options, [
57
            'indexField' => false,
58
            'conditions' => [],
59
            'order' => false,
60
            'limit' => false,
61
            'offset' => 0,
62
        ]);
63
                
64
        $query = 'SELECT * FROM `%s` WHERE (%s)';
65
        $params = [
66
            static::getHistoryTable(),
67
            count($options['conditions']) ? join(') AND (', static::_mapConditions($options['conditions'])) : 1,
68
        ];
69
        
70
        if ($options['order']) {
71
            $query .= ' ORDER BY ' . join(',', static::_mapFieldOrder($options['order']));
72
        }
73
        
74
        if ($options['limit']) {
75
            $query .= sprintf(' LIMIT %u,%u', $options['offset'], $options['limit']);
76
        }
77
        
78
        
79
        if ($options['indexField']) {
80
            return DB::table(static::_cn($options['indexField']), $query, $params);
81
        } else {
82
            return DB::allRecords($query, $params);
83
        }
84
    }
85
86
    public function beforeVersionedSave()
87
    {
88
        $this->wasDirty = false;
89
        if ($this->isDirty && static::$createRevisionOnSave) {
90
            // update creation time
91
            $this->Created = time();
1 ignored issue
show
Bug Best Practice introduced by
The property Created does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
            $this->wasDirty = true;
93
        }
94
    }
95
96
    public function afterVersionedSave()
97
    {
98
        if ($this->wasDirty && static::$createRevisionOnSave) {
99
            // save a copy to history table
100
            $recordValues = $this->_prepareRecordValues();
1 ignored issue
show
Bug introduced by
It seems like _prepareRecordValues() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            /** @scrutinizer ignore-call */ 
101
            $recordValues = $this->_prepareRecordValues();
Loading history...
101
            $set = static::_mapValuesToSet($recordValues);
102
            DB::nonQuery(
103
                'INSERT INTO `%s` SET %s',
104
                [
105
                    static::getHistoryTable(),
106
                    join(',', $set),
107
                ]
108
            );
109
        }
110
    }
111
}
112