Completed
Branch master (915db5)
by Mario
03:03
created

currency   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 249
Duplicated Lines 8.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 13
Bugs 3 Features 2
Metric Value
wmc 26
c 13
b 3
f 2
lcom 1
cbo 1
dl 22
loc 249
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A build_sql_data_exists() 0 6 2
A check_required_field() 0 4 3
A get_currency_position() 0 4 2
A set_currency_position() 0 7 1
A check_currency_enable() 0 8 2
A __construct() 22 22 1
A get_currency_order() 0 4 2
A get_iso_code() 0 4 2
A get_symbol() 0 4 2
A set_currency_enable() 0 7 1
A set_iso_code() 0 7 1
A set_symbol() 0 7 1
A set_order() 0 18 3
A get_max_order() 0 8 1
A get_currency_enable() 0 4 2

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
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\entity;
12
13
/**
14
 * @property \phpbb\db\driver\driver_interface db                 phpBB Database object
15
 * @property \phpbb\user                       user               phpBB User object
16
 * @property string                            lang_key_prefix    Prefix for the messages thrown by exceptions
17
 * @property string                            lang_key_suffix    Suffix for the messages thrown by exceptions
18
 */
19
class currency extends main
20
{
21
	/**
22
	 * Data for this entity
23
	 *
24
	 * @var array
25
	 *    currency_id
26
	 *    currency_name
27
	 *    currency_iso_code
28
	 *    currency_symbol
29
	 *    currency_enable
30
	 *    currency_order
31
	 * @access protected
32
	 */
33
	protected $data;
34
	protected $currency_table;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\db\driver\driver_interface $db         Database object
40
	 * @param \phpbb\user                       $user       User object
41
	 * @param string                            $table_name Name of the table used to store data
42
	 *
43
	 * @access public
44
	 */
45 View Code Duplication
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $table_name)
46
	{
47
		$this->db = $db;
48
		$this->user = $user;
49
		$this->currency_table = $table_name;
50
		parent::__construct(
51
			$db,
52
			$user,
53
			'PPDE_DC',
54
			'CURRENCY',
55
			$table_name,
56
			array(
57
				'item_id'       => array('name' => 'currency_id', 'type' => 'integer'),
58
				'item_name'     => array('name' => 'currency_name', 'type' => 'string'),
59
				'item_iso_code' => array('name' => 'currency_iso_code', 'type' => 'string'),
60
				'item_symbol'   => array('name' => 'currency_symbol', 'type' => 'string'),
61
				'item_on_left'  => array('name' => 'currency_on_left', 'type' => 'boolean'),
62
				'item_enable'   => array('name' => 'currency_enable', 'type' => 'boolean'),
63
				'item_order'    => array('name' => 'currency_order', 'type' => 'integer'),
64
			)
65
		);
66
	}
67
68
	/**
69
	 * SQL Query to return the ID of selected currency
70
	 *
71
	 * @param string $iso_code Currency ISO code name
72
	 *
73
	 * @return string
74
	 * @access public
75
	 */
76
	public function build_sql_data_exists($iso_code = '')
77
	{
78
		return 'SELECT currency_id
79
			FROM ' . $this->currency_table . "
80
			WHERE currency_iso_code = '" . $this->db->sql_escape($iso_code ? $iso_code : $this->data['currency_iso_code']) . "'";
81
	}
82
83
	/**
84
	 * Get the order number of the currency
85
	 *
86
	 * @return int Order identifier
87
	 * @access public
88
	 */
89
	public function get_currency_order()
90
	{
91
		return (isset($this->data['currency_order'])) ? (int) $this->data['currency_order'] : 0;
92
	}
93
94
	/**
95
	 * Get Currency status
96
	 *
97
	 * @return bool
98
	 * @access public
99
	 */
100
	public function get_currency_position()
101
	{
102
		return (isset($this->data['currency_on_left'])) ? (bool) $this->data['currency_on_left'] : false;
103
	}
104
105
	/**
106
	 * Get Currency ISO code
107
	 *
108
	 * @return string ISO code name
109
	 * @access public
110
	 */
111
	public function get_iso_code()
112
	{
113
		return (isset($this->data['currency_iso_code'])) ? (string) $this->data['currency_iso_code'] : '';
114
	}
115
116
	/**
117
	 * Get Currency Symbol
118
	 *
119
	 * @return string Currency symbol
120
	 * @access public
121
	 */
122
	public function get_symbol()
123
	{
124
		return (isset($this->data['currency_symbol'])) ? (string) html_entity_decode($this->data['currency_symbol'], ENT_COMPAT | ENT_HTML5, 'UTF-8') : '';
125
	}
126
127
	/**
128
	 * Set Currency status
129
	 *
130
	 * @param bool $on_left
131
	 *
132
	 * @return bool
133
	 * @access public
134
	 */
135
	public function set_currency_position($on_left)
136
	{
137
		// Set the item type on our data array
138
		$this->data['currency_on_left'] = (bool) $on_left;
139
140
		return $this;
141
	}
142
143
	/**
144
	 * Set Currency status
145
	 *
146
	 * @param bool $enable
147
	 *
148
	 * @return bool
149
	 * @access public
150
	 */
151
	public function set_currency_enable($enable)
152
	{
153
		// Set the item type on our data array
154
		$this->data['currency_enable'] = (bool) $enable;
155
156
		return $this;
157
	}
158
159
	/**
160
	 * Set Currency ISO code name
161
	 *
162
	 * @param string $iso_code
163
	 *
164
	 * @return currency $this object for chaining calls; load()->set()->save()
165
	 * @access public
166
	 */
167
	public function set_iso_code($iso_code)
168
	{
169
		// Set the lang_id on our data array
170
		$this->data['currency_iso_code'] = (string) $iso_code;
171
172
		return $this;
173
	}
174
175
	/**
176
	 * Set Currency symbol
177
	 *
178
	 * @param string $symbol
179
	 *
180
	 * @return currency $this object for chaining calls; load()->set()->save()
181
	 * @access public
182
	 */
183
	public function set_symbol($symbol)
184
	{
185
		// Set the lang_id on our data array
186
		$this->data['currency_symbol'] = (string) htmlentities($symbol, ENT_COMPAT | ENT_HTML5, 'UTF-8');
187
188
		return $this;
189
	}
190
191
	/**
192
	 * Check if required field are set
193
	 *
194
	 * @return bool
195
	 * @access public
196
	 */
197
	public function check_required_field()
198
	{
199
		return empty($this->data['currency_name']) || empty($this->data['currency_iso_code']) || empty($this->data['currency_symbol']);
200
	}
201
202
	/**
203
	 * Set Currency order number
204
	 *
205
	 * @return currency $this object for chaining calls; load()->set()->save()
206
	 * @access protected
207
	 */
208
	protected function set_order()
209
	{
210
		$order = (int) $this->get_max_order() + 1;
211
212
		/*
213
		* If the data is out of range we'll throw an exception. We use 16777215 as a
214
		* maximum because it matches the MySQL unsigned mediumint maximum value which
215
		* is the lowest amongst the DBMS supported by phpBB.
216
		*/
217
		if ($order < 0 || $order > 16777215)
218
		{
219
			$this->display_warning_message('EXCEPTION_OUT_OF_BOUNDS', 'currency_order');
220
		}
221
222
		$this->data['currency_order'] = $order;
223
224
		return $this;
225
	}
226
227
	/**
228
	 * Get max currency order value
229
	 *
230
	 * @return int Order identifier
231
	 * @access private
232
	 */
233
	private function get_max_order()
234
	{
235
		$sql = 'SELECT MAX(currency_order) AS max_order
236
			FROM ' . $this->currency_table;
237
		$this->db->sql_query($sql);
238
239
		return $this->db->sql_fetchfield('max_order');
240
	}
241
242
	/**
243
	 * Returns error if the currency is enabled
244
	 *
245
	 * @return null
246
	 * @access protected
247
	 */
248
	protected function check_currency_enable()
249
	{
250
		if ($this->get_currency_enable())
251
		{
252
			// Return an error if the currency is enabled
253
			$this->display_warning_message('PPDE_DISABLE_BEFORE_DELETION');
254
		}
255
	}
256
257
	/**
258
	 * Get Currency status
259
	 *
260
	 * @return boolean
261
	 * @access public
262
	 */
263
	public function get_currency_enable()
264
	{
265
		return (isset($this->data['currency_enable'])) ? (bool) $this->data['currency_enable'] : false;
266
	}
267
}
268