Completed
Pull Request — master (#7)
by Mateusz
02:42
created

AuditHookManyManyList::removeByID()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 31
Code Lines 20

Duplication

Lines 31
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 31
loc 31
rs 5.3846
cc 8
eloc 20
nc 5
nop 1
1
<?php
2
3
namespace SilverStripe\Auditor;
4
5 View Code Duplication
class AuditHookManyManyList extends \ManyManyList
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    /**
8
     * Overload {@link ManyManyList::removeByID()} so we can log
9
     * when a Member is removed from a Group.
10
     */
11
    public function removeByID($itemID)
12
    {
13
        parent::removeByID($itemID);
14
15
        if ($this->getJoinTable() == 'Group_Members') {
16
            $currentMember = \Member::currentUser();
17
            if (!($currentMember && $currentMember->exists())) {
18
                return;
19
            }
20
21
            $member = \Member::get()->byId($itemID);
22
            $group = \Group::get()->byId($this->getForeignID());
23
24
            if (!$group) {
25
                return;
26
            }
27
            if (!$member) {
28
                return;
29
            }
30
31
            $this->getAuditLogger()->info(sprintf(
32
                '"%s" (ID: %s) removed Member "%s" (ID: %s) from Group "%s" (ID: %s)',
33
                $currentMember->Email ?: $currentMember->Title,
34
                $currentMember->ID,
35
                $member->Email ?: $member->Title,
36
                $member->ID,
37
                $group->Title,
38
                $group->ID
39
            ));
40
        }
41
    }
42
43
	protected function getAuditLogger() {
44
		// See note on AuditHook::getAuditLogger
45
		return \Injector::inst()->get('AuditLogger');
46
	}
47
}
48