EventNew_Notify_Background::execute()   B
last analyzed

Complexity

Conditions 11
Paths 48

Size

Total Lines 80
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 46
nop 0
dl 0
loc 80
rs 7.3166
c 0
b 0
f 0
nc 48

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This taks handles notifying someone that a new event has been
5
 * added to the calendar - but only when no topic has been created.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2019 Simple Machines and individual contributors
12
 * @license http://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC2
15
 */
16
17
/**
18
 * Class EventNew_Notify_Background
19
 */
20
class EventNew_Notify_Background extends SMF_BackgroundTask
21
{
22
	/**
23
     * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
24
	 * @return bool Always returns true
25
	 */
26
	public function execute()
27
 	{
28
 		global $sourcedir, $smcFunc, $user_profile;
29
30
		// Get everyone who could be notified - those are the people who can see the calendar.
31
		require_once($sourcedir . '/Subs-Members.php');
32
		$members = membersAllowedTo('calendar_view');
33
34
		// Don't alert the event creator
35
		if (!empty($this->_details['sender_id']))
36
			$members = array_diff($members, array($this->_details['sender_id']));
37
38
		// Having successfully figured this out, now let's get the preferences of everyone.
39
		require_once($sourcedir . '/Subs-Notify.php');
40
		$prefs = getNotifyPrefs($members, 'event_new', true);
41
42
		// Just before we go any further, we may not have the sender's name. Let's just quickly fix that.
43
		// If a guest creates the event, we wouldn't be capturing a username or anything.
44
		if (!empty($this->_details['sender_id']) && empty($this->_details['sender_name']))
45
		{
46
			loadMemberData($this->_details['sender_id'], 'minimal');
0 ignored issues
show
Bug introduced by
'minimal' of type string is incompatible with the type boolean expected by parameter $is_name of loadMemberData(). ( Ignorable by Annotation )

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

46
			loadMemberData($this->_details['sender_id'], /** @scrutinizer ignore-type */ 'minimal');
Loading history...
47
			if (!empty($user_profile[$this->_details['sender_id']]))
48
				$this->_details['sender_name'] = $user_profile[$this->_details['sender_id']]['real_name'];
49
			else
50
				$this->_details['sender_id'] = 0;
51
		}
52
53
		// So now we find out who wants what.
54
		$alert_bits = array(
55
			'alert' => self::RECEIVE_NOTIFY_ALERT,
56
			'email' => self::RECEIVE_NOTIFY_EMAIL,
57
		);
58
		$notifies = array();
59
60
		foreach ($prefs as $member => $pref_option)
61
		{
62
			foreach ($alert_bits as $type => $bitvalue)
63
				if ($pref_option['event_new'] & $bitvalue)
64
					$notifies[$type][] = $member;
65
		}
66
67
		// Firstly, anyone who wants alerts.
68
		if (!empty($notifies['alert']))
69
		{
70
			// Alerts are relatively easy.
71
			$insert_rows = array();
72
			foreach ($notifies['alert'] as $member)
73
			{
74
				$insert_rows[] = array(
75
					'alert_time' => $this->_details['time'],
76
					'id_member' => $member,
77
					'id_member_started' => $this->_details['sender_id'],
78
					'member_name' => $this->_details['sender_name'],
79
					'content_type' => 'event',
80
					'content_id' => $this->_details['event_id'],
81
					'content_action' => empty($this->_details['sender_id']) ? 'new_guest' : 'new',
82
					'is_read' => 0,
83
					'extra' => $smcFunc['json_encode'](
84
					    array(
85
					        "event_id" => $this->_details['event_id'],
86
							"event_title" => $this->_details['event_title']
87
						)
88
					),
89
				);
90
			}
91
92
			$smcFunc['db_insert']('insert',
93
				'{db_prefix}user_alerts',
94
				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
95
					'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
96
					'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
97
				$insert_rows,
98
				array('id_alert')
99
			);
100
101
			// And update the count of alerts for those people.
102
			updateMemberData($notifies['alert'], array('alerts' => '+'));
103
		}
104
105
		return true;
106
	}
107
}
108
109
?>