Passed
Push — 6.x ( 1f2552...dee522 )
by Jerome
13:17
created

addOriginalPosterToSubscriptions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 27
ccs 0
cts 14
cp 0
crap 30
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\TheWire\Notifications;
4
5
use Elgg\Notifications\NotificationEventHandler;
6
7
/**
8
 * Notification Event Handler for 'object' 'thewire' 'create' action
9
 */
10
class CreateTheWireEventHandler extends NotificationEventHandler {
11
12
	/**
13
	 * {@inheritdoc}
14
	 */
15
	public function getSubscriptions(): array {
16
		$subscriptions = parent::getSubscriptions();
17
	
18
		return $this->addOriginalPosterToSubscriptions($subscriptions);
19
	}
20
	
21
	/**
22
	 * Add subscription for original poster if not already registered to
23
	 * receive a notification of reply
24
	 *
25
	 * @param array $subscriptions Existing subscriptions
26
	 *
27
	 * @return array
28
	 */
29
	protected function addOriginalPosterToSubscriptions(array $subscriptions): array {
30
		$entity = $this->getEventEntity();
31
		if (!$entity instanceof \ElggWire) {
32
			return $subscriptions;
33
		}
34
35
		$parent = $entity->getParent();
36
		if (empty($parent)) {
37
			return $subscriptions;
38
		}
39
		
40
		// do not add a subscription if reply was to self
41
		if ($parent->owner_guid === $entity->owner_guid) {
42
			return $subscriptions;
43
		}
44
		
45
		if (array_key_exists($parent->owner_guid, $subscriptions)) {
46
			// already in the list
47
			return $subscriptions;
48
		}
49
		
50
		/* @var $parent_owner \ElggUser */
51
		$parent_owner = $parent->getOwnerEntity();
52
		
53
		$subscriptions[$parent_owner->guid] = $parent_owner->getNotificationSettings('default', true);
54
		
55
		return $subscriptions;
56
	}
57
	
58
	/**
59
	 * {@inheritdoc}
60
	 */
61
	protected function getNotificationSubject(\ElggUser $recipient, string $method): string {
62
		return elgg_echo('thewire:notify:subject', [$this->getEventActor()?->getDisplayName()]);
63
	}
64
	
65
	/**
66
	 * {@inheritdoc}
67
	 */
68
	protected function getNotificationSummary(\ElggUser $recipient, string $method): string {
69
		return elgg_echo('thewire:notify:summary', [elgg_get_excerpt((string) $this->getEventEntity()?->description)]);
70
	}
71
	
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	protected function getNotificationBody(\ElggUser $recipient, string $method): string {
76
		$entity = $this->getEventEntity();
77
		if (!$entity instanceof \ElggWire) {
78
			return '';
79
		}
80
		
81
		$owner = $entity->getOwnerEntity();
82
		
83
		$body = '';
84
		if ($entity->reply) {
85
			$parent = $entity->getParent();
86
			if ($parent instanceof \ElggWire) {
87
				$parent_owner = $parent->getOwnerEntity();
88
				$body = elgg_echo('thewire:notify:reply', [$owner?->getDisplayName(), $parent_owner?->getDisplayName()]);
89
			}
90
		}
91
		
92
		if (empty($body)) {
93
			$body = elgg_echo('thewire:notify:post', [$owner?->getDisplayName()]);
94
		}
95
		
96
		$body .= PHP_EOL . PHP_EOL;
97
		$body .= $entity->description;
98
		$body .= PHP_EOL . PHP_EOL;
99
		
100
		$body .= elgg_echo('thewire:notify:footer', [$entity->getURL()]);
101
		
102
		return $body;
103
	}
104
	
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	protected static function isConfigurableForGroup(\ElggGroup $group): bool {
109
		return false;
110
	}
111
}
112