DataObjectExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 46
c 2
b 0
f 0
dl 0
loc 103
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onAfterDelete() 0 13 1
C onAfterWrite() 0 78 14
1
<?php
2
3
namespace ilateral\SilverStripe\Notifier;
4
5
use ilateral\SilverStripe\Notifier\Model\Notification;
6
use ilateral\SilverStripe\Notifier\Model\NotificationRule;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DataExtension;
9
10
class DataObjectExtension extends DataExtension
11
{
12
    /**
13
     * Check if this object has been changed and
14
     * notify if needed
15
     */
16
    public function onAfterWrite()
17
    {
18
        /** @var DataObject */
19
        $owner = $this->getOwner();
20
        $changed_fields = $owner->getChangedFields(true, DataObject::CHANGE_VALUE);
21
        $new_values = [];
22
23
        foreach ($changed_fields as $key => $value) {
24
            $new_values[$key] = $value['after'];
25
        }
26
27
        $notifications = Notification::get()
28
            ->filter('BaseClassName', $owner->ClassName);
29
30
        // if created, shortcut below logic
31
        if (in_array('ID', array_keys($changed_fields))) {
32
            Notifier::processNotifications(
33
                $notifications->filter(Notification::STATE_CREATED, true),
34
                $owner
35
            );
36
            return;
37
        }
38
39
        // If no fields were changed, then no need to continue
40
        if (count(array_keys($changed_fields)) == 0) {
41
            return;
42
        }
43
44
        // Get a list of all relevent rules
45
        $rules = NotificationRule::get()->filter([
46
            'Notification.BaseClassName' => $owner->ClassName,
47
            'FieldName' => array_keys($changed_fields),
48
            'Value:not' => null
49
        ]);
50
51
        $rule_ids = $rules->filterByCallback(
52
            function($item, $list) use ($new_values) {
0 ignored issues
show
Unused Code introduced by
The parameter $list is not used and could be removed. ( Ignorable by Annotation )

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

52
            function($item, /** @scrutinizer ignore-unused */ $list) use ($new_values) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
                if (in_array($item->FieldName, array_keys($new_values))
54
                    && $new_values[$item->FieldName] == $item->Value) {
55
                        return true;
56
                }
57
            }
58
        )->column('ID');
59
60
        if (count($rule_ids) > 0) {
61
            $notifications = $notifications->filter('Rules.ID', $rule_ids);
62
        }
63
64
        $notifications = $notifications->filterByCallback(
65
            function($item, $list) use ($changed_fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $list is not used and could be removed. ( Ignorable by Annotation )

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

65
            function($item, /** @scrutinizer ignore-unused */ $list) use ($changed_fields) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
                /**
67
                 * @var Notification $item
68
                 * @var DataList $list
69
                 */
70
                if ($item->dbObject(Notification::STATE_UPDATED)->getValue()) {
71
                    return true;
72
                }
73
74
                foreach ($item->Rules() as $rule) {
75
                    /** @var NotificationRule $rule */
76
                    if (in_array($rule->FieldName, array_keys($changed_fields))
77
                        && $rule->WasChanged == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
78
                            return true;
79
                    }
80
81
                    if (in_array($rule->FieldName, array_keys($changed_fields))
82
                        && isset($changed_fields[$rule->FieldName]['after'])
83
                        && $changed_fields[$rule->FieldName]['after'] == $rule->Value
84
                    ) {
85
                            return true;
86
                    }
87
                }
88
89
                return false;
90
            }
91
        );
92
93
        Notifier::processNotifications($notifications, $owner);
94
    }
95
96
    /**
97
     * Check if this object has been deleted and
98
     * notify if needed
99
     */
100
    public function onAfterDelete()
101
    {
102
        /** @var DataObject */
103
        $owner = $this->getOwner();
104
105
        $notifications = Notification::get()->filter(
106
            [
107
                'BaseClassName' => $owner->ClassName,
108
                Notification::STATE_DELETED => true
109
            ]
110
        );
111
112
        Notifier::processNotifications($notifications, $owner);
113
    }
114
}
115