Passed
Push — develop-3.3.x ( 59400a...541ac7 )
by Mario
02:37
created

main_donate::set_entity_donation_pages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
class main_donate extends main_controller
14
{
15
	private const RETURN_BODY = 'body';
16
	private const RETURN_CANCEL = 'cancel';
17
	private const RETURN_SUCCESS = 'success';
18
	private const RETURN_DONORLIST = 'donorlist';
19
20
	/** @var \skouat\ppde\actions\vars */
21
	protected $actions_vars;
22
23
	/** @var \skouat\ppde\controller\main_display_stats */
24
	protected $controller_display_stats;
25
26
	/** @var \skouat\ppde\entity\donation_pages */
27
	protected $donation_pages_entity;
28
29
	/** @var \skouat\ppde\operators\donation_pages */
30
	protected $donation_pages_operator;
31
32
	public function set_actions_vars(\skouat\ppde\actions\vars $actions_vars): void
33
	{
34
		$this->actions_vars = $actions_vars;
35
	}
36
37
	public function set_display_stats(\skouat\ppde\controller\main_display_stats $controller_display_stats): void
38
	{
39
		$this->controller_display_stats = $controller_display_stats;
40
	}
41
42
	public function set_entity_donation_pages(\skouat\ppde\entity\donation_pages $donation_pages_entity): void
43
	{
44
		$this->donation_pages_entity = $donation_pages_entity;
45
	}
46
47
	public function set_operator_donation_pages(\skouat\ppde\operators\donation_pages $donation_pages_operator): void
48
	{
49
		$this->donation_pages_operator = $donation_pages_operator;
50
	}
51
52
	public function handle()
53
	{
54
		$this->check_extension_enabled();
55
		$this->check_user_permission();
56
57
		$return_args = $this->request->variable('return', self::RETURN_BODY);
58
		$this->set_return_args($return_args);
59
60
		$this->prepare_donation_page_content($return_args);
61
		$this->prepare_donation_form();
62
		$this->controller_display_stats->display_stats();
63
64
		// Send all data to the template file
65
		return $this->helper->render('donate_body.html', $this->get_page_title($return_args));
66
	}
67
68
	/**
69
	 * Check if PPDE is enabled
70
	 *
71
	 * @throws \phpbb\exception\http_exception
72
	 */
73
	private function check_extension_enabled(): void
74
	{
75
		if (empty($this->config['ppde_enable']))
76
		{
77
			redirect(append_sid($this->root_path . 'index.' . $this->php_ext));
78
		}
79
	}
80
81
	/**
82
	 * Check if the user has permission to use PPDE
83
	 *
84
	 * @throws \phpbb\exception\http_exception
85
	 */
86
	private function check_user_permission(): void
87
	{
88
		if (!$this->actions_auth->can_use_ppde())
89
		{
90
			trigger_error('NOT_AUTHORISED');
91
		}
92
	}
93
94
	/**
95
	 * Set return arguments for the template
96
	 *
97
	 * @param string $return_args Return arguments
98
	 */
99
	private function set_return_args(string $return_args): void
100
	{
101
		$this->template->assign_vars([
102
			'S_RETURN_ARGS' => $return_args,
103
		]);
104
105
		if (in_array($return_args, [self::RETURN_CANCEL, self::RETURN_SUCCESS, self::RETURN_DONORLIST]))
106
		{
107
			$this->template->assign_var(
108
				'L_PPDE_' . strtoupper($return_args) . '_TITLE',
109
				$this->language->lang('PPDE_' . strtoupper($return_args) . '_TITLE')
110
			);
111
		}
112
	}
113
114
	/**
115
	 * Prepare the donation page content
116
	 *
117
	 * @param string $return_args Return arguments
118
	 */
119
	private function prepare_donation_page_content(string $return_args): void
120
	{
121
		$content_data = $this->get_donation_content_data($return_args);
122
		if (!empty($content_data))
123
		{
124
			$this->actions_vars->get_vars();
125
			$content = $this->actions_vars->replace_template_vars($this->donation_pages_entity->get_message_for_display());
126
			$this->template->assign_var('DONATION_BODY', $content);
127
		}
128
	}
129
130
	/**
131
	 * Get content of current donation pages
132
	 *
133
	 * @param string $return_args Return arguments
134
	 * @return array
135
	 */
136
	private function get_donation_content_data(string $return_args): array
137
	{
138
		return $this->donation_pages_entity->get_data(
139
			$this->donation_pages_operator->build_sql_data($this->user->get_iso_lang_id(), $return_args)
0 ignored issues
show
Bug introduced by
It seems like $this->user->get_iso_lang_id() can also be of type boolean; however, parameter $lang_id of skouat\ppde\operators\do...pages::build_sql_data() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
			$this->donation_pages_operator->build_sql_data(/** @scrutinizer ignore-type */ $this->user->get_iso_lang_id(), $return_args)
Loading history...
140
		);
141
	}
142
143
	/**
144
	 * Prepare the donation form
145
	 */
146
	private function prepare_donation_form(): void
147
	{
148
		$this->actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']);
149
150
		$this->template->assign_vars([
151
			'PPDE_DEFAULT_VALUE' => (int) ($this->config['ppde_default_value'] ?? 0),
152
			'PPDE_LIST_VALUE'    => $this->config['ppde_dropbox_enable'] && $this->config['ppde_dropbox_value']
153
				? $this->actions_currency->build_currency_value_select_menu($this->config['ppde_dropbox_value'], $this->config['ppde_default_value'])
0 ignored issues
show
Bug introduced by
$this->config['ppde_default_value'] of type string is incompatible with the type integer expected by parameter $default_value of skouat\ppde\actions\curr...ncy_value_select_menu(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
				? $this->actions_currency->build_currency_value_select_menu($this->config['ppde_dropbox_value'], /** @scrutinizer ignore-type */ $this->config['ppde_default_value'])
Loading history...
154
				: '',
155
			'S_HIDDEN_FIELDS'    => $this->get_paypal_hidden_fields(),
156
			'S_PPDE_FORM_ACTION' => $this->get_paypal_uri(),
157
			'S_SANDBOX'          => $this->use_sandbox(),
158
		]);
159
	}
160
161
	/**
162
	 * Get PayPal hidden fields
163
	 *
164
	 * @return string
165
	 */
166
	private function get_paypal_hidden_fields(): string
167
	{
168
		return build_hidden_fields([
169
			'cmd'           => '_donations',
170
			'business'      => $this->get_account_id(),
171
			'item_name'     => $this->language->lang('PPDE_DONATION_TITLE_HEAD', $this->config['sitename']),
172
			'no_shipping'   => 1,
173
			'return'        => generate_board_url(true) . $this->helper->route('skouat_ppde_donate', ['return' => self::RETURN_SUCCESS]),
174
			'notify_url'    => generate_board_url(true) . $this->helper->route('skouat_ppde_ipn_listener'),
175
			'cancel_return' => generate_board_url(true) . $this->helper->route('skouat_ppde_donate', ['return' => self::RETURN_CANCEL]),
176
			'item_number'   => 'uid_' . $this->user->data['user_id'] . '_' . time(),
177
			'custom'        => 'uid_' . $this->user->data['user_id'] . '_' . time(),
178
			'tax'           => 0,
179
			'bn'            => 'Board_Donate_WPS',
180
			'charset'       => 'utf-8',
181
		]);
182
	}
183
184
	/**
185
	 * Get PayPal account id
186
	 *
187
	 * @return string PayPal account Identifier
188
	 */
189
	private function get_account_id(): string
190
	{
191
		return $this->use_sandbox() ? $this->config['ppde_sandbox_address'] : $this->config['ppde_account_id'];
192
	}
193
194
	/**
195
	 * Get the page title
196
	 *
197
	 * @param string $return_args Return arguments
198
	 * @return string
199
	 */
200
	private function get_page_title($return_args)
201
	{
202
		$title_lang_var = 'PPDE_DONATION_TITLE';
203
		if (in_array($return_args, [self::RETURN_CANCEL, self::RETURN_SUCCESS]))
204
		{
205
			$title_lang_var = 'PPDE_' . strtoupper($return_args) . '_TITLE';
206
		}
207
		return $this->language->lang($title_lang_var);
208
	}
209
}
210