| Total Complexity | 55 |
| Total Lines | 404 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WcPagantis 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 WcPagantis, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WcPagantis |
||
| 18 | { |
||
| 19 | const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce'; |
||
| 20 | const PAGANTIS_DOC_URL = 'https://developer.pagantis.com'; |
||
| 21 | const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
||
| 22 | |||
| 23 | /** Concurrency tablename */ |
||
| 24 | const LOGS_TABLE = 'pagantis_logs'; |
||
| 25 | |||
| 26 | /** Config tablename */ |
||
| 27 | const CONFIG_TABLE = 'pagantis_config'; |
||
| 28 | |||
| 29 | /** Concurrency tablename */ |
||
| 30 | const CONCURRENCY_TABLE = 'pagantis_concurrency'; |
||
| 31 | |||
| 32 | public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing', |
||
| 33 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE', |
||
| 34 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE', |
||
| 35 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
| 36 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
||
| 37 | 'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
| 38 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
| 39 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER', |
||
| 40 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'a:3:{i:0;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:1;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:2;s:36:"*:not(del)>.woocommerce-Price-amount";}', |
||
| 41 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'a:2:{i:0;s:22:"div.quantity input.qty";i:1;s:18:"div.quantity>input";}', |
||
| 42 | 'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
||
| 43 | 'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
||
| 44 | 'PAGANTIS_URL_OK'=>'', |
||
| 45 | 'PAGANTIS_URL_KO'=>'', |
||
| 46 | 'PAGANTIS_ALLOWED_COUNTRIES' => 'a:2:{i:0;s:2:"es";i:1;s:2:"it";}' |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** @var Array $extraConfig */ |
||
| 50 | public $extraConfig; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * WC_Pagantis constructor. |
||
| 54 | */ |
||
| 55 | public function __construct() |
||
| 56 | { |
||
| 57 | require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
||
|
|
|||
| 58 | |||
| 59 | $this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
||
| 60 | |||
| 61 | $this->pagantisActivation(); |
||
| 62 | |||
| 63 | $this->extraConfig = $this->getExtraConfig(); |
||
| 64 | |||
| 65 | load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages'); |
||
| 66 | add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway')); |
||
| 67 | add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999); |
||
| 68 | add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2); |
||
| 69 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks')); |
||
| 70 | add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
||
| 71 | add_action('wp_enqueue_scripts', 'add_pagantis_widget_js'); |
||
| 72 | add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint |
||
| 73 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
| 74 | } |
||
| 75 | |||
| 76 | /* |
||
| 77 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
| 78 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
| 79 | * File to be placed, for example, wp-content/lanaguages/textdomain/yourtranslationfile-en_GB.mo |
||
| 80 | */ |
||
| 81 | public function loadPagantisTranslation($mofile, $domain) |
||
| 82 | { |
||
| 83 | if ('pagantis' === $domain) { |
||
| 84 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
| 85 | } |
||
| 86 | return $mofile; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Sql table |
||
| 91 | */ |
||
| 92 | public function pagantisActivation() |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Product simulator |
||
| 153 | */ |
||
| 154 | public function pagantisAddProductSimulator() |
||
| 155 | { |
||
| 156 | global $product; |
||
| 157 | |||
| 158 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 159 | $locale = strtolower(strstr(get_locale(), '_', true)); |
||
| 160 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
| 161 | $allowedCountry = (in_array(strtolower($locale), $allowedCountries)); |
||
| 162 | if ($cfg['enabled'] !== 'yes' || $cfg['pagantis_public_key'] == '' || $cfg['pagantis_private_key'] == '' || |
||
| 163 | $cfg['simulator'] !== 'yes' || $product->price < $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'] || |
||
| 164 | !$allowedCountry ) { |
||
| 165 | return; |
||
| 166 | } |
||
| 167 | |||
| 168 | $css_quantity_selector = $this->prepareQuantitySelector(); |
||
| 169 | $css_price_selector = $this->preparePriceSelector(); |
||
| 170 | $template_fields = array( |
||
| 171 | 'total' => is_numeric($product->price) ? $product->price : 0, |
||
| 172 | 'public_key' => $cfg['pagantis_public_key'], |
||
| 173 | 'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'], |
||
| 174 | 'positionSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'], |
||
| 175 | 'quantitySelector' => unserialize($css_quantity_selector), |
||
| 176 | 'priceSelector' => unserialize($css_price_selector), |
||
| 177 | 'totalAmount' => is_numeric($product->price) ? $product->price : 0, |
||
| 178 | 'locale' => $locale |
||
| 179 | ); |
||
| 180 | wc_get_template('product_simulator.php', $template_fields, '', $this->template_path); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Add Pagantis to payments list. |
||
| 185 | * |
||
| 186 | * @param $methods |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function addPagantisGateway($methods) |
||
| 191 | { |
||
| 192 | if (! class_exists('WC_Payment_Gateway')) { |
||
| 193 | return $methods; |
||
| 194 | } |
||
| 195 | |||
| 196 | include_once('controllers/paymentController.php'); |
||
| 197 | $methods[] = 'WcPagantisGateway'; |
||
| 198 | |||
| 199 | return $methods; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Initialize WC_Pagantis class |
||
| 204 | * |
||
| 205 | * @param $methods |
||
| 206 | * |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | public function pagantisFilterGateways($methods) |
||
| 210 | { |
||
| 211 | $pagantis = new WcPagantisGateway(); |
||
| 212 | if (!$pagantis->is_available()) { |
||
| 213 | unset($methods['pagantis']); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $methods; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add links to Plugin description |
||
| 221 | * |
||
| 222 | * @param $links |
||
| 223 | * |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | public function pagantisActionLinks($links) |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Add links to Plugin options |
||
| 239 | * |
||
| 240 | * @param $links |
||
| 241 | * @param $file |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function pagantisRowMeta($links, $file) |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Read logs |
||
| 261 | */ |
||
| 262 | public function readLogs($data) |
||
| 263 | { |
||
| 264 | global $wpdb; |
||
| 265 | $filters = ($data->get_params()); |
||
| 266 | $response = array(); |
||
| 267 | $secretKey = $filters['secret']; |
||
| 268 | $from = $filters['from']; |
||
| 269 | $to = $filters['to']; |
||
| 270 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 271 | $privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null; |
||
| 272 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
| 273 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
| 274 | $results = $wpdb->get_results($query); |
||
| 275 | if (isset($results) && $privateKey == $secretKey) { |
||
| 276 | foreach ($results as $key => $result) { |
||
| 277 | $response[$key]['timestamp'] = $result->createdAt; |
||
| 278 | $response[$key]['log'] = json_decode($result->log); |
||
| 279 | } |
||
| 280 | } else { |
||
| 281 | $response['result'] = 'Error'; |
||
| 282 | } |
||
| 283 | $response = json_encode($response); |
||
| 284 | header("HTTP/1.1 200", true, 200); |
||
| 285 | header('Content-Type: application/json', true); |
||
| 286 | header('Content-Length: '.strlen($response)); |
||
| 287 | echo($response); |
||
| 288 | exit(); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Update extra config |
||
| 293 | */ |
||
| 294 | public function updateExtraConfig($data) |
||
| 295 | { |
||
| 296 | global $wpdb; |
||
| 297 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
| 298 | $response = array('status'=>null); |
||
| 299 | |||
| 300 | $filters = ($data->get_params()); |
||
| 301 | $secretKey = $filters['secret']; |
||
| 302 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 303 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
| 304 | if ($privateKey != $secretKey) { |
||
| 305 | $response['status'] = 401; |
||
| 306 | $response['result'] = 'Unauthorized'; |
||
| 307 | } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 308 | if (count($_POST)) { |
||
| 309 | foreach ($_POST as $config => $value) { |
||
| 310 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
| 311 | $wpdb->update( |
||
| 312 | $tableName, |
||
| 313 | array('value' => $value), |
||
| 314 | array('config' => $config), |
||
| 315 | array('%s'), |
||
| 316 | array('%s') |
||
| 317 | ); |
||
| 318 | } else { |
||
| 319 | $response['status'] = 400; |
||
| 320 | $response['result'] = 'Bad request'; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } else { |
||
| 324 | $response['status'] = 422; |
||
| 325 | $response['result'] = 'Empty data'; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | if ($response['status']==null) { |
||
| 330 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
| 331 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
| 332 | foreach ($dbResult as $value) { |
||
| 333 | $formattedResult[$value['config']] = $value['value']; |
||
| 334 | } |
||
| 335 | $response['result'] = $formattedResult; |
||
| 336 | } |
||
| 337 | |||
| 338 | $result = json_encode($response['result']); |
||
| 339 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
| 340 | header('Content-Type: application/json', true); |
||
| 341 | header('Content-Length: '.strlen($result)); |
||
| 342 | echo($result); |
||
| 343 | exit(); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | public function pagantisRegisterEndpoint() |
||
| 351 | { |
||
| 352 | register_rest_route( |
||
| 353 | 'pagantis/v1', |
||
| 354 | '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', |
||
| 355 | array( |
||
| 356 | 'methods' => 'GET', |
||
| 357 | 'callback' => array( |
||
| 358 | $this, |
||
| 359 | 'readLogs') |
||
| 360 | ), |
||
| 361 | true |
||
| 362 | ); |
||
| 363 | |||
| 364 | register_rest_route( |
||
| 365 | 'pagantis/v1', |
||
| 366 | '/configController/(?P<secret>\w+)', |
||
| 367 | array( |
||
| 368 | 'methods' => 'GET, POST', |
||
| 369 | 'callback' => array( |
||
| 370 | $this, |
||
| 371 | 'updateExtraConfig') |
||
| 372 | ), |
||
| 373 | true |
||
| 374 | ); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | private function getExtraConfig() |
||
| 381 | { |
||
| 382 | global $wpdb; |
||
| 383 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
| 384 | $response = array(); |
||
| 385 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
| 386 | foreach ($dbResult as $value) { |
||
| 387 | $response[$value['config']] = $value['value']; |
||
| 388 | } |
||
| 389 | |||
| 390 | return $response; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return mixed|string |
||
| 395 | */ |
||
| 396 | private function prepareQuantitySelector() |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return mixed|string |
||
| 410 | */ |
||
| 411 | private function preparePriceSelector() |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Add widget Js |
||
| 426 | **/ |
||
| 434 |