Total Complexity | 41 |
Total Lines | 326 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like admin_main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_main, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class admin_main |
||
16 | { |
||
17 | /** @var object \phpbb\config\config */ |
||
18 | protected $config; |
||
19 | /** @var object Symfony\Component\DependencyInjection\ContainerInterface */ |
||
20 | protected $container; |
||
21 | /** @var string */ |
||
22 | protected $id_prefix_name; |
||
23 | /** @var string */ |
||
24 | protected $lang_key_prefix; |
||
25 | /** @var \phpbb\language\language */ |
||
26 | protected $language; |
||
27 | /** @var \phpbb\log\log */ |
||
28 | protected $log; |
||
29 | /** @var string */ |
||
30 | protected $module_name; |
||
31 | /** @var bool */ |
||
32 | protected $preview; |
||
33 | /** @var \phpbb\request\request */ |
||
34 | protected $request; |
||
35 | /** @var bool */ |
||
36 | protected $submit; |
||
37 | /** @var \phpbb\template\template */ |
||
38 | protected $template; |
||
39 | /** @var string */ |
||
40 | protected $u_action; |
||
41 | /** @var \phpbb\user */ |
||
42 | protected $user; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param string $lang_key_prefix Prefix for the messages thrown by exceptions |
||
48 | * @param string $id_prefix_name Prefix name for identifier in the URL |
||
49 | * @param string $module_name Name of the module currently used |
||
50 | * |
||
51 | * @access public |
||
52 | */ |
||
53 | public function __construct($module_name, $lang_key_prefix, $id_prefix_name) |
||
54 | { |
||
55 | $this->module_name = $module_name; |
||
56 | $this->lang_key_prefix = $lang_key_prefix; |
||
57 | $this->id_prefix_name = $id_prefix_name; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Set page url |
||
62 | * |
||
63 | * @param string $u_action Custom form action |
||
64 | * |
||
65 | * @return void |
||
66 | * @access public |
||
67 | */ |
||
68 | public function set_page_url($u_action) |
||
69 | { |
||
70 | $this->u_action = $u_action; |
||
71 | } |
||
72 | |||
73 | public function display() |
||
74 | { |
||
75 | } |
||
76 | |||
77 | public function add() |
||
78 | { |
||
79 | } |
||
80 | |||
81 | public function edit($id) |
||
82 | { |
||
83 | } |
||
84 | |||
85 | public function delete($id) |
||
86 | { |
||
87 | } |
||
88 | |||
89 | public function enable($id, $action) |
||
91 | } |
||
92 | |||
93 | public function move($id, $action) |
||
94 | { |
||
95 | } |
||
96 | |||
97 | protected function set_settings() |
||
98 | { |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * The form submitting if 'submit' is true |
||
103 | * |
||
104 | * @return void |
||
105 | * @access protected |
||
106 | */ |
||
107 | protected function submit_settings() |
||
108 | { |
||
109 | $this->submit = $this->request->is_set_post('submit'); |
||
110 | |||
111 | // Test if the submitted form is valid |
||
112 | $errors = $this->is_invalid_form('ppde_' . $this->module_name, $this->submit); |
||
113 | |||
114 | if ($this->can_submit_data($errors)) |
||
115 | { |
||
116 | // Set the options the user configured |
||
117 | $this->set_settings(); |
||
118 | |||
119 | // Add option settings change action to the admin log |
||
120 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED'); |
||
121 | |||
122 | // Option settings have been updated and logged |
||
123 | // Confirm this to the user and provide link back to previous page |
||
124 | trigger_error($this->language->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action)); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Check if form is valid or not |
||
130 | * |
||
131 | * @param string $form_name |
||
132 | * @param bool $submit_or_preview |
||
133 | * |
||
134 | * @return array |
||
135 | * @access protected |
||
136 | */ |
||
137 | protected function is_invalid_form($form_name, $submit_or_preview = false) |
||
138 | { |
||
139 | return (!check_form_key($form_name) && $submit_or_preview) ? array($this->language->lang('FORM_INVALID')) : array(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param array $errors |
||
144 | * |
||
145 | * @return bool |
||
146 | * @access protected |
||
147 | */ |
||
148 | protected function can_submit_data(array $errors) |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Trigger error message if data already exists |
||
155 | * |
||
156 | * @param \skouat\ppde\entity\main $entity The entity object |
||
157 | * |
||
158 | * @access protected |
||
159 | */ |
||
160 | protected function trigger_error_data_already_exists(main $entity) |
||
161 | { |
||
162 | if ($this->is_added_data_exists($entity)) |
||
163 | { |
||
164 | // Show user warning for an already exist page and provide link back to the edit page |
||
165 | $message = $this->language->lang($this->lang_key_prefix . '_EXISTS'); |
||
166 | $message .= '<br><br>'; |
||
167 | $message .= $this->language->lang($this->lang_key_prefix . '_GO_TO_PAGE', '<a href="' . $this->u_action . '&action=edit&' . $this->id_prefix_name . '_id=' . $entity->get_id() . '">» ', '</a>'); |
||
168 | trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param \skouat\ppde\entity\main $entity The entity object |
||
174 | * |
||
175 | * @return bool |
||
176 | * @access protected |
||
177 | */ |
||
178 | protected function is_added_data_exists(main $entity) |
||
179 | { |
||
180 | return $entity->data_exists($entity->build_sql_data_exists()) && $this->request->variable('action', '') === 'add'; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Check some settings before submitting data |
||
185 | * |
||
186 | * @param \skouat\ppde\entity\main $entity The entity object |
||
187 | * @param string $field_name Name of the entity function to call |
||
188 | * @param string|int $value_cmp Default value to compare with the call_user_func() return value |
||
189 | * @param bool $submit_or_preview Form submit or preview status |
||
190 | * |
||
191 | * @return array $errors |
||
192 | * @access protected |
||
193 | */ |
||
194 | protected function is_empty_data(main $entity, $field_name, $value_cmp, $submit_or_preview = false) |
||
195 | { |
||
196 | $errors = array(); |
||
197 | |||
198 | if (call_user_func(array($entity, 'get_' . $field_name)) == $value_cmp && $submit_or_preview) |
||
199 | { |
||
200 | $errors[] = $this->language->lang($this->lang_key_prefix . '_EMPTY_' . strtoupper($field_name)); |
||
201 | } |
||
202 | |||
203 | return $errors; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get result of submit and preview expression |
||
208 | * |
||
209 | * @param bool $submit |
||
210 | * @param bool $preview |
||
211 | * |
||
212 | * @return bool |
||
213 | * @access protected |
||
214 | */ |
||
215 | protected function submit_or_preview($submit = false, $preview = false) |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Show user a result message if AJAX was used |
||
222 | * |
||
223 | * @param string $message Text message to show to the user |
||
224 | * |
||
225 | * @return void |
||
226 | * @access protected |
||
227 | */ |
||
228 | protected function ajax_delete_result_message($message = '') |
||
229 | { |
||
230 | if ($this->request->is_ajax()) |
||
231 | { |
||
232 | $json_response = new \phpbb\json_response; |
||
233 | $json_response->send(array( |
||
234 | 'MESSAGE_TITLE' => $this->language->lang('INFORMATION'), |
||
235 | 'MESSAGE_TEXT' => $message, |
||
236 | 'REFRESH_DATA' => array( |
||
237 | 'time' => 3, |
||
238 | ), |
||
239 | )); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Set u_action output vars for display in the template |
||
245 | * |
||
246 | * @return void |
||
247 | * @access protected |
||
248 | */ |
||
249 | protected function u_action_assign_template_vars() |
||
250 | { |
||
251 | $this->template->assign_vars(array( |
||
252 | 'U_ACTION' => $this->u_action, |
||
253 | )); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Set add/edit action output vars for display in the template |
||
258 | * |
||
259 | * @param string $type Action type: 'add' or 'edit' |
||
260 | * @param integer $id Identifier to Edit. If action = add, then let to '0'. |
||
261 | * |
||
262 | * @return void |
||
263 | * @access protected |
||
264 | */ |
||
265 | protected function add_edit_action_assign_template_vars($type, $id = 0) |
||
266 | { |
||
267 | $id_action = !empty($id) ? '&' . $this->id_prefix_name . '_id=' . (int) $id : ''; |
||
268 | |||
269 | $this->template->assign_vars(array( |
||
270 | 'S_ADD_EDIT' => true, |
||
271 | 'U_ACTION' => $this->u_action . '&action=' . $type . $id_action, |
||
272 | 'U_BACK' => $this->u_action, |
||
273 | )); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Set error output vars for display in the template |
||
278 | * |
||
279 | * @param array $errors |
||
280 | * |
||
281 | * @return void |
||
282 | * @access protected |
||
283 | */ |
||
284 | protected function s_error_assign_template_vars($errors) |
||
289 | )); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Check if a config value is true |
||
294 | * |
||
295 | * @param mixed $config Config value |
||
296 | * @param string $type (see settype()) |
||
297 | * @param mixed $default |
||
298 | * |
||
299 | * @return mixed |
||
300 | * @access protected |
||
301 | */ |
||
302 | protected function check_config($config, $type = 'boolean', $default = '') |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Check if settings is required |
||
313 | * |
||
314 | * @param $settings |
||
315 | * @param $depend_on |
||
316 | * |
||
317 | * @return mixed |
||
318 | * @access protected |
||
319 | */ |
||
320 | protected function required_settings($settings, $depend_on) |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Check if a settings depend on another. |
||
332 | * |
||
333 | * @param $config_name |
||
334 | * |
||
335 | * @return bool |
||
336 | * @access protected |
||
337 | */ |
||
338 | protected function depend_on($config_name) |
||
343 |