Completed
Push — develop-3.2.x ( 8c5ed0...addb5b )
by Mario
01:51
created

main_controller::compare_lt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\controller;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\language\language;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
class main_controller
23
{
24
	/** Production Postback URL */
25
	const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr';
26
	/** Sandbox Postback URL */
27
	const SANDBOX_VERIFY_URI = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr';
28
	/** @var array */
29
	private static $operators_table = array(
30
		'<'  => 'compare_lt',
31
		'<=' => 'compare_lte',
32
		'=='  => 'compare_eq',
33
		'>=' => 'compare_gte',
34
		'>'  => 'compare_gt',
35
	);
36
37
	protected $auth;
38
	protected $config;
39
	protected $container;
40
	protected $helper;
41
	protected $language;
42
	protected $ppde_entity_currency;
43
	protected $ppde_operator_currency;
44
	protected $request;
45
	protected $template;
46
	protected $user;
47
	protected $root_path;
48
	protected $php_ext;
49
50
	/**
51
	 * Constructor
52
	 *
53
	 * @param auth                            $auth                   Auth object
54
	 * @param config                          $config                 Config object
55
	 * @param ContainerInterface              $container              Service container interface
56
	 * @param helper                          $helper                 Controller helper object
57
	 * @param language                        $language               Language user object
58
	 * @param \skouat\ppde\entity\currency    $ppde_entity_currency   Currency entity object
59
	 * @param \skouat\ppde\operators\currency $ppde_operator_currency Currency operator object
60
	 * @param request                         $request                Request object
61
	 * @param template                        $template               Template object
62
	 * @param user                            $user                   User object
63
	 * @param string                          $root_path              phpBB root path
64
	 * @param string                          $php_ext                phpEx
65
	 *
66
	 * @access public
67
	 */
68
	public function __construct(auth $auth, config $config, ContainerInterface $container, helper $helper, language $language, \skouat\ppde\entity\currency $ppde_entity_currency, \skouat\ppde\operators\currency $ppde_operator_currency, request $request, template $template, user $user, $root_path, $php_ext)
69
	{
70
		$this->auth = $auth;
71
		$this->config = $config;
72
		$this->container = $container;
73
		$this->helper = $helper;
74
		$this->language = $language;
75
		$this->ppde_entity_currency = $ppde_entity_currency;
76
		$this->ppde_operator_currency = $ppde_operator_currency;
77
		$this->request = $request;
78
		$this->template = $template;
79
		$this->user = $user;
80
		$this->root_path = $root_path;
81
		$this->php_ext = $php_ext;
82
	}
83
84
	public function handle()
85
	{
86
		// We stop the execution of the code because nothing need to be returned to phpBB.
87
		garbage_collection();
88
		exit_handler();
89
	}
90
91
	/**
92
	 * @return bool
93
	 * @access public
94
	 */
95
	public function can_use_ppde()
96
	{
97
		return $this->auth->acl_get('u_ppde_use');
98
	}
99
100
	/**
101
	 * @return bool
102
	 * @access public
103
	 */
104
	public function can_view_ppde_donorlist()
105
	{
106
		return $this->auth->acl_get('u_ppde_view_donorlist');
107
	}
108
109
	/**
110
	 * @return bool
111
	 * @access private
112
	 */
113
	public function donorlist_is_enabled()
114
	{
115
		return $this->use_ipn() && $this->config['ppde_ipn_donorlist_enable'];
116
	}
117
118
	/**
119
	 * Check if IPN is enabled based on config value
120
	 *
121
	 * @return bool
122
	 * @access public
123
	 */
124
	public function use_ipn()
125
	{
126
		return !empty($this->config['ppde_enable']) && !empty($this->config['ppde_ipn_enable']) && $this->is_ipn_requirement_satisfied();
127
	}
128
129
	/**
130
	 * Check if IPN requirements are satisfied based on config value
131
	 *
132
	 * @return bool
133
	 * @access public
134
	 */
135
	public function is_ipn_requirement_satisfied()
136
	{
137
		return !empty($this->config['ppde_curl_detected']) && !empty($this->config['ppde_tls_detected']);
138
	}
139
140
	/**
141
	 * Build pull down menu options of available currency
142
	 *
143
	 * @param int $config_value Currency identifier; default: 0
144
	 *
145
	 * @return void
146
	 * @access public
147
	 */
148
	public function build_currency_select_menu($config_value = 0)
149
	{
150
		// Grab the list of all enabled currencies; 0 is for all data
151
		$currency_items = $this->ppde_entity_currency->get_data($this->ppde_operator_currency->build_sql_data(0, true));
152
153
		// Process each rule menu item for pull-down
154
		foreach ($currency_items as $currency_item)
155
		{
156
			// Set output block vars for display in the template
157
			$this->template->assign_block_vars('options', array(
158
				'CURRENCY_ID'        => (int) $currency_item['currency_id'],
159
				'CURRENCY_ISO_CODE'  => $currency_item['currency_iso_code'],
160
				'CURRENCY_NAME'      => $currency_item['currency_name'],
161
				'CURRENCY_SYMBOL'    => $currency_item['currency_symbol'],
162
				'S_CURRENCY_DEFAULT' => $config_value == $currency_item['currency_id'],
163
			));
164
		}
165
		unset ($currency_items, $currency_item);
166
	}
167
168
	/**
169
	 * Get PayPal URI
170
	 * Used in form and in IPN process
171
	 *
172
	 * @param bool $is_test_ipn
173
	 *
174
	 * @return string
175
	 * @access public
176
	 */
177
	public function get_paypal_uri($is_test_ipn = false)
178
	{
179
		return ($is_test_ipn || $this->use_sandbox()) ? self::SANDBOX_VERIFY_URI : self::VERIFY_URI;
180
	}
181
182
	/**
183
	 * Check if Sandbox is enabled based on config value
184
	 *
185
	 * @return bool
186
	 * @access public
187
	 */
188
	public function use_sandbox()
189
	{
190
		return $this->use_ipn() && !empty($this->config['ppde_sandbox_enable']) && $this->is_sandbox_founder_enable();
191
	}
192
193
	/**
194
	 * Check if Sandbox could be use by founders based on config value
195
	 *
196
	 * @return bool
197
	 * @access public
198
	 */
199
	public function is_sandbox_founder_enable()
200
	{
201
		return (!empty($this->config['ppde_sandbox_founder_enable']) && ($this->user->data['user_type'] == USER_FOUNDER)) || empty($this->config['ppde_sandbox_founder_enable']);
202
	}
203
204
	/**
205
	 * Get default currency symbol
206
	 *
207
	 * @param int $id Currency identifier; default: 0
208
	 *
209
	 * @return array
210
	 * @access public
211
	 */
212
	public function get_default_currency_data($id = 0)
213
	{
214
		return $this->ppde_entity_currency->get_data($this->ppde_operator_currency->build_sql_data($id, true));
215
	}
216
217
	/**
218
	 * Put the currency on the left or on the right of the amount
219
	 *
220
	 * @param int|float $value
221
	 * @param string    $currency
222
	 * @param bool      $on_left
223
	 * @param string    $dec_point
224
	 * @param string    $thousands_sep
225
	 *
226
	 * @return string
227
	 * @access public
228
	 */
229
	public function currency_on_left($value, $currency, $on_left = true, $dec_point = '.', $thousands_sep = '')
230
	{
231
		return $on_left ? $currency . number_format(round($value, 2), 2, $dec_point, $thousands_sep) : number_format(round($value, 2), 2, $dec_point, $thousands_sep) . $currency;
232
	}
233
234
235
	/**
236
	 * Compare two value
237
	 *
238
	 * @param int    $value1
239
	 * @param int    $value2
240
	 * @param string $operator
241
	 *
242
	 * @return bool
243
	 * @access public
244
	 */
245
	public function compare($value1, $value2, $operator)
246
	{
247
		if (array_key_exists($operator, $this::$operators_table))
248
		{
249
			return call_user_func_array(array($this, $this::$operators_table[$operator]), array($value1, $value2));
250
		}
251
		else
252
		{
253
			return false;
254
		}
255
	}
256
257
	/**
258
	 * Method called by $this->compare
259
	 *
260
	 * @param $a
261
	 * @param $b
262
	 *
263
	 * @return bool
264
	 * @access private
265
	 */
266
	private function compare_lt($a, $b)
267
	{
268
		return $a < $b;
269
	}
270
271
	/**
272
	 * Method called by $this->compare
273
	 *
274
	 * @param $a
275
	 * @param $b
276
	 *
277
	 * @return bool
278
	 * @access private
279
	 */
280
	private function compare_lte($a, $b)
281
	{
282
		return $a <= $b;
283
	}
284
285
	/**
286
	 * Method called by $this->compare
287
	 *
288
	 * @param $a
289
	 * @param $b
290
	 *
291
	 * @return bool
292
	 * @access private
293
	 */
294
	private function compare_eq($a, $b)
295
	{
296
		return $a == $b;
297
	}
298
299
	/**
300
	 * Method called by $this->compare
301
	 *
302
	 * @param $a
303
	 * @param $b
304
	 *
305
	 * @return bool
306
	 * @access private
307
	 */
308
	private function compare_gte($a, $b)
309
	{
310
		return $a >= $b;
311
	}
312
313
	/**
314
	 * Method called by $this->compare
315
	 *
316
	 * @param $a
317
	 * @param $b
318
	 *
319
	 * @return bool
320
	 * @access private
321
	 */
322
	private function compare_gt($a, $b)
323
	{
324
		return $a > $b;
325
	}
326
}
327