cron   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 227
Duplicated Lines 6.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 14
loc 227
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A auto_check() 0 25 2
A get_cat() 14 14 2
B _check() 0 45 7
B _update_check() 0 61 5

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
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\core;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class cron extends helper
16
{
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/** @var \phpbb\config\config */
21
	protected $config;
22
23
	/** @var \phpbb\log\log */
24
	protected $phpbb_log;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \phpbb\notification\manager */
30
	protected $notification;
31
32
	/** @var \ernadoo\phpbbdirectory\core\link */
33
	protected $link;
34
35
	/** @var string phpBB root path */
36
	protected $root_path;
37
38
	/** @var string phpEx */
39
	protected $php_ext;
40
41
	/**
42
	* Constructor
43
	*
44
	* @param \phpbb\db\driver\driver_interface 	$db					Database object
45
	* @param \phpbb\config\config 				$config				Config object
46
	* @param \phpbb\log\log						$phpbb_log			Log object
47
	* @param \phpbb\user 						$user				User object
48
	* @param \phpbb\notification\manager		$notification		Notification object
49
	* @param \ernadoo\phpbbdirectory\core\link	$link				PhpBB Directory extension link object
50
	* @param string         					$root_path			phpBB root path
51
	* @param string         					$php_ext			phpEx
52
	*/
53
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\log\log $phpbb_log, \phpbb\user $user, \phpbb\notification\manager $notification, \ernadoo\phpbbdirectory\core\link $link, $root_path, $php_ext)
54
	{
55
		$this->db				= $db;
56
		$this->config			= $config;
57
		$this->phpbb_log		= $phpbb_log;
58
		$this->user				= $user;
59
		$this->notification		= $notification;
60
		$this->link				= $link;
61
		$this->root_path		= $root_path;
62
		$this->php_ext			= $php_ext;
63
	}
64
65
	/**
66
	* Method called by cron task.
67
	*
68
	* @param	array	$cat_data	Information about category, from db
69
	* @return	null
70
	*/
71
	public function auto_check($cat_data)
72
	{
73
		$sql = 'SELECT cat_name
74
			FROM ' . $this->categories_table . '
75
			WHERE cat_id = ' . (int) $cat_data['cat_id'];
76
		$result = $this->db->sql_query($sql);
77
		$row = $this->db->sql_fetchrow($result);
78
		$this->db->sql_freeresult($result);
79
80
		if ($row)
81
		{
82
			$next_prune = time() + ($cat_data['cat_cron_freq'] * 86400);
83
84
			$this->_check($cat_data['cat_id'], $cat_data['cat_cron_nb_check'], $next_prune);
85
86
			$sql = 'UPDATE ' . $this->categories_table . "
87
			SET cat_cron_next = $next_prune
88
			WHERE cat_id = " . (int) $cat_data['cat_id'];
89
			$this->db->sql_query($sql);
90
91
			$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_AUTO_PRUNE', time(), array($row['cat_name']));
92
		}
93
94
		return;
95
	}
96
97
	/**
98
	 * Return cron informations about a category.
99
	 *
100
	 * @param	int	$cat_id	The category ID
101
	 * @return	array
102
	 */
103 View Code Duplication
	public function get_cat($cat_id)
104
	{
105
		$sql = 'SELECT cat_id, cat_cron_enable, cat_cron_next, cat_cron_freq, cat_cron_nb_check
106
				FROM ' . $this->categories_table . '
107
					WHERE cat_id = ' . (int) $cat_id;
108
		$result = $this->db->sql_query($sql);
109
		$row = $this->db->sql_fetchrow($result);
110
		$this->db->sql_freeresult($result);
111
112
		if ($row)
113
		{
114
			return $row;
115
		}
116
	}
117
118
	/**
119
	* Check, for website with backlink specified, if backlink is always here.
120
	* After $nb_check verification, website is deleted, otherwise, a notification is send to poster
121
	*
122
	* @param	int		$cat_id		The category ID
123
	* @param	int		$nb_check	Number of check before demete a website
124
	* @param	int		$next_prune	Date of next auto check
125
	* @return	null
126
	*/
127
	private function _check($cat_id, $nb_check, $next_prune)
128
	{
129
		$del_array = $update_array = array();
130
131
		$sql_array = array(
132
			'SELECT'	=> 'link_id, link_cat, link_back, link_guest_email, link_nb_check, link_user_id, link_name, link_url, link_description, u.user_lang, u.user_dateformat',
133
			'FROM'		=> array(
134
				$this->links_table	=> 'l'),
135
			'LEFT_JOIN'	=> array(
136
				array(
137
					'FROM'	=> array(USERS_TABLE	=> 'u'),
138
					'ON'	=> 'l.link_user_id = u.user_id'
139
				)
140
			),
141
			'WHERE'		=> "l.link_back <> '' AND l.link_active = 1 AND l.link_cat = "  . (int) $cat_id);
142
143
			$sql = $this->db->sql_build_query('SELECT', $sql_array);
144
			$result = $this->db->sql_query($sql);
145
146
			while ($row = $this->db->sql_fetchrow($result))
147
			{
148
				if ($this->link->validate_link_back($row['link_back'], false, true) !== false)
149
				{
150
					if (!$nb_check || ($row['link_nb_check']+1) >= $nb_check)
151
					{
152
						$del_array[] = $row['link_id'];
153
					}
154
					else
155
					{
156
						// A first table containing links ID to update
157
						$update_array[$row['link_id']] = $row;
158
					}
159
				}
160
			}
161
			$this->db->sql_freeresult($result);
162
163
			if (count($del_array))
164
			{
165
				$this->link->del($cat_id, $del_array);
166
			}
167
			if (count($update_array))
168
			{
169
				$this->_update_check($update_array, $next_prune);
170
			}
171
	}
172
173
	/**
174
	* Update website verification number after a missing backlink, and send notificaton
175
	*
176
	* @param	array	$u_array	Information about website
177
	* @param	int		$next_prune	Date of next auto check
178
	* @return	null
179
	*/
180
	private function _update_check($u_array, $next_prune)
181
	{
182
		if (!class_exists('messenger'))
183
		{
184
			include($this->root_path . 'includes/functions_messenger.' . $this->php_ext);
185
		}
186
187
		$messenger = new \messenger(false);
188
189
		// cron.php don't call $user->setup(), so $this->timezone is unset.
190
		// We need to define it, because we use user->format_date below
191
		$this->user->timezone = new \DateTimeZone($this->config['board_timezone']);
192
193
		$sql = 'UPDATE ' . $this->links_table . '
194
			SET link_nb_check = link_nb_check + 1
195
			WHERE ' . $this->db->sql_in_set('link_id', array_keys($u_array));
196
		$this->db->sql_query($sql);
197
198
		foreach ($u_array as $data)
199
		{
200
			strip_bbcode($data['link_description']);
201
202
			$notification_data = array(
203
				'cat_name'			=> \ernadoo\phpbbdirectory\core\categorie::getname((int) $data['link_cat']),
204
				'link_id'			=> $data['link_id'],
205
				'link_user_id'		=> $data['link_user_id'],
206
				'link_name'			=> $data['link_name'],
207
				'link_url'			=> $data['link_url'],
208
				'link_description'	=> $data['link_description'],
209
				'next_cron' 		=> $this->user->format_date($next_prune, $data['user_dateformat']),
210
			);
211
212
			if ($data['link_nb_check'])
213
			{
214
				$this->notification->delete_notifications('ernadoo.phpbbdirectory.notification.type.directory_website_error_cron', $notification_data);
215
			}
216
217
			// New notification system can't send mail to an anonymous user with an email address stored in another table than phpbb_users
218
			if ($data['link_user_id'] == ANONYMOUS)
219
			{
220
				$username = $email = $data['link_guest_email'];
221
222
				$messenger->template('@ernadoo_phpbbdirectory/directory_website_error_cron', $data['user_lang']);
223
				$messenger->to($email, $username);
224
225
				$messenger->assign_vars(array(
226
					'USERNAME'			=> htmlspecialchars_decode($username),
227
					'LINK_NAME'			=> $data['link_name'],
228
					'LINK_URL'			=> $data['link_url'],
229
					'LINK_DESCRIPTION'	=> $data['link_description'],
230
					'NEXT_CRON' 		=> $this->user->format_date($next_prune, $data['user_dateformat']),
231
				));
232
233
				$messenger->send(NOTIFY_EMAIL);
234
			}
235
			else
236
			{
237
				$this->notification->add_notifications('ernadoo.phpbbdirectory.notification.type.directory_website_error_cron', $notification_data);
238
			}
239
		}
240
	}
241
}
242