shipping::get_first()   C
last analyzed

Complexity

Conditions 9
Paths 15

Size

Total Lines 23
Code Lines 14

Duplication

Lines 7
Ratio 30.43 %

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 15
nop 0
dl 7
loc 23
rs 5.8541
c 0
b 0
f 0
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\Apps;
10
  use OSC\OM\Registry;
11
12
  class shipping {
13
    var $modules;
14
15
    protected $lang;
16
17
// class constructor
18
    function __construct($module = '') {
19
      global $PHP_SELF;
20
21
      $this->lang = Registry::get('Language');
22
23
      if (defined('MODULE_SHIPPING_INSTALLED') && tep_not_null(MODULE_SHIPPING_INSTALLED)) {
24
        $this->modules = explode(';', MODULE_SHIPPING_INSTALLED);
25
26
        $include_modules = array();
27
28
        $code = null;
29
30
        if (isset($module) && is_array($module) && isset($module['id'])) {
31
          if (strpos($module['id'], '\\') !== false) {
32
            list($vendor, $app, $module) = explode('\\', $module['id']);
33
            list($module, $method) = explode('_', $module);
34
35
            $code = $vendor . '\\' . $app . '\\' . $module;
36
          } elseif (strpos($module['id'], '_') !== false) {
37
            $code = substr($module['id'], 0, strpos($module['id'], '_'));
38
          }
39
        }
40
41
        if (isset($code) && (in_array($code . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules) || in_array($code, $this->modules))) {
42
          if (strpos($code, '\\') !== false) {
43
            $class = Apps::getModuleClass($code, 'Shipping');
44
45
            $include_modules[] = [
46
              'class' => $code,
47
              'file' => $class
48
            ];
49
          } else {
50
            $include_modules[] = [
51
                'class' => $code,
52
                'file' => $code . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1))
53
            ];
54
          }
55 View Code Duplication
        } else {
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...
56
          foreach ($this->modules as $value) {
57
            if (strpos($value, '\\') !== false) {
58
              $class = Apps::getModuleClass($value, 'Shipping');
59
60
              $include_modules[] = [
61
                'class' => $value,
62
                'file' => $class
63
              ];
64
            } else {
65
              $class = substr($value, 0, strrpos($value, '.'));
66
67
              $include_modules[] = [
68
                'class' => $class,
69
                'file' => $value
70
              ];
71
            }
72
          }
73
        }
74
75 View Code Duplication
        for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
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...
76
          if (strpos($include_modules[$i]['class'], '\\') !== false) {
77
            Registry::set('Shipping_' . str_replace('\\', '_', $include_modules[$i]['class']), new $include_modules[$i]['file']);
78
          } else {
79
            $this->lang->loadDefinitions('modules/shipping/' . pathinfo($include_modules[$i]['file'], PATHINFO_FILENAME));
80
            include('includes/modules/shipping/' . $include_modules[$i]['file']);
81
82
            $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
83
          }
84
        }
85
      }
86
    }
87
88
    function quote($method = '', $module = '') {
89
      global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;
90
91
      $quotes_array = array();
92
93
      if (is_array($this->modules)) {
94
        $shipping_quoted = '';
95
        $shipping_num_boxes = 1;
96
        $shipping_weight = $total_weight;
97
98
        if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100) {
99
          $shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT;
100
        } else {
101
          $shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100);
102
        }
103
104
        if ($shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes
105
          $shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT);
106
          $shipping_weight = $shipping_weight/$shipping_num_boxes;
107
        }
108
109
        $include_quotes = array();
110
111
        foreach($this->modules as $value) {
112
          if (strpos($value, '\\') !== false) {
113
            $obj = Registry::get('Shipping_' . str_replace('\\', '_', $value));
114
115 View Code Duplication
            if (tep_not_null($module)) {
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...
116
              if ( ($module == $value) && ($obj->enabled) ) {
117
                $include_quotes[] = $value;
118
              }
119
            } elseif ($obj->enabled) {
120
              $include_quotes[] = $value;
121
            }
122
          } else {
123
            $class = substr($value, 0, strrpos($value, '.'));
124
125 View Code Duplication
            if (tep_not_null($module)) {
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...
126
              if ( ($module == $class) && ($GLOBALS[$class]->enabled) ) {
127
                $include_quotes[] = $class;
128
              }
129
            } elseif ($GLOBALS[$class]->enabled) {
130
              $include_quotes[] = $class;
131
            }
132
          }
133
        }
134
135
        $size = sizeof($include_quotes);
136
        for ($i=0; $i<$size; $i++) {
137
          if (strpos($include_quotes[$i], '\\') !== false) {
138
            $quotes = Registry::get('Shipping_' . str_replace('\\', '_', $include_quotes[$i]))->quote($method);
139
          } else {
140
            $quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
141
          }
142
143
          if (is_array($quotes)) {
144
            $quotes_array[] = $quotes;
145
          }
146
        }
147
      }
148
149
      return $quotes_array;
150
    }
151
152
    function get_first() {
153
      foreach ( $this->modules as $value ) {
154 View Code Duplication
        if (strpos($value, '\\') !== false) {
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...
155
          $obj = Registry::get('Shipping_' . str_replace('\\', '_', $value));
156
        } else {
157
          $class = substr($value, 0, strrpos($value, '.'));
158
159
          $obj = $GLOBALS[$class];
160
        }
161
162
        if ( $obj->enabled ) {
163
          foreach ( $obj->quotes['methods'] as $method ) {
164
            if ( isset($method['cost']) && tep_not_null($method['cost']) ) {
165
              return [
166
                'id' => $obj->quotes['id'] . '_' . $method['id'],
167
                'title' => $obj->quotes['module'] . (isset($method['title']) && !empty($method['title']) ? ' (' . $method['title'] . ')' : ''),
168
                'cost' => $method['cost']
169
              ];
170
            }
171
          }
172
        }
173
      }
174
    }
175
176
    function cheapest() {
177
      if (is_array($this->modules)) {
178
        $rates = array();
179
180
        foreach($this->modules as $value) {
181 View Code Duplication
          if (strpos($value, '\\') !== false) {
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...
182
            $obj = Registry::get('Shipping_' . str_replace('\\', '_', $value));
183
          } else {
184
            $class = substr($value, 0, strrpos($value, '.'));
185
186
            $obj = $GLOBALS[$class];
187
          }
188
189
          if ($obj->enabled) {
190
            $quotes = $obj->quotes;
191
192
            for ($i=0, $n=sizeof($quotes['methods']); $i<$n; $i++) {
193
              if (isset($quotes['methods'][$i]['cost']) && tep_not_null($quotes['methods'][$i]['cost'])) {
194
                $rates[] = [
195
                  'id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
196
                  'title' => $quotes['module'] . (isset($quotes['methods'][$i]['title']) && !empty($quotes['methods'][$i]['title']) ? ' (' . $quotes['methods'][$i]['title'] . ')' : ''),
197
                  'cost' => $quotes['methods'][$i]['cost']
198
                ];
199
              }
200
            }
201
          }
202
        }
203
204
        $cheapest = false;
205
206
        for ($i=0, $n=sizeof($rates); $i<$n; $i++) {
207
          if (is_array($cheapest)) {
208
            if ($rates[$i]['cost'] < $cheapest['cost']) {
209
              $cheapest = $rates[$i];
210
            }
211
          } else {
212
            $cheapest = $rates[$i];
213
          }
214
        }
215
216
        return $cheapest;
217
      }
218
    }
219
  }
220
?>
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...
221