Passed
Push — master ( 146fdc...6e4ec6 )
by Dark❶
02:30
created

acp_cron   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 33 3
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\controller;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\config\config;
17
use phpbb\template\template;
18
use phpbb\user;
19
use phpbb\language\language;
20
use phpbb\log\log;
21
use phpbb\request\request;
22
use phpbb\cron\manager as cron_manager;
23
24
/**
25
 * Reduce Search Index [RSI] ACP controller Cron.
26
 */
27
class acp_cron extends acp_base
28
{
29
	/** @var \phpbb\config\config */
30
	protected $config;
31
32
	/** @var \phpbb\cron\manager */
33
	protected $cron_manager;
34
35
	/**
36
	 * Constructor.
37
	 *
38
	 * @param \phpbb\language\language				$language		Language object
39
	 * @param \phpbb\log\log						$log			Log object
40
	 * @param \phpbb\request\request				$request		Request object
41
	 * @param \phpbb\template\template				$template		Template object
42
	 * @param \phpbb\user							$user			User object
43
	 * @param \phpbb\config\config					$config			Config object
44
	 * @param \phpbb\cron\manager					$cron_manager	Cron manager
45
	 */
46
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, cron_manager $cron_manager)
47
	{
48
		parent::__construct($language, $log, $request, $template, $user);
49
50
		$this->config		= $config;
51
		$this->cron_manager	= $cron_manager;
52
	}
53
54
	/**
55
	 * Display the options a user can configure for Cron Mode.
56
	 *
57
	 * @return void
58
	 */
59
	public function handle()
60
	{
61
		// Is the form being submitted to us?
62
		if ($this->request->is_set_post('submit'))
63
		{
64
			$this->check_form_on_submit();
65
66
			// Set the options the user configured
67
			$this->config->set('dark1_rsi_auto_reduce_sync_enable', $this->request->variable('dark1_rsi_auto_reduce_sync_enable', 0));
68
			$this->config->set('dark1_rsi_auto_reduce_sync_gc', ($this->request->variable('dark1_rsi_auto_reduce_sync_gc', 0)) * 86400);
69
			$this->config->set('dark1_rsi_auto_reduce_sync_last_gc', strtotime($this->request->variable('dark1_rsi_auto_reduce_sync_last_gc', '0', true)), false);
70
71
			$this->success_form_on_submit();
72
		}
73
74
		// Run Cron Task
75
		if ($this->request->is_set_post('runcrontask'))
76
		{
77
			$this->check_form_on_submit();
78
79
			$cron_task = $this->cron_manager->find_task('dark1.reducesearchindex.cron.auto_reduce_sync');
80
			$cron_task->run();
81
82
			$this->success_form_on_submit();
83
		}
84
85
		// Set output variables for display in the template
86
		$this->template->assign_vars([
87
			'ENABLE_CRON'		=> $this->config['dark1_rsi_auto_reduce_sync_enable'],
88
			'CRON_INTERVAL'		=> ($this->config['dark1_rsi_auto_reduce_sync_gc'] / 86400),
89
			'CRON_LAST_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'], 'Y-m-d h:i:s A P', true),
90
			'CRON_NEXT_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] + $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
91
			'CRON_PREV_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] - $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
92
		]);
93
	}
94
}
95