Audit::getPreviousFieldValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\DataObjectAuditor\Models;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Security\Member;
7
use SilverStripe\Security\Security;
8
9
/**
10
 * Class Audit
11
 * Tracks all mutation operations on dataobjects (i.e. deletes or updates)
12
 */
13
class Audit extends DataObject
14
{
15
    const DB_MODEL_NAME = 'ModelName';
16
    const DB_MODEL_ID = 'ModelID';
17
    const DB_MUTATION_PEFORMED = 'MutationPerformed';
18
    const HAS_MANY_AUDIT_VALUES = 'AuditValues';
19
    const HAS_ONE_DONE_BY = 'DoneBy';
20
    const DB_DONE_BY_ID = 'DoneByID';
21
22
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
        self::DB_MODEL_NAME => 'Varchar(150)',
24
        self::DB_MODEL_ID => 'Int',
25
        self::DB_MUTATION_PEFORMED => 'Varchar(50)'
26
    ];
27
28
    private static $has_one = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
        self::HAS_ONE_DONE_BY => Member::class
30
    ];
31
32
    private static $indexes = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $indexes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
33
        'inxModelAndID' => [
34
            'type' => 'index',
35
            'columns' => [self::DB_MODEL_NAME, self::DB_MODEL_ID]
36
        ],
37
        self::DB_MUTATION_PEFORMED => true
38
    ];
39
40
    private static $has_many = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
        self::HAS_MANY_AUDIT_VALUES => AuditValue::class
42
    ];
43
44
    private static $table_name = 'DataObjectAuditor_Audit';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
    public function onBeforeWrite()
47
    {
48
        parent::onBeforeWrite();
49
        if (!$this->isInDB()) {
50
            $memberID = ($member = Security::getCurrentUser()) ? $member->ID : 0;
51
            $this->setField(self::DB_DONE_BY_ID, $memberID);
52
        }
53
    }
54
55
    /**
56
     * Returns the previous value for a specific field.
57
     * @param string $field The field name to query
58
     * @return null|DataObject
59
     */
60
    public function getPreviousFieldValue($field)
61
    {
62
        if ($item = AuditValue::get()
63
            ->filter(
64
                [
65
                    AuditValue::DB_FIELD => $field,
66
                    AuditValue::HAS_ONE_AUDIT_ID => $this->getField('ID')
67
                ]
68
            )
69
            ->last()) {
70
            return $item->getField(AuditValue::DB_PREVIOUS_VALUE);
71
        }
72
73
        return null;
74
    }
75
}
76