Passed
Pull Request — master (#1)
by Dark❶
03:32
created

auto_reduce_sync::run()   C

Complexity

Conditions 10
Paths 7

Size

Total Lines 97
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 66
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 97
rs 6.8751

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
 *
4
 * Reduce Search Index [RSI]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\reducesearchindex\cron;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\cron\task\base;
17
use phpbb\config\config;
18
use phpbb\db\driver\driver_interface;
19
use phpbb\log\log;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
/**
23
 * Reduce Search Index Cron Task.
24
 */
25
class auto_reduce_sync extends base
26
{
27
28
	/** @var \phpbb\config\config */
29
	protected $config;
30
31
	/** @var \phpbb\db\driver\driver_interface */
32
	protected $db;
33
34
	/** @var \phpbb\log\log */
35
	protected $phpbb_log;
36
37
	/** @var ContainerInterface */
38
	protected $phpbb_container;
39
40
	/**
41
	* Constructor for cron task
42
	*
43
	* @param \phpbb\config\config				$config			phpBB config
44
	* @param \phpbb\db\driver\driver_interface	$db				phpBB DBAL object
45
	* @param \phpbb\log\log						$phpbb_log		phpBB log
46
	* @param ContainerInterface					$phpbb_container
47
	* @access public
48
	*/
49
	public function __construct(config $config, driver_interface $db, log $phpbb_log, ContainerInterface $phpbb_container)
50
	{
51
		$this->config					= $config;
52
		$this->db						= $db;
53
		$this->phpbb_log				= $phpbb_log;
54
		$this->phpbb_container			= $phpbb_container;
55
	}
56
57
	/**
58
	* Returns whether this cron task can run, given current board configuration.
59
	*
60
	* @return bool
61
	*/
62
	public function is_runnable()
63
	{
64
		return ($this->config['dark1_rsi_auto_reduce_sync_enable'] && $this->config['dark1_rsi_enable']);
65
	}
66
67
	/**
68
	* Returns whether this cron task should run now, because enough time has passed since it was last run.
69
	*
70
	* @return bool
71
	*/
72
	public function should_run()
73
	{
74
		return (($this->config['dark1_rsi_auto_reduce_sync_last_gc'] < (time() - $this->config['dark1_rsi_auto_reduce_sync_gc'])) && ($this->config['dark1_rsi_time'] < (time() - $this->config['dark1_rsi_interval'])));
75
	}
76
77
	/**
78
	* Runs this cron task.
79
	*
80
	* @return null
81
	*/
82
	public function run()
83
	{
84
		if ($this->config['dark1_rsi_enable'] && ($this->config['dark1_rsi_time'] < (time() - $this->config['dark1_rsi_interval'])))
85
		{
86
			$this->config->set('dark1_rsi_time', (time() - $this->config['dark1_rsi_interval']), false);
87
			$post_ids = $poster_ids = $topic_ids = $forum_ids = array();
88
89
			$sql = 'SELECT t.topic_id, p.post_id, p.poster_id, p.forum_id' . PHP_EOL .
90
					'FROM ' . POSTS_TABLE . ' as p' . PHP_EOL .
91
					'LEFT JOIN ' . TOPICS_TABLE . ' as t' . PHP_EOL .
92
					'ON t.topic_id = p.topic_id' . PHP_EOL .
93
					'LEFT JOIN ' . FORUMS_TABLE . ' as f' . PHP_EOL .
94
					'ON f.forum_id = p.forum_id' . PHP_EOL .
95
					'WHERE f.dark1_rsi_f_enable = 3 AND t.topic_time <= ' . (int) $this->config['dark1_rsi_time'];
96
			$result = $this->db->sql_query($sql);
97
98
			while ($row = $this->db->sql_fetchrow($result))
99
			{
100
				$post_ids[] = (int) $row['post_id'];
101
				$poster_ids[] = (int) $row['poster_id'];
102
				$topic_ids[] = (int) $row['topic_id'];
103
				$forum_ids[] = (int) $row['forum_id'];
104
			}
105
			$this->db->sql_freeresult($result);
106
107
			$sql = 'SELECT p.post_id, p.poster_id, p.forum_id' . PHP_EOL .
108
					'FROM ' . POSTS_TABLE . ' as p' . PHP_EOL .
109
					'LEFT JOIN ' . TOPICS_TABLE . ' as t' . PHP_EOL .
110
					'ON t.topic_id = p.topic_id' . PHP_EOL .
111
					'LEFT JOIN ' . FORUMS_TABLE . ' as f' . PHP_EOL .
112
					'ON f.forum_id = p.forum_id' . PHP_EOL .
113
					'WHERE f.dark1_rsi_f_enable = 2 AND t.topic_time <= ' . (int) $this->config['dark1_rsi_time'];
114
			$result = $this->db->sql_query($sql);
115
116
			while ($row = $this->db->sql_fetchrow($result))
117
			{
118
				$post_ids[] = (int) $row['post_id'];
119
				$poster_ids[] = (int) $row['poster_id'];
120
				$forum_ids[] = (int) $row['forum_id'];
121
			}
122
			$this->db->sql_freeresult($result);
123
124
			$sql = 'SELECT p.post_id, p.poster_id, p.forum_id' . PHP_EOL .
125
					'FROM ' . POSTS_TABLE . ' as p' . PHP_EOL .
126
					'LEFT JOIN ' . FORUMS_TABLE . ' as f' . PHP_EOL .
127
					'ON f.forum_id = p.forum_id' . PHP_EOL .
128
					'WHERE f.dark1_rsi_f_enable = 1 AND p.post_time <= ' . (int) $this->config['dark1_rsi_time'];
129
			$result = $this->db->sql_query($sql);
130
131
			while ($row = $this->db->sql_fetchrow($result))
132
			{
133
				$post_ids[] = (int) $row['post_id'];
134
				$poster_ids[] = (int) $row['poster_id'];
135
				$forum_ids[] = (int) $row['forum_id'];
136
			}
137
			$this->db->sql_freeresult($result);
138
139
			$post_ids = $this->array_unique_sort($post_ids);
140
			$poster_ids = $this->array_unique_sort($poster_ids);
141
			$topic_ids = $this->array_unique_sort($topic_ids);
142
			$forum_ids = $this->array_unique_sort($forum_ids);
143
144
			// Lock Topics
145
			if (count($topic_ids) > 0)
146
			{
147
				$sql = 'UPDATE ' . TOPICS_TABLE . PHP_EOL .
148
						'SET topic_status = ' . ITEM_LOCKED  . PHP_EOL .
149
						'WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids);
150
				$this->db->sql_query($sql);
151
			}
152
153
			// Remove the message from the search index
154
			$search_type = $this->config['search_type'];
155
			$identifier = substr($search_type, strrpos($search_type, '\\') + 1);
156
			if ($identifier == 'fulltext_native' && class_exists($search_type))
157
			{
158
				$error = false;
159
				$phpbb_root_path = $this->phpbb_container->getParameter('core.root_path');
160
				$phpEx = $this->phpbb_container->getParameter('core.php_ext');
161
				$auth = $this->phpbb_container->get('auth');
162
				$user = $this->phpbb_container->get('user');
163
				$phpbb_dispatcher = $this->phpbb_container->get('dispatcher');
164
165
				$search = new $search_type($error, $phpbb_root_path, $phpEx, $auth, $this->config, $this->db, $user, $phpbb_dispatcher);
166
				if ($error == false)
0 ignored issues
show
introduced by
The condition $error == false is always true.
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
167
				{
168
					@$search->index_remove($post_ids, $poster_ids, $forum_ids);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for index_remove(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

168
					/** @scrutinizer ignore-unhandled */ @$search->index_remove($post_ids, $poster_ids, $forum_ids);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
169
				}
170
			}
171
172
			$dark1_rsi_interval = $this->config['dark1_rsi_interval'] / 86400;
173
			$dark1_rsi_time = date('Y-m-d h:i:s A P', $this->config['dark1_rsi_time']);
0 ignored issues
show
Bug introduced by
$this->config['dark1_rsi_time'] of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
			$dark1_rsi_time = date('Y-m-d h:i:s A P', /** @scrutinizer ignore-type */ $this->config['dark1_rsi_time']);
Loading history...
174
			$this->phpbb_log->add('admin', '', '', 'RSI_AUTO_LOG', time(), array($dark1_rsi_interval, $dark1_rsi_time));
175
		}
176
177
		// Update the last backup time
178
		$this->config->set('dark1_rsi_auto_reduce_sync_last_gc', time(), false);
179
	}
180
181
	/**
182
	* Array to Uniquely Sort the IDs.
183
	*
184
	* @param array		$ary_ids		Array with IDs
185
	* @return array
186
	* @access private
187
	*/
188
	private function array_unique_sort($ary_ids)
189
	{
190
		$ary_ids = array_unique($ary_ids);
191
		sort($ary_ids, SORT_NUMERIC);
192
		return $ary_ids;
193
	}
194
195
}
196