Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 26 | class admin_settings_controller extends admin_main |
||
| 27 | { |
||
| 28 | protected $config; |
||
| 29 | protected $ppde_controller_main; |
||
| 30 | protected $ppde_operator_currency; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor |
||
| 34 | * |
||
| 35 | * @param \phpbb\config\config $config Config object |
||
| 36 | * @param ContainerInterface $container Service container interface |
||
| 37 | * @param \skouat\ppde\controller\main_controller $ppde_controller_main Main controller object |
||
| 38 | * @param \skouat\ppde\operators\currency $ppde_operator_currency Operator object |
||
| 39 | * @param \phpbb\request\request $request Request object |
||
| 40 | * @param \phpbb\template\template $template Template object |
||
| 41 | * @param \phpbb\user $user User object |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | */ |
||
| 45 | public function __construct(\phpbb\config\config $config, ContainerInterface $container, \skouat\ppde\controller\main_controller $ppde_controller_main, \skouat\ppde\operators\currency $ppde_operator_currency, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Display the general settings a user can configure for this extension |
||
| 63 | * |
||
| 64 | * @return null |
||
| 65 | * @access public |
||
| 66 | */ |
||
| 67 | public function display_settings() |
||
| 68 | { |
||
| 69 | $this->ppde_controller_main->first_start(); |
||
| 70 | |||
| 71 | // Define the name of the form for use as a form key |
||
| 72 | add_form_key('ppde_settings'); |
||
| 73 | |||
| 74 | // Create an array to collect errors that will be output to the user |
||
| 75 | $errors = array(); |
||
| 76 | |||
| 77 | $this->submit_settings(); |
||
| 78 | |||
| 79 | // Set output vars for display in the template |
||
| 80 | $this->s_error_assign_template_vars($errors); |
||
| 81 | $this->u_action_assign_template_vars(); |
||
| 82 | $this->template->assign_vars(array( |
||
| 83 | // Global Settings vars |
||
| 84 | 'PPDE_ACCOUNT_ID' => $this->check_config($this->config['ppde_account_id'], 'string', ''), |
||
| 85 | 'PPDE_DEFAULT_CURRENCY' => $this->container->get('skouat.ppde.controller')->build_currency_select_menu($this->config['ppde_default_currency']), |
||
| 86 | 'PPDE_DEFAULT_VALUE' => $this->check_config($this->config['ppde_default_value'], 'integer', 0), |
||
| 87 | 'PPDE_DROPBOX_VALUE' => $this->check_config($this->config['ppde_dropbox_value'], 'string', '1,2,3,4,5,10,20,25,50,100'), |
||
| 88 | 'S_PPDE_DROPBOX_ENABLE' => $this->check_config($this->config['ppde_dropbox_enable']), |
||
| 89 | 'S_PPDE_ENABLE' => $this->check_config($this->config['ppde_enable']), |
||
| 90 | 'S_PPDE_HEADER_LINK' => $this->check_config($this->config['ppde_header_link']), |
||
| 91 | |||
| 92 | // PayPal IPN vars |
||
| 93 | 'S_PPDE_IPN_AG_ENABLE' => $this->check_config($this->config['ppde_ipn_autogroup_enable']), |
||
| 94 | 'S_PPDE_IPN_AG_GROUP_AS_DEFAULT' => $this->check_config($this->config['ppde_ipn_group_as_default']), |
||
| 95 | 'S_PPDE_IPN_DL_ENABLE' => $this->check_config($this->config['ppde_ipn_donorlist_enable']), |
||
| 96 | 'S_PPDE_IPN_ENABLE' => $this->check_config($this->config['ppde_ipn_enable']), |
||
| 97 | 'S_PPDE_IPN_GROUP_OPTIONS' => group_select_options($this->config['ppde_ipn_group_id']), |
||
| 98 | 'S_PPDE_IPN_LOGGING' => $this->check_config($this->config['ppde_ipn_logging']), |
||
| 99 | 'S_PPDE_IPN_NOTIFICATION_ENABLE' => $this->check_config($this->config['ppde_ipn_notification_enable']), |
||
| 100 | |||
| 101 | // Sandbox Settings vars |
||
| 102 | 'PPDE_SANDBOX_ADDRESS' => $this->check_config($this->config['ppde_sandbox_address'], 'string', ''), |
||
| 103 | 'S_PPDE_SANDBOX_ENABLE' => $this->check_config($this->config['ppde_sandbox_enable']), |
||
| 104 | 'S_PPDE_SANDBOX_FOUNDER_ENABLE' => $this->check_config($this->config['ppde_sandbox_founder_enable'], 'boolean', true), |
||
| 105 | |||
| 106 | // Statistics Settings vars |
||
| 107 | 'PPDE_RAISED' => $this->check_config($this->config['ppde_raised'], 'float', 0), |
||
| 108 | 'PPDE_GOAL' => $this->check_config($this->config['ppde_goal'], 'float', 0), |
||
| 109 | 'PPDE_USED' => $this->check_config($this->config['ppde_used'], 'float', 0), |
||
| 110 | 'S_PPDE_STATS_INDEX_ENABLE' => $this->check_config($this->config['ppde_stats_index_enable']), |
||
| 111 | 'S_PPDE_RAISED_ENABLE' => $this->check_config($this->config['ppde_raised_enable']), |
||
| 112 | 'S_PPDE_GOAL_ENABLE' => $this->check_config($this->config['ppde_goal_enable']), |
||
| 113 | 'S_PPDE_USED_ENABLE' => $this->check_config($this->config['ppde_used_enable']), |
||
| 114 | )); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The form submitting if 'submit' is true |
||
| 119 | * |
||
| 120 | * @return null |
||
| 121 | * @access private |
||
| 122 | */ |
||
| 123 | private function submit_settings() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the options a user can configure |
||
| 147 | * |
||
| 148 | * @return null |
||
| 149 | * @access private |
||
| 150 | */ |
||
| 151 | private function set_settings() |
||
| 152 | { |
||
| 153 | // Set options for Global settings |
||
| 154 | $this->config->set('ppde_default_currency', $this->request->variable('ppde_default_currency', 0)); |
||
| 155 | $this->config->set('ppde_default_value', $this->request->variable('ppde_default_value', 0)); |
||
| 156 | $this->config->set('ppde_dropbox_enable', $this->request->variable('ppde_dropbox_enable', false)); |
||
| 157 | $this->config->set('ppde_dropbox_value', $this->clean_items_list($this->request->variable('ppde_dropbox_value', '1,2,3,4,5,10,20,25,50,100'))); |
||
| 158 | $this->config->set('ppde_enable', $this->request->variable('ppde_enable', false)); |
||
| 159 | $this->config->set('ppde_header_link', $this->request->variable('ppde_header_link', false)); |
||
| 160 | |||
| 161 | // Set options for PayPal IPN |
||
| 162 | $this->config->set('ppde_ipn_autogroup_enable', $this->request->variable('ppde_ipn_autogroup_enable', false)); |
||
| 163 | $this->config->set('ppde_ipn_donorlist_enable', $this->request->variable('ppde_ipn_donorlist_enable', false)); |
||
| 164 | $this->config->set('ppde_ipn_enable', $this->request->variable('ppde_ipn_enable', false)); |
||
| 165 | $this->config->set('ppde_ipn_group_as_default', $this->request->variable('ppde_ipn_group_as_default', false)); |
||
| 166 | $this->config->set('ppde_ipn_group_id', $this->request->variable('ppde_ipn_group_id', 0)); |
||
| 167 | $this->config->set('ppde_ipn_logging', $this->request->variable('ppde_ipn_logging', false)); |
||
| 168 | $this->config->set('ppde_ipn_notification_enable', $this->request->variable('ppde_ipn_notification_enable', false)); |
||
| 169 | |||
| 170 | // Set options for Sandbox Settings |
||
| 171 | $this->config->set('ppde_sandbox_enable', $this->request->variable('ppde_sandbox_enable', false)); |
||
| 172 | $this->config->set('ppde_sandbox_founder_enable', $this->request->variable('ppde_sandbox_founder_enable', true)); |
||
| 173 | |||
| 174 | // Set options for Statistics Settings |
||
| 175 | $this->config->set('ppde_stats_index_enable', $this->request->variable('ppde_stats_index_enable', false)); |
||
| 176 | $this->config->set('ppde_raised_enable', $this->request->variable('ppde_raised_enable', false)); |
||
| 177 | $this->config->set('ppde_raised', $this->request->variable('ppde_raised', 0.0)); |
||
| 178 | $this->config->set('ppde_goal_enable', $this->request->variable('ppde_goal_enable', false)); |
||
| 179 | $this->config->set('ppde_goal', $this->request->variable('ppde_goal', 0.0)); |
||
| 180 | $this->config->set('ppde_used_enable', $this->request->variable('ppde_used_enable', false)); |
||
| 181 | $this->config->set('ppde_used', $this->request->variable('ppde_used', 0.0)); |
||
| 182 | |||
| 183 | // Set misc settings |
||
| 184 | $this->ppde_controller_main->set_curl_info(); |
||
| 185 | $this->ppde_controller_main->set_remote_detected(); |
||
| 186 | |||
| 187 | // Settings with dependencies are the last to be set. |
||
| 188 | $this->config->set('ppde_account_id', $this->required_settings($this->request->variable('ppde_account_id', ''), $this->depend_on('ppde_enable'))); |
||
| 189 | $this->config->set('ppde_sandbox_address', $this->required_settings($this->request->variable('ppde_sandbox_address', ''), $this->depend_on('ppde_sandbox_enable'))); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Clean items list to conserve only numeric values |
||
| 194 | * |
||
| 195 | * @param string $config_value |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | * @access private |
||
| 199 | */ |
||
| 200 | private function clean_items_list($config_value) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Check if a config value is true |
||
| 221 | * |
||
| 222 | * @param mixed $config Config value |
||
| 223 | * @param string $type (see settype()) |
||
| 224 | * @param mixed $default |
||
| 225 | * |
||
| 226 | * @return mixed |
||
| 227 | * @access private |
||
| 228 | */ |
||
| 229 | private function check_config($config, $type = 'boolean', $default = '') |
||
| 230 | { |
||
| 231 | // We're using settype to enforce data types |
||
| 232 | settype($config, $type); |
||
| 233 | settype($default, $type); |
||
| 234 | |||
| 235 | return $config ? $config : $default; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Check if settings is required |
||
| 240 | * |
||
| 241 | * @param $settings |
||
| 242 | * @param $depend_on |
||
| 243 | * |
||
| 244 | * @return mixed |
||
| 245 | * @access private |
||
| 246 | */ |
||
| 247 | private function required_settings($settings, $depend_on) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Check if a settings depend on another. |
||
| 259 | * |
||
| 260 | * @param $config_name |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | * @access private |
||
| 264 | */ |
||
| 265 | private function depend_on($config_name) |
||
| 269 | } |
||
| 270 |