Completed
Pull Request — master (#39)
by Matt
06:12
created

main_module   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 15
c 6
b 0
f 0
lcom 1
cbo 0
dl 0
loc 194
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A main() 0 22 3
A display_settings() 0 9 1
A display_manage() 0 7 1
A get_sites() 0 19 3
A get_enabled_sites() 0 10 3
A save_manage() 0 10 1
A save_settings() 0 12 1
A purge_textformatter_cache() 0 5 1
1
<?php
2
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\acp;
12
13
/**
14
 * phpBB Media Embed Plugin ACP module.
15
 */
16
class main_module
17
{
18
	/** @var \phpbb\cache\driver\driver_interface $cache */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config $config */
22
	protected $config;
23
24
	/** @var \phpbb\config\db_text $config_text */
25
	protected $config_text;
26
27
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
28
	protected $container;
29
30
	/** @var \phpbb\language\language $language */
31
	protected $language;
32
33
	/** @var \phpbb\log\log $log */
34
	protected $log;
35
36
	/** @var \phpbb\request\request $request */
37
	protected $request;
38
39
	/** @var \phpbb\template\template $template */
40
	protected $template;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $page_title */
49
	public $page_title;
50
51
	/** @var string $tpl_name */
52
	public $tpl_name;
53
54
	/** @var string $u_action */
55
	public $u_action;
56
57
	/**
58
	 * Constructor
59
	 */
60
	public function __construct()
61
	{
62
		global $phpbb_container;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
63
64
		$this->container   = $phpbb_container;
65
		$this->cache       = $this->container->get('cache');
66
		$this->config      = $this->container->get('config');
67
		$this->config_text = $this->container->get('config_text');
68
		$this->language    = $this->container->get('language');
69
		$this->log         = $this->container->get('log');
70
		$this->request     = $this->container->get('request');
71
		$this->template    = $this->container->get('template');
72
		$this->user        = $this->container->get('user');
73
74
		$this->language->add_lang('acp', 'phpbb/mediaembed');
75
	}
76
77
	/**
78
	 * Main ACP module
79
	 *
80
	 * @param int    $id   The module ID (not used)
81
	 * @param string $mode The module mode (manage|settings)
82
	 */
83
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
	{
85
		$mode = strtolower($mode);
86
87
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . $mode;
88
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
89
90
		$form_key = 'phpbb/mediaembed';
91
		add_form_key($form_key);
92
93
		if ($this->request->is_set_post('submit'))
94
		{
95
			if (!check_form_key($form_key))
96
			{
97
				trigger_error('FORM_INVALID', E_USER_WARNING);
98
			}
99
100
			$this->{'save_' . $mode}();
101
		}
102
103
		$this->{'display_' . $mode}();
104
	}
105
106
	/**
107
	 * Add settings template vars to the form
108
	 */
109
	protected function display_settings()
110
	{
111
		$this->template->assign_vars([
112
			'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
113
			'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
114
			'S_MEDIA_EMBED_PARSE_URLS'	=> $this->config['media_embed_parse_urls'],
115
			'U_ACTION'					=> $this->u_action,
116
		]);
117
	}
118
119
	/**
120
	 * Add manage sites template vars to the form
121
	 */
122
	protected function display_manage()
123
	{
124
		$this->template->assign_vars([
125
			'MEDIA_SITES'	=> $this->get_sites(),
126
			'U_ACTION'		=> $this->u_action,
127
		]);
128
	}
129
130
	/**
131
	 * Get a list of available sites
132
	 *
133
	 * @return array An array of available sites
134
	 */
135
	protected function get_sites()
136
	{
137
		$sites = [];
138
139
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
140
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
141
		{
142
			$disabled = isset($configurator->BBCodes[$siteId]);
143
			$sites[] = [
144
				'id'		=> $siteId,
145
				'name'		=> $siteConfig['name'],
146
				'title'		=> $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
147
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
148
				'disabled'	=> $disabled,
149
			];
150
		}
151
152
		return $sites;
153
	}
154
155
	/**
156
	 * Get enabled media sites stored in the database
157
	 *
158
	 * @return array An array of enabled sites
159
	 */
160
	protected function get_enabled_sites()
161
	{
162
		if ($this->enabled_sites === null)
163
		{
164
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
165
			$this->enabled_sites = is_array($sites) ? $sites : [];
166
		}
167
168
		return $this->enabled_sites;
169
	}
170
171
	/**
172
	 * Save site managed data to the database
173
	 */
174
	protected function save_manage()
175
	{
176
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
177
178
		$this->purge_textformatter_cache();
179
180
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
181
182
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
183
	}
184
185
	/**
186
	 * Save settings data to the database
187
	 */
188
	protected function save_settings()
189
	{
190
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
191
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
192
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
193
194
		$this->purge_textformatter_cache();
195
196
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
197
198
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
199
	}
200
201
	/**
202
	 * Purge cached TextFormatter files
203
	 */
204
	protected function purge_textformatter_cache()
205
	{
206
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
207
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
208
	}
209
}
210