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

ApprovePost_Notify_Background   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 48
loc 100
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C execute() 48 93 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
?>