Completed
Push — master ( d4dae7...701b1d )
by Harald
08:45 queued 04:20
created

cm_footer_banner   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 4.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 6
loc 130
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A execute() 0 15 3
A isEnabled() 0 3 1
A check() 0 3 1
A install() 0 47 1
A remove() 0 3 1
A keys() 0 3 1
A showBannerGroupSelection() 0 3 2
A getBannerGroupSelection() 6 23 3

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2016 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  use OSC\OM\HTML;
14
  use OSC\OM\Registry;
15
16
  class cm_footer_banner {
17
    var $code;
18
    var $group;
19
    var $title;
20
    var $description;
21
    var $sort_order;
22
    var $enabled = false;
23
24
    function __construct() {
25
      $this->code = get_class($this);
26
      $this->group = basename(dirname(__FILE__));
27
28
      $this->title = MODULE_CONTENT_FOOTER_BANNER_TITLE;
29
      $this->description = MODULE_CONTENT_FOOTER_BANNER_DESCRIPTION;
30
      $this->description .= '<div class="secWarning">' . MODULE_CONTENT_BOOTSTRAP_ROW_DESCRIPTION . '</div>';
31
32
      if ( defined('MODULE_CONTENT_FOOTER_BANNER_STATUS') ) {
33
        $this->sort_order = MODULE_CONTENT_FOOTER_BANNER_SORT_ORDER;
34
        $this->enabled = (MODULE_CONTENT_FOOTER_BANNER_STATUS == 'True');
35
      }
36
    }
37
38
    function execute() {
39
      global $oscTemplate;
40
41
      if (!empty(MODULE_CONTENT_FOOTER_BANNER_GROUP)) {
42
        $content_width = (int)MODULE_CONTENT_FOOTER_BANNER_CONTENT_WIDTH;
43
44
        if ($banner = tep_banner_exists('dynamic', MODULE_CONTENT_FOOTER_BANNER_GROUP)) {
45
          ob_start();
46
          include('includes/modules/content/' . $this->group . '/templates/banner.php');
47
          $template = ob_get_clean();
48
49
          $oscTemplate->addContent($template, $this->group);
50
        }
51
      }
52
    }
53
54
    function isEnabled() {
55
      return $this->enabled;
56
    }
57
58
    function check() {
59
      return defined('MODULE_CONTENT_FOOTER_BANNER_STATUS');
60
    }
61
62
    function install() {
63
      $OSCOM_Db = Registry::get('Db');
64
65
      $OSCOM_Db->save('configuration', [
66
        'configuration_title' => 'Enable Banner Footer Module',
67
        'configuration_key' => 'MODULE_CONTENT_FOOTER_BANNER_STATUS',
68
        'configuration_value' => 'True',
69
        'configuration_description' => 'Do you want to enable the Banner content module?',
70
        'configuration_group_id' => '6',
71
        'sort_order' => '1',
72
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
73
        'date_added' => 'now()'
74
      ]);
75
76
      $OSCOM_Db->save('configuration', [
77
        'configuration_title' => 'Banner Group',
78
        'configuration_key' => 'MODULE_CONTENT_FOOTER_BANNER_GROUP',
79
        'configuration_value' => '',
80
        'configuration_description' => 'Which group should the banner be displayed from?',
81
        'configuration_group_id' => '6',
82
        'sort_order' => '1',
83
        'use_function' => 'cm_footer_banner::showBannerGroupSelection',
84
        'set_function' => 'cm_footer_banner::getBannerGroupSelection(',
85
        'date_added' => 'now()'
86
      ]);
87
88
      $OSCOM_Db->save('configuration', [
89
        'configuration_title' => 'Content Width',
90
        'configuration_key' => 'MODULE_CONTENT_FOOTER_BANNER_CONTENT_WIDTH',
91
        'configuration_value' => '3',
92
        'configuration_description' => 'What width container should the content be shown in? (12 = full width, 6 = half width).',
93
        'configuration_group_id' => '6',
94
        'sort_order' => '1',
95
        'set_function' => 'tep_cfg_select_option(array(\'12\', \'11\', \'10\', \'9\', \'8\', \'7\', \'6\', \'5\', \'4\', \'3\', \'2\', \'1\'), ',
96
        'date_added' => 'now()'
97
      ]);
98
99
      $OSCOM_Db->save('configuration', [
100
        'configuration_title' => 'Sort Order',
101
        'configuration_key' => 'MODULE_CONTENT_FOOTER_BANNER_SORT_ORDER',
102
        'configuration_value' => '0',
103
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
104
        'configuration_group_id' => '6',
105
        'sort_order' => '0',
106
        'date_added' => 'now()'
107
      ]);
108
    }
109
110
    function remove() {
111
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
112
    }
113
114
    function keys() {
115
      return array('MODULE_CONTENT_FOOTER_BANNER_STATUS', 'MODULE_CONTENT_FOOTER_BANNER_GROUP', 'MODULE_CONTENT_FOOTER_BANNER_CONTENT_WIDTH', 'MODULE_CONTENT_FOOTER_BANNER_SORT_ORDER');
116
    }
117
118
    function showBannerGroupSelection($value) {
119
      return empty($value) ? '--None--' : $value;
120
    }
121
122
    function getBannerGroupSelection($value, $key = '') {
123
      $OSCOM_Db = Registry::get('Db');
124
125
      $name = !empty($key) ? 'configuration[' . $key . ']' : 'configuration_value';
126
127
      $groups = [
128
        [
129
          'id' => '',
130
          'text' => '--None--'
131
        ]
132
      ];
133
134
      $Qgroups = $OSCOM_Db->query('select distinct banners_group from :table_banners order by banners_group');
135
136 View Code Duplication
      while ($Qgroups->fetch()) {
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...
137
        $groups[] = [
138
          'id' => $Qgroups->value('banners_group'),
139
          'text' => $Qgroups->value('banners_group')
140
        ];
141
      }
142
143
      return HTML::selectField($name, $groups, $value);
144
    }
145
  }
146