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

status   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 22
eloc 50
c 11
b 1
f 0
dl 0
loc 270
rs 10

18 Methods

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

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