Passed
Push — develop-3.3.x ( 0d6b30...b6d44e )
by Mario
02:37
created

vars::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 13
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\actions;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\user;
16
17
class vars
18
{
19
	protected $actions_core;
20
	protected $actions_currency;
21
	protected $config;
22
	protected $dp_vars;
23
	protected $language;
24
	protected $user;
25
26
	/**
27
	 * currency constructor.
28
	 *
29
	 * @param \skouat\ppde\actions\core     $actions_core     PPDE actions core object
30
	 * @param \skouat\ppde\actions\currency $actions_currency PPDE actions currency object
31
	 * @param config                        $config           Config object
32
	 * @param language                      $language         Language object
33
	 * @param user                          $user             User object
34
	 *
35
	 * @access public
36
	 */
37
38
	public function __construct(
39
		core $actions_core,
40
		currency $actions_currency,
41
		config $config,
42
		language $language,
43
		user $user
44
	)
45
	{
46
		$this->actions_core = $actions_core;
47
		$this->actions_currency = $actions_currency;
48
		$this->config = $config;
49
		$this->language = $language;
50
		$this->user = $user;
51
	}
52
53
	/**
54
	 * Get template vars
55
	 *
56
	 * @return array $this->dp_vars
57
	 * @access public
58
	 */
59
	public function get_vars(): array
60
	{
61
		$currency_data = $this->get_currency_data();
62
		$this->dp_vars = $this->populate_template_vars($currency_data);
63
64
		if ($this->actions_core->is_in_admin())
65
		{
66
			$this->add_predefined_lang_vars();
67
		}
68
69
		return $this->dp_vars;
70
	}
71
72
	private function get_currency_data(): array
73
	{
74
		$default_currency_data = $this->actions_currency->get_default_currency_data((int) $this->config['ppde_default_currency']);
75
76
		return [
77
			$default_currency_data[0]['currency_iso_code'],
78
			$default_currency_data[0]['currency_symbol'],
79
			(bool) $default_currency_data[0]['currency_on_left'],
80
		];
81
	}
82
83
	private function populate_template_vars(array $currency_data): array
84
	{
85
		return [
86
			0 => ['var' => '{USER_ID}', 'value' => $this->user->data['user_id']],
87
			1 => ['var' => '{USERNAME}', 'value' => $this->user->data['username']],
88
			2 => ['var' => '{SITE_NAME}', 'value' => $this->config['sitename']],
89
			3 => ['var' => '{SITE_DESC}', 'value' => $this->config['site_desc']],
90
			4 => ['var' => '{BOARD_CONTACT}', 'value' => $this->config['board_contact']],
91
			5 => ['var' => '{BOARD_EMAIL}', 'value' => $this->config['board_email']],
92
			6 => ['var' => '{BOARD_SIG}', 'value' => $this->config['board_email_sig']],
93
			7 => ['var' => '{DONATION_GOAL}', 'value' => $this->actions_currency->format_currency(
94
				(float) $this->config['ppde_goal'], ...$currency_data)],
95
			8 => ['var' => '{DONATION_RAISED}', 'value' => $this->actions_currency->format_currency(
96
				(float) $this->config['ppde_raised'], ...$currency_data)],
97
		];
98
	}
99
100
	/**
101
	 * Adds predefined language keys variables to the donation pages.
102
	 *
103
	 * @return void
104
	 * @access private
105
	 */
106
	private function add_predefined_lang_vars(): void
107
	{
108
		array_walk($this->dp_vars, function (&$value) {
109
			$value['name'] = $this->language->lang('PPDE_DP_' . trim($value['var'], '{}'));
110
		});
111
	}
112
113
	/**
114
	 * Replace template vars in the message
115
	 *
116
	 * @param string $message
117
	 *
118
	 * @return string
119
	 * @access public
120
	 */
121
	public function replace_template_vars($message): string
122
	{
123
		$tpl_ary = array_column($this->dp_vars, 'value', 'var');
124
125
		return str_replace(array_keys($tpl_ary), array_values($tpl_ary), $message);
126
	}
127
}
128