Completed
Pull Request — release-2.1 (#6024)
by John
15:33
created

ApprovePost_Notify_Background::execute()   C

Complexity

Conditions 10
Paths 38

Size

Total Lines 118
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 76
c 0
b 0
f 0
nc 38
nop 0
dl 0
loc 118
rs 6.6569

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
 * This file contains background notification code for moderators
4
 * to approve posts.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
/**
17
 * Class ApprovePost_Notify_Background
18
 */
19
class ApprovePost_Notify_Background extends SMF_BackgroundTask
20
{
21
	/**
22
	 * This executes the task - loads up the info, puts the email in the queue and inserts any alerts as needed.
23
	 * @return bool Always returns true
24
	 */
25
	public function execute()
26
	{
27
		global $smcFunc, $sourcedir, $scripturl, $modSettings, $language;
28
29
		$msgOptions = $this->_details['msgOptions'];
30
		$topicOptions = $this->_details['topicOptions'];
31
		$posterOptions = $this->_details['posterOptions'];
32
		$type = $this->_details['type'];
33
34
		$members = array();
35
		$alert_rows = array();
36
37
		// We need to know who can approve this post.
38
		require_once($sourcedir . '/Subs-Members.php');
39
		$modMembers = membersAllowedTo('approve_posts', $topicOptions['board']);
40
41
		$request = $smcFunc['db_query']('', '
42
			SELECT id_member, email_address, lngfile
43
			FROM {db_prefix}members
44
			WHERE id_member IN ({array_int:members})',
45
			array(
46
				'members' => $modMembers,
47
			)
48
		);
49
50
		$watched = array();
51
		while ($row = $smcFunc['db_fetch_assoc']($request))
52
		{
53
			$members[] = $row['id_member'];
54
			$watched[$row['id_member']] = $row;
55
		}
56
		$smcFunc['db_free_result']($request);
57
58
		if (empty($members))
59
			return true;
60
61
		require_once($sourcedir . '/Subs-Notify.php');
62
		$members = array_unique($members);
63
		$prefs = getNotifyPrefs($members, 'unapproved_post', true);
64
		foreach ($watched as $member => $data)
65
		{
66
			$pref = !empty($prefs[$member]['unapproved_post']) ? $prefs[$member]['unapproved_post'] : 0;
67
68
			if ($pref & self::RECEIVE_NOTIFY_EMAIL)
69
			{
70
				// Emails are a bit complicated. We have to do language stuff.
71
				require_once($sourcedir . '/Subs-Post.php');
72
				require_once($sourcedir . '/ScheduledTasks.php');
73
				loadEssentialThemeData();
74
75
				$replacements = array(
76
					'SUBJECT' => $msgOptions['subject'],
77
					'LINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new',
78
				);
79
80
				$emaildata = loadEmailTemplate(
81
					'alert_unapproved_post',
82
					$replacements,
83
					empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile']
84
				);
85
				sendmail(
86
					$data['email_address'],
87
					$emaildata['subject'],
88
					$emaildata['body'],
89
					null,
90
					'm' . $topicOptions['id'],
91
					$emaildata['is_html']
92
				);
93
			}
94
95
			if ($pref & self::RECEIVE_NOTIFY_ALERT)
96
			{
97
				$alert_rows[] = array(
98
					'alert_time' => time(),
99
					'id_member' => $member,
100
					'id_member_started' => $posterOptions['id'],
101
					'member_name' => $posterOptions['name'],
102
					'content_type' => 'msg',
103
					'content_id' => $msgOptions['id'],
104
					'content_action' => 'unapproved_' . $type,
105
					'is_read' => 0,
106
					'extra' => $smcFunc['json_encode'](
107
						array(
108
							'topic' => $topicOptions['id'],
109
							'board' => $topicOptions['board'],
110
							'content_subject' => $msgOptions['subject'],
111
							'content_link' => '{SCRIPTURL}?msg={CONTENT_ID}',
112
						)
113
					),
114
				);
115
			}
116
		}
117
118
		// Insert the alerts if any
119
		if (!empty($alert_rows))
120
		{
121
			$smcFunc['db_insert'](
122
				'',
123
				'{db_prefix}user_alerts',
124
				array(
125
					'alert_time' => 'int',
126
					'id_member' => 'int',
127
					'id_member_started' => 'int',
128
					'member_name' => 'string',
129
					'content_type' => 'string',
130
					'content_id' => 'int',
131
					'content_action' => 'string',
132
					'is_read' => 'int',
133
					'extra' => 'string',
134
				),
135
				$alert_rows,
136
				array()
137
			);
138
139
			updateMemberData(array_keys($watched), array('alerts' => '+'));
140
		}
141
142
		return true;
143
	}
144
}
145
146
?>