Passed
Push — master ( bc9a55...f3faba )
by Dark❶
07:56
created

unc_table   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_notify_method_type_value() 0 6 2
A update_user_notifications_table() 0 28 6
A set_notify_method_type_matrix() 0 29 4
A get_notify_method_type_matrix() 0 20 3
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. 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\usernotificationcontrol\core;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\db\driver\driver_interface as db_driver;
17
use phpbb\cache\driver\driver_interface as cache_driver;
18
19
/**
20
 * User Notification Control Core Table Class.
21
 */
22
class unc_table
23
{
24
	/** @var string Table Name */
25
	const TABLE_NAME = 'dark1_unc';
26
27
	/** @var string Cache Key */
28
	const CACHE_KEY = '_dark1_unc_notify_matrix';
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\cache\driver\driver_interface */
34
	protected $cache;
35
36
	/** @var string */
37
	protected $table_prefix;
38
39
	/**
40
	 * Constructor for User Notification Control Core Table Class.
41
	 *
42
	 * @param \phpbb\db\driver\driver_interface		$db				Database object
43
	 * @param \phpbb\cache\driver\driver_interface	$cache			Cache object
44
	 * @param string								$table_prefix
45
	 */
46
	public function __construct(db_driver $db, cache_driver $cache, $table_prefix)
47
	{
48
		$this->db				= $db;
49
		$this->cache			= $cache;
50
		$this->table_prefix		= $table_prefix;
51
	}
52
53
54
55
	/**
56
	 * Get Notification Method & Type is Enabled {true} OR Disabled {false}.
57
	 *
58
	 * @param string $notification_method
59
	 * @param string $notification_type
60
	 *
61
	 * @return bool|null Enabled {true} OR Disabled {false}
62
	 * @access public
63
	 */
64
	public function get_notify_method_type_value($notification_method, $notification_type)
65
	{
66
		// Get notify matrix
67
		$notify_matrix = $this->get_notify_method_type_matrix();
68
69
		return isset($notify_matrix[$notification_method][$notification_type]) ? $notify_matrix[$notification_method][$notification_type] : null;
70
	}
71
72
73
74
	/**
75
	 * Get Notification Methods & Types is Enabled {true} OR Disabled {false} in Matrix.
76
	 *
77
	 * @return array in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
78
	 * @access public
79
	 */
80
	public function get_notify_method_type_matrix()
81
	{
82
		// Get notify matrix data from the cache
83
		$notify_matrix = $this->cache->get(self::CACHE_KEY);
84
85
		if ($notify_matrix === false)
86
		{
87
			$sql = 'SELECT * FROM ' . $this->table_prefix . self::TABLE_NAME;
88
			$result = $this->db->sql_query($sql);
89
			while ($row = $this->db->sql_fetchrow($result))
90
			{
91
				$notify_matrix[$row['notification_method']][$row['notification_type']] = $row['notification_value'];
92
			}
93
			$this->db->sql_freeresult($result);
94
95
			// Cache notify matrix data
96
			$this->cache->put(self::CACHE_KEY, $notify_matrix);
97
		}
98
99
		return $notify_matrix;
100
	}
101
102
103
104
	/**
105
	 * Set Notification Methods & Types is Disabled {true}in Matrix.
106
	 *
107
	 * @param array $notify_matrix in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
108
	 *
109
	 * @return void
110
	 * @access public
111
	 */
112
	public function set_notify_method_type_matrix($notify_matrix)
113
	{
114
		// Truncate the Table
115
		$sql = 'DELETE FROM ' . $this->table_prefix . self::TABLE_NAME;
116
		$this->db->sql_query($sql);
117
118
		// Multi-Insert the
119
		$sr = 0;
120
		$sql_ary = [];
121
		foreach ($notify_matrix as $notification_method => $ary_type)
122
		{
123
			foreach ($ary_type as $notification_type => $notification_value)
124
			{
125
				if (isset($notification_value))
126
				{
127
					$sql_ary[] = [
128
						'notification_sr_no' 	=> $sr,
129
						'notification_method' 	=> $notification_method,
130
						'notification_type' 	=> $notification_type,
131
						'notification_value' 	=> $notification_value,
132
					];
133
					$sr++;
134
				}
135
			}
136
		}
137
		$this->db->sql_multi_insert($this->table_prefix . self::TABLE_NAME, $sql_ary);
138
139
		// Cache notify matrix data
140
		$this->cache->put(self::CACHE_KEY, $notify_matrix);
141
	}
142
143
144
145
	/**
146
	 * Update User Notification Table using No Notify Matrix.
147
	 *
148
	 * @param array $notify_matrix in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
149
	 * @param bool $update to Update or not
150
	 *
151
	 * @return void
152
	 * @access public
153
	 */
154
	public function update_user_notifications_table($notify_matrix, $update)
155
	{
156
		if ($update)
157
		{
158
			foreach ($notify_matrix as $notification_method => $ary_type)
159
			{
160
				$sql_ary = [
161
					0 => '',
162
					1 => ''
163
				];
164
				$flag_1st = [
165
					0 => true,
166
					1 => true
167
				];
168
169
				foreach ($ary_type as $notification_type => $value)
170
				{
171
					$sql_ary[$value] .= (($flag_1st[$value]) ? '' : 'OR ' ) . 'item_type = "' . $this->db->sql_escape($notification_type) . '"' . PHP_EOL;
172
					$flag_1st[$value] = false;
173
				}
174
175
				foreach ($sql_ary as $key => $value)
176
				{
177
					$sql = 'UPDATE ' . USER_NOTIFICATIONS_TABLE .
178
							' SET notify = ' . (int) $key .
179
							' WHERE method = "' . $this->db->sql_escape($notification_method) . '"' .
180
							' AND (' . PHP_EOL . (string) $value . ')';
181
					$this->db->sql_query($sql);
182
				}
183
			}
184
		}
185
	}
186
187
}
188