Passed
Branch 3.3 (508034)
by Jeroen
22:33 queued 25s
created

Delete::deleteLikes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
1
<?php
2
3
namespace Elgg\Likes;
4
5
use Elgg\Database\Delete as DbDelete;
6
7
/**
8
 * Listen to 'delete' events to cleanup likes
9
 *
10
 * @internal
11
 * @since 3.3
12
 */
13
class Delete {
14
	
15
	/**
16
	 * Cleanup likes annotations on delete of an entity
17
	 *
18
	 * @param \Elgg\Event $event 'delete', 'group'|'object'|'site'|'user'
19
	 *
20
	 *  @return void
21
	 */
22 384
	public static function deleteLikes(\Elgg\Event $event) {
23
		
24 384
		$entity = $event->getObject();
25 384
		if (!$entity instanceof \ElggEntity) {
26
			return;
27
		}
28
		
29
		// Let's do a bulk delete of all likes annotations, instead of for each individually
30
		// this will save performance
31 384
		$delete = DbDelete::fromTable('annotations');
32 384
		$delete->where($delete->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_GUID))
33 384
			->andWhere($delete->compare('name', '=', 'likes', ELGG_VALUE_STRING));
34
		
35 384
		elgg()->db->deleteData($delete);
36 384
	}
37
}
38