main_display_stats   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 65
c 5
b 0
f 0
dl 0
loc 226
rs 10
wmc 29

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A assign_template_enable_vars() 0 6 1
A assign_template_stats_text_only_var() 0 3 1
A is_stats_enabled() 0 3 3
A assign_template_lang_vars() 0 6 1
A generate_stats_percentage() 0 16 3
A is_ppde_goal_stats() 0 3 2
A get_ppde_used_langkey() 0 19 3
A is_ppde_used_stats() 0 3 3
A display_stats() 0 13 2
A get_ppde_goal_langkey() 0 17 3
A get_ppde_raised_langkey() 0 11 2
A assign_vars_stats_percentage() 0 6 2
A calculate_percentage() 0 7 2
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\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
			$this->ppde_actions_currency->set_default_currency_data((int) $this->config['ppde_default_currency']);
60
61
			$this->assign_template_enable_vars();
62
			$this->assign_template_lang_vars();
63
			$this->assign_template_stats_text_only_var();
64
65
			// Generate statistics percent for display
66
			$this->generate_stats_percentage();
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(): void
90
	{
91
		$this->template->assign_vars([
92
			'L_PPDE_GOAL'   => $this->get_ppde_goal_langkey(),
93
			'L_PPDE_RAISED' => $this->get_ppde_raised_langkey(),
94
			'L_PPDE_USED'   => $this->get_ppde_used_langkey(),
95
		]);
96
	}
97
98
	/**
99
	 * Retrieve the language key for donation goal
100
	 *
101
	 * @return string
102
	 */
103
	public function get_ppde_goal_langkey(): string
104
	{
105
		$goal = (int) $this->config['ppde_goal'];
106
		$raised = (int) $this->config['ppde_raised'];
107
108
		if ($goal <= 0)
109
		{
110
			return $this->language->lang('PPDE_DONATE_NO_GOAL');
111
		}
112
113
		if ($goal < $raised)
114
		{
115
			return $this->language->lang('PPDE_DONATE_GOAL_REACHED');
116
		}
117
118
		$formatted_goal = $this->ppde_actions_currency->format_currency((float) $goal);
119
		return $this->language->lang('PPDE_DONATE_GOAL_RAISE', $formatted_goal);
120
	}
121
122
	/**
123
	 * Retrieve the language key for donation raised
124
	 *
125
	 * @return string
126
	 */
127
	public function get_ppde_raised_langkey(): string
128
	{
129
		$raised = (int) $this->config['ppde_raised'];
130
131
		if ($raised <= 0)
132
		{
133
			return $this->language->lang('PPDE_DONATE_NOT_RECEIVED');
134
		}
135
136
		$formatted_raised = $this->ppde_actions_currency->format_currency((float) $raised);
137
		return $this->language->lang('PPDE_DONATE_RECEIVED', $formatted_raised);
138
	}
139
140
	/**
141
	 * Retrieve the language key for donation used
142
	 *
143
	 * @return string
144
	 */
145
	public function get_ppde_used_langkey(): string
146
	{
147
		$used = (int) $this->config['ppde_used'];
148
		$raised = (int) $this->config['ppde_raised'];
149
150
		if ($used <= 0)
151
		{
152
			return $this->language->lang('PPDE_DONATE_NOT_USED');
153
		}
154
155
		$formatted_used = $this->ppde_actions_currency->format_currency((float) $used);
156
157
		if ($used < $raised)
158
		{
159
			$formatted_raised = $this->ppde_actions_currency->format_currency((float) $raised);
160
			return $this->language->lang('PPDE_DONATE_USED', $formatted_used, $formatted_raised);
161
		}
162
163
		return $this->language->lang('PPDE_DONATE_USED_EXCEEDED', $formatted_used);
164
	}
165
166
	/**
167
	 * Generate statistics percentage for display
168
	 *
169
	 * @return void
170
	 * @access private
171
	 */
172
	private function generate_stats_percentage(): void
173
	{
174
		$stat_conditions = [
175
			'GOAL_NUMBER' => ['condition' => $this->is_ppde_goal_stats(), 'numerator' => 'ppde_raised', 'denominator' => 'ppde_goal'],
176
			'USED_NUMBER' => ['condition' => $this->is_ppde_used_stats(), 'numerator' => 'ppde_used', 'denominator' => 'ppde_raised'],
177
		];
178
179
		foreach ($stat_conditions as $stat_name => $details)
180
		{
181
			if ($details['condition'])
182
			{
183
				$percentage = $this->calculate_percentage(
184
					(float) $this->config[$details['numerator']],
185
					(float) $this->config[$details['denominator']]
186
				);
187
				$this->assign_vars_stats_percentage($stat_name, $percentage);
188
			}
189
		}
190
	}
191
192
	/**
193
	 * Verifies if stats can be shown
194
	 *
195
	 * @return bool
196
	 * @access private
197
	 */
198
	private function is_ppde_goal_stats(): bool
199
	{
200
		return $this->config['ppde_goal_enable'] && (int) $this->config['ppde_goal'] > 0;
201
	}
202
203
	/**
204
	 * Verifies if statistics can be shown
205
	 *
206
	 * @return bool
207
	 * @access private
208
	 */
209
	private function is_ppde_used_stats(): bool
210
	{
211
		return $this->config['ppde_used_enable'] && (int) $this->config['ppde_raised'] > 0 && (int) $this->config['ppde_used'] > 0;
212
	}
213
214
	/**
215
	 * Gives back the percentage value
216
	 *
217
	 * @param float $numerator
218
	 * @param float $denominator
219
	 * @return float
220
	 */
221
	private function calculate_percentage(float $numerator, float $denominator): float
222
	{
223
		if ($denominator == 0)
224
		{
225
			return 0.0;
226
		}
227
		return round(($numerator * 100) / $denominator, 2);
228
	}
229
230
	/**
231
	 * Assigns statistics proportion vars to the template
232
	 *
233
	 * @param string $var_name
234
	 * @param float  $percentage
235
	 * @return void
236
	 * @access private
237
	 */
238
	private function assign_vars_stats_percentage(string $var_name, float $percentage): void
239
	{
240
		$this->template->assign_vars([
241
			'PPDE_' . $var_name          => ($percentage < 100) ? $percentage : round($percentage),
242
			'PPDE_' . $var_name . '_CSS' => max(0, min(100, $percentage)),
243
			'S_' . $var_name             => true,
244
		]);
245
	}
246
}
247