Passed
Push — 5.x ( b03025...f3a773 )
by Jeroen
09:20 queued 15s
created

CreateContentEventHandler::__invoke()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.111

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 9
eloc 17
nc 9
nop 1
dl 0
loc 33
ccs 16
cts 18
cp 0.8889
crap 9.111
rs 8.0555
c 1
b 1
f 0
1
<?php
2
3
namespace Elgg\Notifications;
4
5
/**
6
 * Apply subscriptions based on preferences
7
 *
8
 * @since 4.0
9
 * @internal
10
 */
11
class CreateContentEventHandler {
12
	
13
	/**
14
	 * Subscribe to content you just created in order to receive notifications
15
	 *
16
	 * @param \Elgg\Event $event 'create:after', 'object'|'group'
17
	 *
18
	 * @return void
19
	 */
20 783
	public function __invoke(\Elgg\Event $event): void {
21 783
		$entity = $event->getObject();
22 783
		if (!$entity instanceof \ElggObject && !$entity instanceof \ElggGroup) {
23
			return;
24
		}
25
		
26 783
		$owner = $entity->getOwnerEntity();
27 783
		if (!$owner instanceof \ElggUser) {
28 45
			return;
29
		}
30
		
31 743
		if ($entity instanceof \ElggObject) {
32 572
			$notification_events = _elgg_services()->notifications->getEvents();
33 572
			if (!isset($notification_events[$entity->getType()]) || !isset($notification_events[$entity->getType()][$entity->getSubtype()])) {
34
				// no notification events registered for this entity type/subtype
35
				// so there is no need to subscribe
36
				// this also prevents the database from flooding with relationships that are never used (e.g. subscriptions to site notifications)
37 531
				return;
38
			}
39
		}
40
		
41 289
		$content_preferences = $owner->getNotificationSettings('content_create');
42 289
		$enabled_methods = array_keys(array_filter($content_preferences));
43
		
44
		// loop through all notification types
45 289
		$methods = elgg_get_notification_methods();
46 289
		foreach ($enabled_methods as $method) {
47
			// only enable supported methods
48 289
			if (!in_array($method, $methods)) {
49
				continue;
50
			}
51
			
52 289
			$entity->addSubscription($owner->guid, $method);
53
		}
54
	}
55
}
56