Passed
Push — develop-3.3.x ( 5782d9...f10cd4 )
by Mario
03:04
created

vars_helper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 92
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add_predefined_lang_vars() 0 5 2
A populate_template_vars() 0 12 1
A get_vars() 0 11 2
A __construct() 0 13 1
A replace_template_vars() 0 4 1
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\helpers;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\user;
16
use skouat\ppde\actions\core;
17
use skouat\ppde\actions\currency;
18
19
class vars_helper
20
{
21
	protected $actions_core;
22
	protected $actions_currency;
23
	protected $config;
24
	protected $dp_vars;
25
	protected $language;
26
	protected $user;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param core     $actions_core     PPDE actions core object
32
	 * @param currency $actions_currency PPDE actions currency object
33
	 * @param config   $config           Config object
34
	 * @param language $language         Language object
35
	 * @param user     $user             User object
36
	 */
37
	public function __construct(
38
		core $actions_core,
39
		currency $actions_currency,
40
		config $config,
41
		language $language,
42
		user $user
43
	)
44
	{
45
		$this->actions_core = $actions_core;
46
		$this->actions_currency = $actions_currency;
47
		$this->config = $config;
48
		$this->language = $language;
49
		$this->user = $user;
50
	}
51
52
	/**
53
	 * Get template vars
54
	 *
55
	 * @return array Array of template variables
56
	 */
57
	public function get_vars(): array
58
	{
59
		$this->actions_currency->set_default_currency_data((int) $this->config['ppde_default_currency']);
60
		$this->dp_vars = $this->populate_template_vars();
61
62
		if ($this->actions_core->is_in_admin())
63
		{
64
			$this->add_predefined_lang_vars();
65
		}
66
67
		return $this->dp_vars;
68
	}
69
70
	/**
71
	 * Populate template vars
72
	 *
73
	 * @return array Array of template variables
74
	 */
75
	private function populate_template_vars(): array
76
	{
77
		return [
78
			['var' => '{USER_ID}',         'value' => $this->user->data['user_id']],
79
			['var' => '{USERNAME}',        'value' => $this->user->data['username']],
80
			['var' => '{SITE_NAME}',       'value' => $this->config['sitename']],
81
			['var' => '{SITE_DESC}',       'value' => $this->config['site_desc']],
82
			['var' => '{BOARD_CONTACT}',   'value' => $this->config['board_contact']],
83
			['var' => '{BOARD_EMAIL}',     'value' => $this->config['board_email']],
84
			['var' => '{BOARD_SIG}',       'value' => $this->config['board_email_sig']],
85
			['var' => '{DONATION_GOAL}',   'value' => $this->actions_currency->format_currency((float) $this->config['ppde_goal'])],
86
			['var' => '{DONATION_RAISED}', 'value' => $this->actions_currency->format_currency((float) $this->config['ppde_raised'])],
87
		];
88
	}
89
90
	/**
91
	 * Adds predefined language keys variables to the donation pages.
92
	 */
93
	private function add_predefined_lang_vars(): void
94
	{
95
		foreach ($this->dp_vars as &$value)
96
		{
97
			$value['name'] = $this->language->lang('PPDE_DP_' . trim($value['var'], '{}'));
98
		}
99
	}
100
101
	/**
102
	 * Replace template vars in the message
103
	 *
104
	 * @param string $message The message containing template variables
105
	 * @return string The message with template variables replaced
106
	 */
107
	public function replace_template_vars(string $message): string
108
	{
109
		$tpl_ary = array_column($this->dp_vars, 'value', 'var');
110
		return strtr($message, $tpl_ary);
111
	}
112
}
113