Passed
Pull Request — master (#159)
by Matt
01:38
created

status   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 10
Bugs 1 Features 0
Metric Value
wmc 22
eloc 49
c 10
b 1
f 0
dl 0
loc 266
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A set_config() 0 3 1
A set_idea_factory() 0 3 1
A set_controller_helper() 0 3 1
A set_user_loader() 0 3 1
A get_type() 0 3 1
A create_insert_array() 0 8 1
A is_available() 0 3 1
A get_avatar() 0 4 2
A find_users_for_notification() 0 11 2
A users_to_query() 0 3 1
A get_reference() 0 3 1
A pre_create_insert_array() 0 12 2
A get_item_id() 0 3 1
A get_url() 0 5 1
A get_email_template() 0 3 1
A get_email_template_variables() 0 6 1
A get_title() 0 7 2
A get_item_parent_id() 0 3 1
1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\notification\type;
12
13
use phpbb\ideas\ext;
14
15
/**
16
 * Ideas status change notification class.
17
 */
18
class status extends \phpbb\notification\type\base
0 ignored issues
show
Bug introduced by
The type phpbb\notification\type\base was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
{
20
	/** @var \phpbb\config\config */
21
	protected $config;
22
23
	/** @var \phpbb\controller\helper */
24
	protected $helper;
25
26
	/** @var \phpbb\ideas\factory\idea */
27
	protected $idea;
28
29
	/** @var \phpbb\user_loader */
30
	protected $user_loader;
31
32
	/**
33
	 * Set the controller helper
34
	 *
35
	 * @param \phpbb\controller\helper $helper
36
	 *
37
	 * @return void
38
	 */
39
	public function set_controller_helper(\phpbb\controller\helper $helper)
40
	{
41
		$this->helper = $helper;
42
	}
43
44
	/**
45
	 * Set the Idea object
46
	 *
47
	 * @param \phpbb\ideas\factory\idea $idea
48
	 *
49
	 * @return void
50
	 */
51
	public function set_idea_factory(\phpbb\ideas\factory\idea $idea)
52
	{
53
		$this->idea = $idea;
54
	}
55
56
	/**
57
	 * Set the config object
58
	 *
59
	 * @param \phpbb\config\config $config
60
	 *
61
	 * @return void
62
	 */
63
	public function set_config(\phpbb\config\config $config)
64
	{
65
		$this->config = $config;
66
	}
67
68
	public function set_user_loader(\phpbb\user_loader $user_loader)
69
	{
70
		$this->user_loader = $user_loader;
71
	}
72
73
	/**
74
	 * Get notification type name
75
	 *
76
	 * @return string
77
	 */
78
	public function get_type()
79
	{
80
		return 'phpbb.ideas.notification.type.status';
81
	}
82
83
	/**
84
	 * Email template to use to send notifications
85
	 *
86
	 * @var string
87
	 */
88
	public $email_template = '@phpbb_ideas/status_notification';
89
90
	/**
91
	 * Language key used to output the text
92
	 *
93
	 * @var string
94
	 */
95
	protected $language_key = 'IDEA_STATUS_CHANGE';
96
97
	/**
98
	 * Notification option data (for outputting to the user)
99
	 *
100
	 * @var bool|array False if the service should use its default data
101
	 * 					Array of data (including keys 'id', 'lang', and 'group')
102
	 */
103
	public static $notification_option = [
104
		'lang'	=> 'NOTIFICATION_TYPE_IDEAS',
105
		'group'	=> 'NOTIFICATION_GROUP_MISCELLANEOUS',
106
	];
107
108
	/**
109
	 * Is this type available to the current user (defines whether it will be shown in the UCP Edit notification options)
110
	 *
111
	 * @return bool True/False whether this is available to the user
112
	 */
113
	public function is_available()
114
	{
115
		return (bool) $this->auth->acl_get('f_read', (int) $this->config['ideas_forum_id']);
116
	}
117
118
	/**
119
	 * Get the id of the notification
120
	 *
121
	 * @param array $type_data The type-specific data
122
	 *
123
	 * @return int ID of the notification
124
	 */
125
	public static function get_item_id($type_data)
126
	{
127
		return (int) $type_data['item_id'];
128
	}
129
130
	/**
131
	 * Get the id of the parent
132
	 *
133
	 * @param array $type_data The type-specific data
134
	 *
135
	 * @return int ID of the parent
136
	 */
137
	public static function get_item_parent_id($type_data)
138
	{
139
		return 0;
140
	}
141
142
	/**
143
	 * Find the users who want to receive notifications
144
	 *
145
	 * @param array $type_data The type-specific data
146
	 * @param array $options Options for finding users for notification
147
	 * 		ignore_users => array of users and user types that should not receive notifications from this type because they've already been notified
148
	 * 						e.g.: [2 => [''], 3 => ['', 'email'], ...]
149
	 *
150
	 * @return array
151
	 */
152
	public function find_users_for_notification($type_data, $options = [])
153
	{
154
		$options = array_merge([
155
			'ignore_users'		=> [],
156
		], $options);
157
158
		$idea = $this->idea->get_idea($type_data['idea_id']);
159
160
		$users = $idea ? [$idea['idea_author']] : [];
161
162
		return $this->check_user_notification_options($users, $options);
163
	}
164
165
	/**
166
	 * Get the user's avatar
167
	 */
168
	public function get_avatar()
169
	{
170
		$author = (int) $this->get_data('idea_author');
171
		return $author ? $this->user_loader->get_avatar($author, true) : '';
172
	}
173
174
	/**
175
	 * Users needed to query before this notification can be displayed
176
	 *
177
	 * @return array Array of user_ids
178
	 */
179
	public function users_to_query()
180
	{
181
		return [];
182
	}
183
184
	/**
185
	 * Get the HTML-formatted title of this notification
186
	 *
187
	 * @return string
188
	 */
189
	public function get_title()
190
	{
191
		if (!$this->language->is_set($this->language_key))
192
		{
193
			$this->language->add_lang('common', 'phpbb/ideas');
194
		}
195
		return $this->language->lang($this->language_key, $this->get_data('idea_title'));
196
	}
197
198
	/**
199
	 * Get the HTML-formatted reference of the notification
200
	 *
201
	 * @return string
202
	 */
203
	public function get_reference()
204
	{
205
		return  $this->language->lang(ext::status_name($this->get_data('status')));
206
	}
207
208
	/**
209
	 * Get the url to this item
210
	 *
211
	 * @return string URL
212
	 */
213
	public function get_url()
214
	{
215
		$params = ['idea_id' => $this->get_data('idea_id')];
216
217
		return $this->helper->route('phpbb_ideas_idea_controller', $params);
218
	}
219
220
	/**
221
	 * Get email template
222
	 *
223
	 * @return string|bool
224
	 */
225
	public function get_email_template()
226
	{
227
		return $this->email_template;
228
	}
229
230
	/**
231
	 * Get email template variables
232
	 *
233
	 * @return array
234
	 */
235
	public function get_email_template_variables()
236
	{
237
		return [
238
			'IDEA_TITLE'	=> html_entity_decode(censor_text($this->get_data('idea_title')), ENT_COMPAT),
0 ignored issues
show
Bug introduced by
The function censor_text was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

238
			'IDEA_TITLE'	=> html_entity_decode(/** @scrutinizer ignore-call */ censor_text($this->get_data('idea_title')), ENT_COMPAT),
Loading history...
239
			'STATUS'		=> html_entity_decode($this->language->lang(ext::status_name($this->get_data('status'))), ENT_COMPAT),
240
			'U_VIEW_IDEA'	=> $this->get_url(),
241
		];
242
	}
243
244
	/**
245
	 * Pre create insert array function
246
	 * This allows you to perform certain actions, like run a query
247
	 * and load data, before create_insert_array() is run. The data
248
	 * returned from this function will be sent to create_insert_array().
249
	 *
250
	 * @param array $type_data The type-specific data
251
	 * @param array $notify_users Notify users list
252
	 * 		Formatted from find_users_for_notification()
253
	 * @return array Whatever you want to send to create_insert_array().
254
	 */
255
	public function pre_create_insert_array($type_data, $notify_users)
256
	{
257
		$pre_create_data = [];
258
259
		$idea = $this->idea->get_idea($type_data['idea_id']);
260
		if ($idea !== false)
261
		{
262
			$pre_create_data['idea_title'] = $idea['idea_title'];
263
			$pre_create_data['idea_author'] = $idea['idea_author'];
264
		}
265
266
		return $pre_create_data;
267
	}
268
269
	/**
270
	 * Function for preparing the data for insertion in an SQL query
271
	 * (The service handles insertion)
272
	 *
273
	 * @param array $type_data The type-specific data
274
	 * @param array $pre_create_data Data from pre_create_insert_array()
275
	 */
276
	public function create_insert_array($type_data, $pre_create_data = [])
277
	{
278
		$this->set_data('idea_id', $type_data['idea_id']);
279
		$this->set_data('status', $type_data['status']);
280
		$this->set_data('idea_title', $pre_create_data['idea_title']);
281
		$this->set_data('idea_author', $pre_create_data['idea_author']);
282
283
		parent::create_insert_array($type_data, $pre_create_data);
284
	}
285
}
286