Completed
Push — master ( cb6ae8...5ae7ff )
by Harald
03:13
created

item::__construct()   D

Complexity

Conditions 11
Paths 144

Size

Total Lines 31
Code Lines 22

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 144
nop 0
dl 31
loc 31
rs 4.9629
c 0
b 0
f 0

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
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
  use OSC\OM\HTML;
10
  use OSC\OM\OSCOM;
11
  use OSC\OM\Registry;
12
13
  class item {
14
    var $code, $title, $description, $icon, $enabled;
15
16
// class constructor
17 View Code Duplication
    function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
18
      global $order;
19
20
      $OSCOM_Db = Registry::get('Db');
21
22
      $this->code = 'item';
23
      $this->title = OSCOM::getDef('module_shipping_item_text_title');
24
      $this->description = OSCOM::getDef('module_shipping_item_text_description');
25
      $this->sort_order = defined('MODULE_SHIPPING_ITEM_SORT_ORDER') ? (int)MODULE_SHIPPING_ITEM_SORT_ORDER : 0;
0 ignored issues
show
Bug introduced by
The property sort_order 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...
26
      $this->icon = '';
27
      $this->tax_class = defined('MODULE_SHIPPING_ITEM_TAX_CLASS') ? MODULE_SHIPPING_ITEM_TAX_CLASS : 0;
0 ignored issues
show
Bug introduced by
The property tax_class 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...
28
      $this->enabled = (defined('MODULE_SHIPPING_ITEM_STATUS') && (MODULE_SHIPPING_ITEM_STATUS == 'True') ? true : false);
29
30
      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_ITEM_ZONE > 0) ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
31
        $check_flag = false;
32
        $Qcheck = $OSCOM_Db->get('zones_to_geo_zones', 'zone_id', ['geo_zone_id' => MODULE_SHIPPING_ITEM_ZONE, 'zone_country_id' => $order->delivery['country']['id']], 'zone_id');
33
        while ($Qcheck->fetch()) {
34
          if ($Qcheck->valueInt('zone_id') < 1) {
35
            $check_flag = true;
36
            break;
37
          } elseif ($Qcheck->valueInt('zone_id') == $order->delivery['zone_id']) {
38
            $check_flag = true;
39
            break;
40
          }
41
        }
42
43
        if ($check_flag == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
44
          $this->enabled = false;
45
        }
46
      }
47
    }
48
49
// class methods
50
    function quote($method = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
      global $order;
52
53
      $number_of_items = $this->getNumberOfItems();
54
55
      $this->quotes = array('id' => $this->code,
0 ignored issues
show
Bug introduced by
The property quotes 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...
56
                            'module' => OSCOM::getDef('module_shipping_item_text_title'),
57
                            'methods' => array(array('id' => $this->code,
58
                                                     'title' => OSCOM::getDef('module_shipping_item_text_way'),
59
                                                     'cost' => (MODULE_SHIPPING_ITEM_COST * $number_of_items) + MODULE_SHIPPING_ITEM_HANDLING)));
60
61 View Code Duplication
      if ($this->tax_class > 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...
62
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
63
      }
64
65 View Code Duplication
      if (tep_not_null($this->icon)) $this->quotes['icon'] = HTML::image($this->icon, $this->title);
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...
66
67
      return $this->quotes;
68
    }
69
70
    function check() {
71
      return defined('MODULE_SHIPPING_ITEM_STATUS');
72
    }
73
74 View Code Duplication
    function install() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
      $OSCOM_Db = Registry::get('Db');
76
77
      $OSCOM_Db->save('configuration', [
78
        'configuration_title' => 'Enable Item Shipping',
79
        'configuration_key' => 'MODULE_SHIPPING_ITEM_STATUS',
80
        'configuration_value' => 'True',
81
        'configuration_description' => 'Do you want to offer per item rate shipping?',
82
        'configuration_group_id' => '6',
83
        'sort_order' => '1',
84
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
85
        'date_added' => 'now()'
86
      ]);
87
88
      $OSCOM_Db->save('configuration', [
89
        'configuration_title' => 'Shipping Cost',
90
        'configuration_key' => 'MODULE_SHIPPING_ITEM_COST',
91
        'configuration_value' => '2.50',
92
        'configuration_description' => 'The shipping cost will be multiplied by the number of items in an order that uses this shipping method.',
93
        'configuration_group_id' => '6',
94
        'sort_order' => '1',
95
        'date_added' => 'now()'
96
      ]);
97
98
      $OSCOM_Db->save('configuration', [
99
        'configuration_title' => 'Handling Fee',
100
        'configuration_key' => 'MODULE_SHIPPING_ITEM_HANDLING',
101
        'configuration_value' => '0',
102
        'configuration_description' => 'Handling fee for this shipping method.',
103
        'configuration_group_id' => '6',
104
        'sort_order' => '1',
105
        'date_added' => 'now()'
106
      ]);
107
108
      $OSCOM_Db->save('configuration', [
109
        'configuration_title' => 'Tax Class',
110
        'configuration_key' => 'MODULE_SHIPPING_ITEM_TAX_CLASS',
111
        'configuration_value' => '0',
112
        'configuration_description' => 'Use the following tax class on the shipping fee.',
113
        'configuration_group_id' => '6',
114
        'sort_order' => '1',
115
        'use_function' => 'tep_get_tax_class_title',
116
        'set_function' => 'tep_cfg_pull_down_tax_classes(',
117
        'date_added' => 'now()'
118
      ]);
119
120
      $OSCOM_Db->save('configuration', [
121
        'configuration_title' => 'Shipping Zone',
122
        'configuration_key' => 'MODULE_SHIPPING_ITEM_ZONE',
123
        'configuration_value' => '0',
124
        'configuration_description' => 'If a zone is selected, only enable this shipping method for that zone.',
125
        'configuration_group_id' => '6',
126
        'sort_order' => '1',
127
        'use_function' => 'tep_get_zone_class_title',
128
        'set_function' => 'tep_cfg_pull_down_zone_classes(',
129
        'date_added' => 'now()'
130
      ]);
131
132
      $OSCOM_Db->save('configuration', [
133
        'configuration_title' => 'Sort Order',
134
        'configuration_key' => 'MODULE_SHIPPING_ITEM_SORT_ORDER',
135
        'configuration_value' => '0',
136
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
137
        'configuration_group_id' => '6',
138
        'sort_order' => '0',
139
        'date_added' => 'now()'
140
      ]);
141
    }
142
143
    function remove() {
144
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
145
    }
146
147
    function keys() {
148
      return array('MODULE_SHIPPING_ITEM_STATUS', 'MODULE_SHIPPING_ITEM_COST', 'MODULE_SHIPPING_ITEM_HANDLING', 'MODULE_SHIPPING_ITEM_TAX_CLASS', 'MODULE_SHIPPING_ITEM_ZONE', 'MODULE_SHIPPING_ITEM_SORT_ORDER');
149
    }
150
151
    function getNumberOfItems() {
152
      global $order, $total_count;
153
154
      $OSCOM_Db = Registry::get('Db');
155
156
      $number_of_items = $total_count;
157
158
      if ($order->content_type == 'mixed') {
159
        $number_of_items = 0;
160
161
        for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
162
          $number_of_items += $order->products[$i]['qty'];
163
164
          if (isset($order->products[$i]['attributes'])) {
165
            foreach ( $order->products[$i]['attributes'] as $option => $value ) {
166
              $Qcheck = $OSCOM_Db->prepare('select pa.products_id from :table_products_attributes pa, :table_products_attributes_download pad where pa.products_id = :products_id and pa.options_values_id = :options_values_id and pa.products_attributes_id = pad.products_attributes_id');
167
              $Qcheck->bindInt(':products_id', $order->products[$i]['id']);
168
              $Qcheck->bindInt(':options_values_id', $value['value_id']);
169
              $Qcheck->execute();
170
171
              if ($Qcheck->fetch() !== false) {
172
                $number_of_items -= $order->products[$i]['qty'];
173
              }
174
            }
175
          }
176
        }
177
      }
178
179
      return $number_of_items;
180
    }
181
  }
182
?>
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...
183