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

ApprovePost_Notify_Background::execute()   C

Complexity

Conditions 10
Paths 38

Size

Total Lines 93
Code Lines 58

Duplication

Lines 48
Ratio 51.61 %

Importance

Changes 0
Metric Value
cc 10
eloc 58
nc 38
nop 0
dl 48
loc 93
rs 5.0515
c 0
b 0
f 0

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 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 ApprovePost_Notify_Background
17
 */
18
class ApprovePost_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 info, puts the email in the queue and inserts any alerts as needed.
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
		$type = $this->_details['type'];
32
33
		$members = array();
34
		$alert_rows = array();
35
36
		// We need to know who can approve this post.
37
		require_once($sourcedir . '/Subs-Members.php');
38
		$modMembers = membersAllowedTo('approve_posts', $topicOptions['board']);
39
40
		$request = $smcFunc['db_query']('', '
41
			SELECT id_member, email_address, lngfile
42
			FROM {db_prefix}members
43
			WHERE id_member IN({array_int:members})',
44
			array(
45
				'members' => $modMembers,
46
			)
47
		);
48
49
		$watched = array();
50 View Code Duplication
		while ($row = $smcFunc['db_fetch_assoc']($request))
51
		{
52
			$members[] = $row['id_member'];
53
			$watched[$row['id_member']] = $row;
54
		}
55
		$smcFunc['db_free_result']($request);
56
57
		if (empty($members))
58
			return true;
59
60
		require_once($sourcedir . '/Subs-Notify.php');
61
		$members = array_unique($members);
62
		$prefs = getNotifyPrefs($members, 'unapproved_post', true);
63
		foreach ($watched as $member => $data)
64
		{
65
			$pref = !empty($prefs[$member]['unapproved_post']) ? $prefs[$member]['unapproved_post'] : 0;
66
67 View Code Duplication
			if ($pref & 0x02)
68
			{
69
				// Emails are a bit complicated. We have to do language stuff.
70
				require_once($sourcedir . '/Subs-Post.php');
71
				require_once($sourcedir . '/ScheduledTasks.php');
72
				loadEssentialThemeData();
73
74
				$replacements = array(
75
					'SUBJECT' => $msgOptions['subject'],
76
					'LINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new',
77
				);
78
79
				$emaildata = loadEmailTemplate('alert_unapproved_post', $replacements, empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile']);
80
				sendmail($data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'm' . $topicOptions['id'], $emaildata['is_html']);
81
			}
82
83 View Code Duplication
			if ($pref & 0x01)
84
			{
85
				$alert_rows[] = array(
86
					'alert_time' => time(),
87
					'id_member' => $member,
88
					'id_member_started' => $posterOptions['id'],
89
					'member_name' => $posterOptions['name'],
90
					'content_type' => 'unapproved',
91
					'content_id' => $topicOptions['id'],
92
					'content_action' => $type,
93
					'is_read' => 0,
94
					'extra' => $smcFunc['json_encode'](array(
95
						'topic' => $topicOptions['id'],
96
						'board' => $topicOptions['board'],
97
						'content_subject' => $msgOptions['subject'],
98
						'content_link' => $scripturl . '?topic=' . $topicOptions['id'] . '.new;topicseen#new',
99
					)),
100
				);
101
				updateMemberData($member, array('alerts' => '+'));
102
			}
103
		}
104
105
		// Insert the alerts if any
106 View Code Duplication
		if (!empty($alert_rows))
107
			$smcFunc['db_insert']('',
108
				'{db_prefix}user_alerts',
109
				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
110
					'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
111
				$alert_rows,
112
				array()
113
			);
114
115
		return true;
116
	}
117
}
118
119
?>