Failed Conditions
Branch release-2.1 (4e22cf)
by Rick
07:22
created

ApproveReply_Notify_Background::execute()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 86
Code Lines 53

Duplication

Lines 49
Ratio 56.98 %

Importance

Changes 0
Metric Value
cc 9
eloc 53
nc 36
nop 0
dl 49
loc 86
rs 5.3477
c 0
b 0
f 0

How to fix   Long Method   

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 any create post action
4
 *
5
 * Simple Machines Forum (SMF)
6
 *
7
 * @package SMF
8
 * @author Simple Machines http://www.simplemachines.org
9
 * @copyright 2017 Simple Machines and individual contributors
10
 * @license http://www.simplemachines.org/about/smf/license.php BSD
11
 *
12
 * @version 2.1 Beta 4
13
 */
14
15
/**
16
 * Class ApproveReply_Notify_Background
17
 */
18
class ApproveReply_Notify_Background extends SMF_BackgroundTask
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
19
{
20
	/**
21
     * This executes the task - loads up the information, puts the email in the queue and inserts alerts.
22
	 * @return bool Always returns true.
23
	 */
24
	public function execute()
25
	{
26
		global $smcFunc, $sourcedir, $scripturl, $modSettings, $language;
27
28
		$msgOptions = $this->_details['msgOptions'];
29
		$topicOptions = $this->_details['topicOptions'];
30
		$posterOptions = $this->_details['posterOptions'];
31
32
		$members = array();
33
		$alert_rows = array();
34
35
		$request = $smcFunc['db_query']('', '
36
			SELECT id_member, email_address, lngfile
37
			FROM {db_prefix}topics AS t
38
				INNER JOIN {db_prefix}members AS mem ON (mem.id_member = t.id_member_started)
39
			WHERE id_topic = {int:topic}',
40
			array(
41
				'topic' => $topicOptions['id'],
42
			)
43
		);
44
45
		$watched = array();
46 View Code Duplication
		while ($row = $smcFunc['db_fetch_assoc']($request))
47
		{
48
			$members[] = $row['id_member'];
49
			$watched[$row['id_member']] = $row;
50
		}
51
		$smcFunc['db_free_result']($request);
52
53
		require_once($sourcedir . '/Subs-Notify.php');
54
		$prefs = getNotifyPrefs($members, 'unapproved_reply', true);
55
		foreach ($watched as $member => $data)
56
		{
57
			$pref = !empty($prefs[$member]['unapproved_reply']) ? $prefs[$member]['unapproved_reply'] : 0;
58
59 View Code Duplication
			if ($pref & 0x02)
60
			{
61
				// Emails are a bit complicated. We have to do language stuff.
62
				require_once($sourcedir . '/Subs-Post.php');
63
				require_once($sourcedir . '/ScheduledTasks.php');
64
				loadEssentialThemeData();
65
66
				$replacements = array(
67
					'SUBJECT' => $msgOptions['subject'],
68
					'LINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new',
69
					'POSTERNAME' => un_htmlspecialchars($posterOptions['name']),
70
				);
71
72
				$emaildata = loadEmailTemplate('alert_unapproved_reply', $replacements, empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile']);
73
				sendmail($data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'm' . $topicOptions['id'], $emaildata['is_html']);
74
			}
75
76 View Code Duplication
			if ($pref & 0x01)
77
			{
78
				$alert_rows[] = array(
79
					'alert_time' => time(),
80
					'id_member' => $member,
81
					'id_member_started' => $posterOptions['id'],
82
					'member_name' => $posterOptions['name'],
83
					'content_type' => 'unapproved',
84
					'content_id' => $topicOptions['id'],
85
					'content_action' => 'reply',
86
					'is_read' => 0,
87
					'extra' => $smcFunc['json_encode'](array(
88
						'topic' => $topicOptions['id'],
89
						'board' => $topicOptions['board'],
90
						'content_subject' => $msgOptions['subject'],
91
						'content_link' => $scripturl . '?topic=' . $topicOptions['id'] . '.new;topicseen#new',
92
					)),
93
				);
94
				updateMemberData($member, array('alerts' => '+'));
95
			}
96
		}
97
98
		// Insert the alerts if any
99 View Code Duplication
		if (!empty($alert_rows))
100
			$smcFunc['db_insert']('',
101
				'{db_prefix}user_alerts',
102
				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
103
					'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
104
				$alert_rows,
105
				array()
106
			);
107
108
		return true;
109
	}
110
}
111
112
?>