Completed
Pull Request — 23 (#431)
by Harald
10:27
created

cm_account_braintree_cards()   C

Complexity

Conditions 13
Paths 130

Size

Total Lines 51
Code Lines 29

Duplication

Lines 4
Ratio 7.84 %

Importance

Changes 0
Metric Value
cc 13
eloc 29
nc 130
nop 0
dl 4
loc 51
rs 5.2291
c 0
b 0
f 0

How to fix   Long Method    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
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2014 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  if ( !class_exists('OSCOM_Braintree') ) {
14
    include(DIR_FS_CATALOG . 'includes/apps/braintree/OSCOM_Braintree.php');
15
  }
16
17
  class cm_account_braintree_cards {
18
    var $code;
19
    var $group;
20
    var $title;
21
    var $description;
22
    var $sort_order;
23
    var $enabled = false;
24
    var $_app;
25
26
    function cm_account_braintree_cards() {
27
      global $language;
28
29
      $this->_app = new OSCOM_Braintree();
30
      $this->_app->loadLanguageFile('shop/account_cards_page.php');
31
32
      $this->code = get_class($this);
33
      $this->group = basename(dirname(__FILE__));
34
35
      $this->title = $this->_app->getDef('account_braintree_cards_title');
36
      $this->description = $this->_app->getDef('account_braintree_cards_description') . '<div align="center">' . $this->_app->drawButton($this->_app->getDef('accouint_braintree_cards_legacy_admin_app_button'), tep_href_link('braintree.php', 'action=configure&module=CC'), 'primary', null, true) . '</div>';
37
38
      if ( defined('MODULE_CONTENT_ACCOUNT_BRAINTREE_CARDS_SORT_ORDER') ) {
39
        $this->sort_order = MODULE_CONTENT_ACCOUNT_BRAINTREE_CARDS_SORT_ORDER;
40
        $this->enabled = defined('OSCOM_APP_PAYPAL_BRAINTREE_CC_STATUS') && in_array(OSCOM_APP_PAYPAL_BRAINTREE_CC_STATUS, array('1', '0')) ? true : false;
41
      }
42
43
      $this->public_title = $this->_app->getDef('account_braintree_cards_link_title');
0 ignored issues
show
Bug introduced by
The property public_title does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
45
      $braintree_enabled = false;
46
47
      if ( defined('MODULE_PAYMENT_INSTALLED') && tep_not_null(MODULE_PAYMENT_INSTALLED) && in_array('braintree_cc.php', explode(';', MODULE_PAYMENT_INSTALLED)) ) {
48
        if ( !class_exists('braintree_cc') ) {
49
          include(DIR_FS_CATALOG . 'includes/languages/' . $language . '/modules/payment/braintree_cc.php');
50
          include(DIR_FS_CATALOG . 'includes/modules/payment/braintree_cc.php');
51
        }
52
53
        $braintree_cc = new braintree_cc();
54
55
        if ( $braintree_cc->enabled ) {
56
          if ( defined('OSCOM_APP_PAYPAL_BRAINTREE_CC_STATUS') ) {
57
            $braintree_enabled = true;
58
59 View Code Duplication
            if ( OSCOM_APP_PAYPAL_BRAINTREE_CC_STATUS == '0' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
              $this->title .= ' [Sandbox]';
61
              $this->public_title .= ' (' . $braintree_cc->code . '; Sandbox)';
62
            }
63
64
            if (OSCOM_APP_PAYPAL_BRAINTREE_CC_CC_TOKENS == '0') {
65
              $braintree_enabled = false;
66
            }
67
          }
68
        }
69
      }
70
71
      if ( $braintree_enabled !== true ) {
72
        $this->enabled = false;
73
74
        $this->description = '<div class="secWarning">' . $this->_app->getDef('account_braintree_cards_error_main_module') . '</div>' . $this->description;
75
      }
76
    }
77
78
    function execute() {
79
      global $oscTemplate;
80
81
      $oscTemplate->_data['account']['account']['links']['braintree_cards'] = array('title' => $this->public_title,
82
                                                                                    'link' => tep_href_link('ext/modules/content/account/braintree/cards.php', '', 'SSL'),
83
                                                                                    'icon' => 'newwin');
84
    }
85
86
    function isEnabled() {
87
      return $this->enabled;
88
    }
89
90
    function check() {
91
      $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'OSCOM_APP_PAYPAL_BRAINTREE_CC_STATUS'");
92
      if ( tep_db_num_rows($check_query) ) {
93
        $check = tep_db_fetch_array($check_query);
94
95
        return tep_not_null($check['configuration_value']);
96
      }
97
98
      return false;
99
    }
100
101
    function install() {
102
      tep_redirect(tep_href_link('braintree.php', 'action=configure'));
103
    }
104
105
    function remove() {
106
      tep_redirect(tep_href_link('braintree.php', 'action=configure'));
107
    }
108
109
    function keys() {
110
      return array('MODULE_CONTENT_ACCOUNT_BRAINTREE_CARDS_SORT_ORDER');
111
    }
112
  }
113
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
114