UpdateNoteExtension::onAfterWrite()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 6
eloc 28
nc 7
nop 0
1
<?php
2
3
4
5
class UpdateNoteExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    private static $has_many = array(
0 ignored issues
show
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...
8
        'UpdateNotes' => 'UpdateNote'
9
    );
10
11
    private static $seconds_grace = 600;
0 ignored issues
show
Unused Code introduced by
The property $seconds_grace 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...
12
13
    private static $latest_limit = 600;
0 ignored issues
show
Unused Code introduced by
The property $latest_limit 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...
14
15
    private static $_run_once_only = array();
16
17
    /**
18
     * Event handler called after writing to the database.
19
     */
20
    public function onAfterWrite()
21
    {
22
        parent::onAfterWrite();
23
        $id = $this->owner->ID;
24
        $class = $this->owner->ClassName;
25
        $userID = Member::currentUserID();
26
        $filter = array(
27
            'UpdateNoteRecordID' => $id,
28
            'UpdateNoteRecordClass' => $class,
29
            'UpdatedByID' => $userID
30
        );
31
        $key = implode('_', $filter);
32
        if (! isset(self::$_run_once_only[$key])) {
33
            self::$_run_once_only[$key] = true;
34
            $now = SS_Datetime::now()->Rfc2822();
35
            $gracePointInTime = strtotime($now) - Config::inst()->get('UpdateNoteExtension', 'seconds_grace');
36
            $log = DataObject::get_one(
37
                'UpdateNote',
38
                $filter,
39
                $cacheDataObjectGetOne = false,
40
                array('Created' => 'DESC')
0 ignored issues
show
Documentation introduced by
array('Created' => 'DESC') is of type array<string,string,{"Created":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            );
42
            if ($log) {
43
                $editPointInTime = strtotime($log->Created);
44
                debug::log(Date('l jS \of F Y h:i:s A', $editPointInTime).'...'.Date('l jS \of F Y h:i:s A', $gracePointInTime));
45
                if ($editPointInTime < $gracePointInTime) {
46
                    $log = null;
47
                }
48
            }
49
            if ($log && $log->exists()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
50
                //already exists ...
51
            } else {
52
                $log = UpdateNote::create($filter);
53
            }
54
            $log->write();
55
        }
56
    }
57
58
    /**
59
     * Update Fields
60
     * @return FieldList
61
     */
62
    public function updateCMSFields(FieldList $fields)
63
    {
64
        $owner = $this->owner;
0 ignored issues
show
Unused Code introduced by
$owner is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
        $name = Injector::inst()->get('UpdateNote')->i18n_plural_name();
66
        $fields->addFieldToTab(
67
            'Root.'.$name,
68
            GridField::create(
69
                'UpdateNotes',
70
                $name,
71
                $this->owner->UpdateNotes(),
72
                GridFieldConfig_RecordEditor::create()
73
            )
74
        );
75
76
        return $fields;
77
    }
78
79
    /**
80
     * Event handler called before deleting from the database.
81
     */
82
    public function onBeforeDelete()
83
    {
84
        foreach ($this->owner->UpdateNotes() as $note) {
85
            $note->delete();
86
        }
87
    }
88
89
90
    public function LatestUpdateNotes()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
91
    {
92
        return $this->owner->UpdateNotes()
93
            ->limit(Config::inst()->get('xx', 'latest_limit'));
94
    }
95
}
96