Passed
Push — develop-3.3.x ( ac09dd...6388bd )
by Mario
02:52
created

main_display_stats::calculate_index()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\template\template;
16
use skouat\ppde\actions\currency;
17
18
class main_display_stats
19
{
20
	protected $config;
21
	protected $language;
22
	protected $ppde_actions_currency;
23
	protected $template;
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param config   $config                Config object
29
	 * @param language $language              Language user object
30
	 * @param currency $ppde_actions_currency Currency actions object
31
	 * @param template $template              Template object
32
	 *
33
	 * @access public
34
	 */
35
	public function __construct(
36
		config $config,
37
		language $language,
38
		currency $ppde_actions_currency,
39
		template $template
40
	)
41
	{
42
		$this->config = $config;
43
		$this->language = $language;
44
		$this->ppde_actions_currency = $ppde_actions_currency;
45
		$this->template = $template;
46
	}
47
48
	/**
49
	 * Assign statistics vars to the template
50
	 *
51
	 * @return void
52
	 * @access public
53
	 */
54
	public function display_stats(): void
55
	{
56
		if ($this->is_stats_enabled())
57
		{
58
			// Get data for the default currency
59
			$default_currency_data = $this->ppde_actions_currency->get_default_currency_data((int) $this->config['ppde_default_currency']);
60
61
			$this->assign_template_enable_vars();
62
			$this->assign_template_lang_vars($default_currency_data);
63
			$this->assign_template_stats_text_only_var();
64
65
			// Generate statistics percent for display
66
			$this->generate_stats_proportion();
67
		}
68
	}
69
70
	private function is_stats_enabled(): bool
71
	{
72
		return $this->config['ppde_goal_enable'] || $this->config['ppde_raised_enable'] || $this->config['ppde_used_enable'];
73
	}
74
75
	private function assign_template_enable_vars(): void
76
	{
77
		$this->template->assign_vars([
78
			'PPDE_GOAL_ENABLE'   => $this->config['ppde_goal_enable'],
79
			'PPDE_RAISED_ENABLE' => $this->config['ppde_raised_enable'],
80
			'PPDE_USED_ENABLE'   => $this->config['ppde_used_enable'],
81
		]);
82
	}
83
84
	private function assign_template_stats_text_only_var(): void
85
	{
86
		$this->template->assign_var('S_PPDE_STATS_TEXT_ONLY', $this->config['ppde_stats_text_only']);
87
	}
88
89
	private function assign_template_lang_vars(array $default_currency_data): void
90
	{
91
		$iso_code = $default_currency_data[0]['currency_iso_code'];
92
		$symbol = $default_currency_data[0]['currency_symbol'];
93
		$currency_on_left = (bool) $default_currency_data[0]['currency_on_left'];
94
95
		$this->template->assign_vars([
96
			'L_PPDE_GOAL'   => $this->get_ppde_goal_langkey($iso_code, $symbol, $currency_on_left),
97
			'L_PPDE_RAISED' => $this->get_ppde_raised_langkey($iso_code, $symbol, $currency_on_left),
98
			'L_PPDE_USED'   => $this->get_ppde_used_langkey($iso_code, $symbol, $currency_on_left),
99
		]);
100
	}
101
102
	/**
103
	 * Retrieve the language key for donation goal
104
	 *
105
	 * @param string $currency_iso_code
106
	 * @param string $currency_symbol Currency symbol
107
	 * @param bool   $on_left         Symbol position
108
	 *
109
	 * @return string
110
	 * @access public
111
	 */
112
	public function get_ppde_goal_langkey($currency_iso_code, $currency_symbol, $on_left = true): string
113
	{
114
		if ((int) $this->config['ppde_goal'] <= 0)
115
		{
116
			return $this->language->lang('PPDE_DONATE_NO_GOAL');
117
		}
118
119
		if ((int) $this->config['ppde_goal'] < (int) $this->config['ppde_raised'])
120
		{
121
			return $this->language->lang('PPDE_DONATE_GOAL_REACHED');
122
		}
123
124
		return $this->language->lang('PPDE_DONATE_GOAL_RAISE', $this->ppde_actions_currency->format_currency((float) $this->config['ppde_goal'], $currency_iso_code, $currency_symbol, $on_left));
125
	}
126
127
	/**
128
	 * Retrieve the language key for donation raised
129
	 *
130
	 * @param string $currency_iso_code
131
	 * @param string $currency_symbol Currency symbol
132
	 * @param bool   $on_left         Symbol position
133
	 *
134
	 * @return string
135
	 * @access public
136
	 */
137
	public function get_ppde_raised_langkey($currency_iso_code, $currency_symbol, $on_left = true): string
138
	{
139
		if ((int) $this->config['ppde_raised'] <= 0)
140
		{
141
			return $this->language->lang('PPDE_DONATE_NOT_RECEIVED');
142
		}
143
144
		return $this->language->lang('PPDE_DONATE_RECEIVED', $this->ppde_actions_currency->format_currency((float) $this->config['ppde_raised'], $currency_iso_code, $currency_symbol, $on_left));
145
	}
146
147
	/**
148
	 * Retrieve the language key for donation used
149
	 *
150
	 * @param string $currency_iso_code
151
	 * @param string $currency_symbol Currency symbol
152
	 * @param bool   $on_left         Symbol position
153
	 *
154
	 * @return string
155
	 * @access public
156
	 */
157
	public function get_ppde_used_langkey($currency_iso_code, $currency_symbol, $on_left = true): string
158
	{
159
		if ((int) $this->config['ppde_used'] <= 0)
160
		{
161
			return $this->language->lang('PPDE_DONATE_NOT_USED');
162
		}
163
164
		if ((int) $this->config['ppde_used'] < (int) $this->config['ppde_raised'])
165
		{
166
			return $this->language->lang(
167
				'PPDE_DONATE_USED',
168
				$this->ppde_actions_currency->format_currency((float) $this->config['ppde_used'], $currency_iso_code, $currency_symbol, $on_left),
169
				$this->ppde_actions_currency->format_currency((float) $this->config['ppde_raised'], $currency_iso_code, $currency_symbol, $on_left)
170
			);
171
		}
172
173
		return $this->language->lang('PPDE_DONATE_USED_EXCEEDED', $this->ppde_actions_currency->format_currency((float) $this->config['ppde_used'], $currency_iso_code, $currency_symbol, $on_left));
174
	}
175
176
	/**
177
	 * Generate statistics proportion for display
178
	 *
179
	 * @return void
180
	 * @access private
181
	 */
182
	private function generate_stats_proportion(): void
183
	{
184
		$stat_conditions = [
185
			'GOAL_NUMBER' => ['condition' => $this->is_ppde_goal_stats(), 'nums' => ['ppde_raised', 'ppde_goal']],
186
			'USED_NUMBER' => ['condition' => $this->is_ppde_used_stats(), 'nums' => ['ppde_used', 'ppde_raised']],
187
		];
188
189
		foreach ($stat_conditions as $stat_name => $details)
190
		{
191
			if ($details['condition'])
192
			{
193
				$data = $details['nums'];
194
				$percentage = $this->percentage_value((float) $this->config[$data[0]], (float) $this->config[$data[1]]);
195
				$this->assign_vars_stats_proportion($stat_name, $percentage);
196
			}
197
		}
198
	}
199
200
	/**
201
	 * Verifies if stats can be shown
202
	 *
203
	 * @return bool
204
	 * @access private
205
	 */
206
	private function is_ppde_goal_stats()
207
	{
208
		return $this->config['ppde_goal_enable'] && (int) $this->config['ppde_goal'] > 0;
209
	}
210
211
	/**
212
	 * Verifies if statistics can be shown
213
	 *
214
	 * @return bool
215
	 * @access private
216
	 */
217
	private function is_ppde_used_stats()
218
	{
219
		return $this->config['ppde_used_enable'] && (int) $this->config['ppde_raised'] > 0 && (int) $this->config['ppde_used'] > 0;
220
	}
221
222
	/**
223
	 * Gives back the percentage value
224
	 *
225
	 * @param float $multiplicand
226
	 * @param float $divisor
227
	 *
228
	 * @return float
229
	 * @access private
230
	 */
231
	private function percentage_value($multiplicand, $divisor)
232
	{
233
		return ($multiplicand * 100) / $divisor;
234
	}
235
236
	/**
237
	 * Assigns statistics proportion vars to the template
238
	 *
239
	 * @param string $var_name
240
	 * @param float  $proportion
241
	 * @param bool   $reverse_css
242
	 *
243
	 * @return void
244
	 * @access private
245
	 */
246
	private function assign_vars_stats_proportion($var_name, $proportion)
247
	{
248
		// Enforcing $var_name to uppercase
249
		$var_name = strtoupper($var_name);
250
251
		$this->template->assign_vars([
252
			'PPDE_' . $var_name     => ($proportion < 100) ? round($proportion, 2) : round($proportion),
253
			'S_' . $var_name        => true,
254
		]);
255
	}
256
}
257