auto_prune_notify   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A is_runnable() 0 3 1
A should_run() 0 3 1
A run() 0 13 1
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-forever, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\usernotificationcontrol\cron;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\cron\task\base;
17
use phpbb\config\config;
18
use phpbb\notification\manager as notification_manager;
19
use phpbb\log\log;
20
21
/**
22
 * Prune All Notifications Cron Task.
23
 */
24
class auto_prune_notify extends base
25
{
26
	/** @var config */
27
	protected $config;
28
29
	/** @var notification_manager */
30
	protected $notification_manager;
31
32
	/** @var log */
33
	protected $phpbb_log;
34
35
	/** Time Format */
36
	const TIME_FORMAT	= 'Y-m-d h:i:s A P';
37
38
	/**
39
	* Constructor for cron task
40
	*
41
	* @param config					$config					phpBB config
42
	* @param notification_manager	$notification_manager	phpBB notification manager
43
	* @param log					$phpbb_log				phpBB log
44
	* @access public
45
	*/
46
	public function __construct(config $config, notification_manager $notification_manager, log $phpbb_log)
47
	{
48
		$this->config					= $config;
49
		$this->notification_manager		= $notification_manager;
50
		$this->phpbb_log				= $phpbb_log;
51
	}
52
53
	/**
54
	* Returns whether this cron task can run, given current board configuration.
55
	*
56
	* @return bool
57
	*/
58
	public function is_runnable()
59
	{
60
		return (bool) $this->config['dark1_unc_auto_prune_notify_enable'];
61
	}
62
63
	/**
64
	* Returns whether this cron task should run now, because enough time has passed since it was last run.
65
	*
66
	* @return bool
67
	*/
68
	public function should_run()
69
	{
70
		return (bool) ($this->config['dark1_unc_auto_prune_notify_last_gc'] < (time() - $this->config['dark1_unc_auto_prune_notify_gc']));
71
	}
72
73
	/**
74
	* Runs this cron task.
75
	*
76
	* @return null
77
	*/
78
	public function run()
79
	{
80
		// time minus expire days in seconds
81
		$days = $this->config['read_notification_expire_days'] + $this->config['dark1_unc_all_notify_expire_days'];
82
		$timestamp = time() - ($days * 86400);
83
		$this->notification_manager->prune_notifications((int) $timestamp, false);
84
85
		// Log the cron task run
86
		$before_date = date(self::TIME_FORMAT, (int) $timestamp);
87
		$this->phpbb_log->add('admin', ANONYMOUS, '127.0.0.1', 'UNC_AUTO_PRUNE_LOG', time(), [$days, $before_date]);
88
89
		// Update the last run time
90
		$this->config->set('dark1_unc_auto_prune_notify_last_gc', time(), false);
91
	}
92
}
93