AuditHookManyManyList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 21
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuditLogger() 0 4 1
B removeByID() 0 28 8
1
<?php
2
3
namespace SilverStripe\Auditor;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\ORM\ManyManyList;
7
use SilverStripe\Security\Group;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
11
class AuditHookManyManyList extends ManyManyList
12
{
13
    /**
14
     * Overload {@link ManyManyList::removeByID()} so we can log
15
     * when a Member is removed from a Group.
16
     */
17
    public function removeByID($itemID)
18
    {
19
        parent::removeByID($itemID);
20
21
        if ($this->getJoinTable() == 'Group_Members') {
22
            $currentMember = Security::getCurrentUser();
23
            if (!($currentMember && $currentMember->exists())) {
24
                return;
25
            }
26
27
            $member = Member::get()->byId($itemID);
28
            $group = Group::get()->byId($this->getForeignID());
0 ignored issues
show
Bug introduced by
$this->getForeignID() of type string is incompatible with the type integer expected by parameter $id of SilverStripe\ORM\DataList::byID(). ( Ignorable by Annotation )

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

28
            $group = Group::get()->byId(/** @scrutinizer ignore-type */ $this->getForeignID());
Loading history...
29
30
            if (!$group) {
0 ignored issues
show
introduced by
$group is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
31
                return;
32
            }
33
            if (!$member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
34
                return;
35
            }
36
37
            $this->getAuditLogger()->info(sprintf(
38
                '"%s" (ID: %s) removed Member "%s" (ID: %s) from Group "%s" (ID: %s)',
39
                $currentMember->Email ?: $currentMember->Title,
40
                $currentMember->ID,
41
                $member->Email ?: $member->Title,
42
                $member->ID,
43
                $group->Title,
44
                $group->ID
45
            ));
46
        }
47
    }
48
49
    protected function getAuditLogger()
50
    {
51
        // See note on AuditHook::getAuditLogger
52
        return Injector::inst()->get('AuditLogger');
53
    }
54
}
55