currency   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 186
rs 10
wmc 19

12 Methods

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