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

status   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Importance

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

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