Completed
Branch develop-3.3.x-refactor_modules (cfd1e1)
by Mario
03:51
created

ppde_module::do_action()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 27
c 0
b 0
f 0
nc 13
nop 2
dl 0
loc 48
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\acp;
12
13
class ppde_module
14
{
15
	/** @var array */
16
	private static $available_mode = [
17
		['module_name' => 'currency', 'lang_key_prefix' => 'PPDE_DC_', 'id_prefix_name' => 'currency'],
18
		['module_name' => 'donation_pages', 'lang_key_prefix' => 'PPDE_DP_', 'id_prefix_name' => 'page'],
19
		['module_name' => 'overview'],
20
		['module_name' => 'paypal_features'],
21
		['module_name' => 'settings'],
22
		['module_name' => 'transactions', 'lang_key_prefix' => 'PPDE_DT_'],
23
	];
24
	/** @var string */
25
	public $u_action;
26
	/** @var string */
27
	public $page_title;
28
	/** @var string */
29
	public $tpl_name;
30
	/** @var array */
31
	private $module_info;
32
33
	/**
34
	 * @param string $id
35
	 * @param string $mode
36
	 *
37
	 * @return void
38
	 * @access public
39
	 * @throws \Exception
40
	 */
41
	public function main($id, $mode)
42
	{
43
		global $phpbb_container;
44
45
		/** @type \phpbb\language\language $language Language object */
46
		$language = $phpbb_container->get('language');
47
48
		if ($this->in_array_field($mode, 'module_name', $this::$available_mode))
49
		{
50
			$this->module_info = $this->array_value($mode, 'module_name', $this::$available_mode);
51
52
			// Load the module language file currently in use
53
			$language->add_lang('acp_' . $mode, 'skouat/ppde');
54
55
			// Get an instance of the admin controller
56
			/** @type \skouat\ppde\controller\admin\admin_main $admin_controller */
57
			$admin_controller = $phpbb_container->get('skouat.ppde.controller.admin.' . $mode);
58
59
			// Make the $u_action url available in the admin controller
60
			$admin_controller->set_page_url($this->u_action);
61
62
			// Set the page title for our ACP page
63
			$this->page_title = 'PPDE_ACP_' . strtoupper($mode);
64
65
			// Load a template from adm/style for our ACP page
66
			$this->tpl_name = 'ppde_' . strtolower($mode);
67
68
			$this->switch_mode($id, $mode, $admin_controller);
69
		}
70
		else
71
		{
72
			trigger_error('NO_MODE', E_USER_ERROR);
73
		}
74
	}
75
76
	/**
77
	 * Check if value is in array
78
	 *
79
	 * @param mixed $needle
80
	 * @param mixed $needle_field
81
	 * @param array $haystack
82
	 *
83
	 * @return bool
84
	 * @access private
85
	 */
86
	private function in_array_field($needle, $needle_field, $haystack)
87
	{
88
		foreach ($haystack as $item)
89
		{
90
			if (isset($item[$needle_field]) && $item[$needle_field] === $needle)
91
			{
92
				return true;
93
			}
94
		}
95
		unset($item);
96
97
		return false;
98
	}
99
100
	/**
101
	 * Return the selected array if value is in array
102
	 *
103
	 * @param mixed $needle
104
	 * @param mixed $needle_field
105
	 * @param array $haystack
106
	 *
107
	 * @return array
108
	 * @access private
109
	 */
110
	private function array_value($needle, $needle_field, $haystack)
111
	{
112
		foreach ($haystack as $item)
113
		{
114
			if (isset($item[$needle_field]) && $item[$needle_field] === $needle)
115
			{
116
				return $item;
117
			}
118
		}
119
		unset($item);
120
121
		return [];
122
	}
123
124
	/**
125
	 * Switch to the mode selected
126
	 *
127
	 * @param int                                      $id
128
	 * @param string                                   $mode
129
	 * @param \skouat\ppde\controller\admin\admin_main $admin_controller
130
	 *
131
	 * @return void
132
	 * @access private
133
	 * @throws \Exception
134
	 */
135
	private function switch_mode($id, $mode, $admin_controller)
136
	{
137
		global $phpbb_container;
138
139
		/** @type \phpbb\request\request $request Request object */
140
		$request = $phpbb_container->get('request');
141
142
		// Requests vars
143
		$action = $request->variable('action', '');
144
145
		switch ($mode)
146
		{
147
			case 'currency':
148
			case 'donation_pages':
149
				// Get an instance of the entity
150
				$entity = $phpbb_container->get('skouat.ppde.entity.' . $mode);
151
152
				// Make the $u_action url available in entity
153
				$entity->set_page_url($this->u_action);
154
			// no break;
155
			case 'transactions':
156
				// Request the ID
157
				$admin_controller->set_item_id($request->variable($this->module_info['id_prefix_name'] . '_id', 0));
158
159
				// Send ids to the controller
160
				$admin_controller->set_hidden_fields($id, $mode, $action);
161
162
				$this->do_action($admin_controller->get_action(), $admin_controller);
163
			break;
164
			case 'paypal_features':
165
			case 'settings':
166
				// Load the display handle in the admin controller
167
				/** @type \skouat\ppde\controller\admin\settings_controller|\skouat\ppde\controller\admin\paypal_features_controller $admin_controller */
168
				$admin_controller->display_settings();
169
			break;
170
			case 'overview':
171
				// Load the display overview handle in the admin controller
172
				/** @type \skouat\ppde\controller\admin\overview_controller $admin_controller */
173
				$admin_controller->display_overview($action);
174
			break;
175
		}
176
	}
177
178
	/**
179
	 * Performs action requested by the module
180
	 *
181
	 * @param string                                   $action
182
	 * @param \skouat\ppde\controller\admin\admin_main $controller
183
	 *
184
	 * @return void
185
	 * @throws \Exception
186
	 * @access private
187
	 */
188
	private function do_action($action, $controller)
189
	{
190
		global $phpbb_container;
191
192
		/** @type \phpbb\language\language $language Language object */
193
		$language = $phpbb_container->get('language');
194
195
		switch ($action)
196
		{
197
			case 'add':
198
			case 'change':
199
			case 'edit':
200
			case 'view':
201
				// Set the page title for our ACP page
202
				$this->page_title = $this->module_info['lang_key_prefix'] . 'CONFIG';
203
204
				// Call the method in the admin controller based on the $action value
205
				$controller->$action();
206
207
				// Return to stop execution of this script
208
				return;
209
			case 'move_down':
210
			case 'move_up':
211
				$controller->move();
212
			break;
213
			case 'activate':
214
			case 'deactivate':
215
				$controller->enable();
216
			break;
217
			case 'approve':
218
			case 'delete':
219
				// Use a confirm box routine when approving/deleting an item
220
				if (confirm_box(true))
221
				{
222
					$controller->$action();
223
					break;
224
				}
225
226
				// Request confirmation from the user to do the action for selected item
227
				confirm_box(false, $language->lang($this->module_info['lang_key_prefix'] . 'CONFIRM_OPERATION'), build_hidden_fields($controller->get_hidden_fields()));
228
229
				// Clear $action status
230
				$controller->set_action($action);
231
			break;
232
		}
233
234
		// Load the display handle in the admin controller
235
		$controller->display();
236
	}
237
}
238