Passed
Pull Request — develop-3.2.x (#69)
by Mario
04:12
created

currency_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 9
dl 0
loc 25
rs 9.8333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller\admin;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\log\log;
16
use phpbb\request\request;
17
use phpbb\template\template;
18
use phpbb\user;
19
use skouat\ppde\operators\currency;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
/**
23
 * @property config             config             Config object
24
 * @property ContainerInterface container          Service container interface
25
 * @property string             id_prefix_name     Prefix name for identifier in the URL
26
 * @property string             lang_key_prefix    Prefix for the messages thrown by exceptions
27
 * @property language           language           Language user object
28
 * @property log                log                The phpBB log system
29
 * @property string             module_name        Name of the module currently used
30
 * @property request            request            Request object
31
 * @property bool               submit             State of submit $_POST variable
32
 * @property template           template           Template object
33
 * @property string             u_action           Action URL
34
 * @property user               user               User object
35
 */
36
class currency_controller extends admin_main
37
{
38
	protected $ppde_entity;
39
	protected $ppde_operator;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param config                       $config
45
	 * @param ContainerInterface           $container
46
	 * @param language                     $language
47
	 * @param log                          $log
48
	 * @param \skouat\ppde\entity\currency $ppde_entity_currency   PPDE Entity object
49
	 * @param currency                     $ppde_operator_currency PPDE Operator object
50
	 * @param request                      $request
51
	 * @param template                     $template
52
	 * @param user                         $user
53
	 *
54
	 * @access public
55
	 */
56
	public function __construct(
57
		config $config,
58
		ContainerInterface $container,
59
		language $language,
60
		log $log,
61
		\skouat\ppde\entity\currency $ppde_entity_currency,
62
		currency $ppde_operator_currency,
63
		request $request,
64
		template $template,
65
		user $user
66
	)
67
	{
68
		$this->config = $config;
69
		$this->container = $container;
70
		$this->language = $language;
71
		$this->log = $log;
72
		$this->ppde_entity = $ppde_entity_currency;
73
		$this->ppde_operator = $ppde_operator_currency;
74
		$this->request = $request;
75
		$this->template = $template;
76
		$this->user = $user;
77
		parent::__construct(
78
			'currency',
79
			'PPDE_DC',
80
			'currency'
81
		);
82
	}
83
84
	/**
85
	 * Display the currency list
86
	 *
87
	 * @return void
88
	 * @access public
89
	 */
90
	public function display()
91
	{
92
		// Check if currency_order is valid and fix it if necessary
93
		$this->ppde_operator->fix_currency_order();
94
95
		// Grab all the pages from the db
96
		$data_ary = $this->ppde_entity->get_data($this->ppde_operator->build_sql_data());
97
98
		array_map(array($this, 'currency_assign_template_vars'), $data_ary);
99
100
		$this->u_action_assign_template_vars();
101
	}
102
103
	/**
104
	 * Add a currency
105
	 *
106
	 * @return void
107
	 * @access public
108
	 */
109
	public function add()
110
	{
111
		// Add form key
112
		add_form_key('add_edit_currency');
113
114
		// Collect the form data
115
		$data = array(
116
			'currency_name'     => $this->request->variable('currency_name', '', true),
117
			'currency_iso_code' => $this->request->variable('currency_iso_code', '', true),
118
			'currency_symbol'   => $this->request->variable('currency_symbol', '', true),
119
			'currency_on_left'  => $this->request->variable('currency_on_left', true),
120
			'currency_enable'   => $this->request->variable('currency_enable', false),
121
		);
122
123
		// Process the new page
124
		$this->add_edit_currency_data($this->ppde_entity, $data);
125
126
		// Set output vars for display in the template
127
		$this->add_edit_action_assign_template_vars('add');
128
	}
129
130
	/**
131
	 * Process currency data to be added or edited
132
	 *
133
	 * @param \skouat\ppde\entity\currency $entity The currency entity object
134
	 * @param array                        $data   The form data to be processed
135
	 *
136
	 * @return void
137
	 * @access private
138
	 */
139
	private function add_edit_currency_data($entity, $data)
140
	{
141
		// Get form's POST actions (submit or preview)
142
		$this->submit = $this->request->is_set_post('submit');
143
144
		// Create an array to collect errors that will be output to the user
145
		$errors = array();
146
147
		// Set the currency's data in the entity
148
		$item_fields = array(
149
			'name'              => $data['currency_name'],
150
			'iso_code'          => $data['currency_iso_code'],
151
			'symbol'            => $data['currency_symbol'],
152
			'currency_position' => $data['currency_on_left'],
153
			'currency_enable'   => $data['currency_enable'],
154
		);
155
156
		$entity->set_entity_data($item_fields);
157
158
		// Check some settings before submitting data
159
		$errors = array_merge($errors,
160
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
161
			$this->is_empty_data($entity, 'name', '', $this->submit_or_preview($this->submit)),
162
			$this->is_empty_data($entity, 'iso_code', '', $this->submit_or_preview($this->submit)),
163
			$this->is_empty_data($entity, 'symbol', '', $this->submit_or_preview($this->submit))
164
		);
165
166
		// Insert or update currency
167
		$this->submit_data($entity, $errors);
168
169
		// Set output vars for display in the template
170
		$this->s_error_assign_template_vars($errors);
171
		$this->template->assign_vars(array(
172
			'CURRENCY_NAME'     => $entity->get_name(),
173
			'CURRENCY_ISO_CODE' => $entity->get_iso_code(),
174
			'CURRENCY_SYMBOL'   => $entity->get_symbol(),
175
			'CURRENCY_POSITION' => $entity->get_currency_position(),
176
			'CURRENCY_ENABLE'   => $entity->get_currency_enable(),
177
178
			'S_HIDDEN_FIELDS'   => '<input type="hidden" name="' . $this->id_prefix_name . '_id" value="' . $entity->get_id() . '">',
179
		));
180
	}
181
182
	/**
183
	 * Submit data to the database
184
	 *
185
	 * @param \skouat\ppde\entity\currency $entity The currency entity object
186
	 * @param array                        $errors
187
	 *
188
	 * @return void
189
	 * @access private
190
	 */
191
	private function submit_data(\skouat\ppde\entity\currency $entity, array $errors)
192
	{
193
		if (!$entity->get_id())
194
		{
195
			$this->trigger_error_data_already_exists($entity);
196
		}
197
198
		if ($this->can_submit_data($errors))
199
		{
200
			$log_action = $entity->add_edit_data('set_order');
201
			// Log and show user confirmation of the saved item and provide link back to the previous page
202
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action), time(), array($entity->get_name()));
203
			trigger_error($this->language->lang($this->lang_key_prefix . '_' . strtoupper($log_action)) . adm_back_link($this->u_action));
204
		}
205
	}
206
207
	/**
208
	 * Edit a Currency
209
	 *
210
	 * @param int $currency_id Currency Identifier
211
	 *
212
	 * @return void
213
	 * @access public
214
	 */
215
	public function edit($currency_id)
216
	{
217
		// Add form key
218
		add_form_key('add_edit_currency');
219
220
221
		$this->ppde_entity->set_page_url($this->u_action);
222
		$this->ppde_entity->load($currency_id);
223
224
		// Collect the form data
225
		$data = array(
226
			'currency_id'       => $this->ppde_entity->get_id(),
227
			'currency_name'     => $this->request->variable('currency_name', $this->ppde_entity->get_name(), true),
228
			'currency_iso_code' => $this->request->variable('currency_iso_code', $this->ppde_entity->get_iso_code(), true),
229
			'currency_symbol'   => $this->request->variable('currency_symbol', $this->ppde_entity->get_symbol(), true),
230
			'currency_on_left'  => $this->request->variable('currency_on_left', $this->ppde_entity->get_currency_position()),
231
			'currency_enable'   => $this->request->variable('currency_enable', $this->ppde_entity->get_currency_enable()),
232
		);
233
234
		// Process the new page
235
		$this->add_edit_currency_data($this->ppde_entity, $data);
236
237
		// Set output vars for display in the template
238
		$this->add_edit_action_assign_template_vars('edit', $currency_id);
239
	}
240
241
	/**
242
	 * Move a currency up/down
243
	 *
244
	 * @param int    $currency_id The currency identifier to move
245
	 * @param string $direction   The direction (up|down)
246
	 *
247
	 * @return void
248
	 * @access   public
249
	 */
250
	public function move($currency_id, $direction)
251
	{
252
		// Before moving the currency, with check the link hash.
253
		// If the hash, is invalid we return an error.
254
		if (!check_link_hash($this->request->variable('hash', ''), 'ppde_move'))
255
		{
256
			trigger_error($this->language->lang('PPDE_DC_INVALID_HASH') . adm_back_link($this->u_action), E_USER_WARNING);
257
		}
258
259
		// Load data
260
		$this->ppde_entity->load($currency_id);
261
		$current_order = $this->ppde_entity->get_currency_order();
262
263
		if ($current_order == 0 && $direction == 'move_up')
264
		{
265
			return;
266
		}
267
268
		// on move_down, switch position with next order_id...
269
		// on move_up, switch position with previous order_id...
270
		$switch_order_id = ($direction == 'move_down') ? $current_order + 1 : $current_order - 1;
271
272
		$move_executed = $this->ppde_operator->move($switch_order_id, $current_order, $this->ppde_entity->get_id());
273
274
		// Log action if data was moved
275
		if ($move_executed)
276
		{
277
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($direction), time(), array($this->ppde_entity->get_name()));
278
		}
279
280
		if ($this->request->is_ajax())
281
		{
282
			$json_response = new \phpbb\json_response;
283
			$json_response->send(array(
284
				'success' => $move_executed,
285
			));
286
		}
287
	}
288
289
	/**
290
	 * Enable/disable a currency
291
	 *
292
	 * @param int    $currency_id
293
	 * @param string $action
294
	 *
295
	 * @return void
296
	 * @access public
297
	 */
298
	public function enable($currency_id, $action)
299
	{
300
		// Return an error if no currency
301
		if (!$currency_id)
302
		{
303
			trigger_error($this->language->lang($this->lang_key_prefix . '_NO_CURRENCY') . adm_back_link($this->u_action), E_USER_WARNING);
304
		}
305
306
		// Return an error if it's the default currency
307
		if ($this->config['ppde_default_currency'] == $currency_id && ($action == 'deactivate'))
308
		{
309
			trigger_error($this->language->lang('PPDE_CANNOT_DISABLE_DEFAULT_CURRENCY') . adm_back_link($this->u_action), E_USER_WARNING);
310
		}
311
312
		// Load data
313
		$this->ppde_entity->load($currency_id);
314
315
		// Set the new status for this currency
316
		$this->ppde_entity->set_currency_enable(($action == 'activate') ? true : false);
317
318
		// Save data to the database
319
		$this->ppde_entity->save($this->ppde_entity->check_required_field());
320
		// Log action
321
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($action) . 'D', time(), array($this->ppde_entity->get_name()));
322
323
		if ($this->request->is_ajax() && ($action == 'activate' || $action == 'deactivate'))
324
		{
325
			$action_lang = ($action == 'activate') ? 'DISABLE' : 'ENABLE';
326
			$json_response = new \phpbb\json_response;
327
			$json_response->send(array(
328
				'text' => $this->language->lang($action_lang),
329
			));
330
		}
331
	}
332
333
	/**
334
	 * Delete a currency
335
	 *
336
	 * @param int $currency_id
337
	 *
338
	 * @return void
339
	 * @access public
340
	 */
341
	public function delete($currency_id)
342
	{
343
		// Load data
344
		$this->ppde_entity->load($currency_id);
345
		$this->ppde_entity->delete($currency_id, 'check_currency_enable');
346
347
		// Log the action
348
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_DELETED', time(), array($this->ppde_entity->get_name()));
349
		trigger_error($this->language->lang($this->lang_key_prefix . '_DELETED') . adm_back_link($this->u_action));
350
	}
351
352
	/**
353
	 * Set output vars for display in the template
354
	 *
355
	 * @param array $data
356
	 *
357
	 * @return void
358
	 * @access protected
359
	 */
360
	protected function currency_assign_template_vars($data)
361
	{
362
		$enable_lang = (!$data['currency_enable']) ? 'ENABLE' : 'DISABLE';
363
		$enable_value = (!$data['currency_enable']) ? 'activate' : 'deactivate';
364
365
		$this->template->assign_block_vars('currency', array(
366
			'CURRENCY_NAME'    => $data['currency_name'],
367
			'CURRENCY_ENABLED' => (bool) $data['currency_enable'],
368
			'L_ENABLE_DISABLE' => $this->language->lang($enable_lang),
369
			'S_DEFAULT'        => (int) $data['currency_id'] === (int) $this->config['ppde_default_currency'],
370
			'U_DELETE'         => $this->u_action . '&amp;action=delete&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
371
			'U_EDIT'           => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
372
			'U_ENABLE_DISABLE' => $this->u_action . '&amp;action=' . $enable_value . '&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
373
			'U_MOVE_DOWN'      => $this->u_action . '&amp;action=move_down&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'] . '&amp;hash=' . generate_link_hash('ppde_move'),
374
			'U_MOVE_UP'        => $this->u_action . '&amp;action=move_up&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'] . '&amp;hash=' . generate_link_hash('ppde_move'),
375
		));
376
	}
377
}
378