Completed
Push — master ( 1e7f7b...a70b60 )
by Michael
8s
created

main_module   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 16
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 193
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A main() 0 20 4
A display() 0 9 1
A get_sites() 0 21 3
A get_enabled_sites() 0 10 3
A save_manage() 0 13 1
A save_settings() 0 10 1
A check_form_key() 0 7 2
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\request\request $request */
31
	protected $request;
32
33
	/** @var \phpbb\template\template $template */
34
	protected $template;
35
36
	/** @var \phpbb\language\language $language */
37
	protected $language;
38
39
	/** @var \phpbb\log\log $log */
40
	protected $log;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $form_key */
49
	protected $form_key;
50
51
	/** @var string $page_title */
52
	public $page_title;
53
54
	/** @var string $tpl_name */
55
	public $tpl_name;
56
57
	/** @var string $u_action */
58
	public $u_action;
59
60
	/**
61
	 * Constructor
62
	 */
63
	public function __construct()
64
	{
65
		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...
66
67
		$this->container   = $phpbb_container;
68
		$this->cache       = $this->container->get('cache');
69
		$this->config      = $this->container->get('config');
70
		$this->config_text = $this->container->get('config_text');
71
		$this->language    = $this->container->get('language');
72
		$this->log         = $this->container->get('log');
73
		$this->request     = $this->container->get('request');
74
		$this->template    = $this->container->get('template');
75
		$this->user        = $this->container->get('user');
76
		$this->form_key    = 'phpbb/mediaembed';
77
78
		$this->language->add_lang('acp', 'phpbb/mediaembed');
79
	}
80
81
	/**
82
	 * Main ACP module
83
	 *
84
	 * @param int    $id   The module ID
85
	 * @param string $mode The module mode
86
	 */
87
	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...
88
	{
89
		add_form_key($this->form_key);
90
91
		if ($this->request->is_set_post('submit'))
92
		{
93
			$this->{'save_' . strtolower($mode)}();
94
		}
95
96
		switch ($mode)
97
		{
98
			case 'manage':
99
				$this->display($mode, ['MEDIA_SITES' => $this->get_sites()]);
100
			break;
101
102
			case 'settings':
103
				$this->display($mode, ['S_MEDIA_EMBED_BBCODE' => $this->config['media_embed_bbcode']]);
104
			break;
105
		}
106
	}
107
108
	/**
109
	 * Display data in the ACP module
110
	 *
111
	 * @param string $mode The ACP module mode (manage|settings)
112
	 * @param array  $data Array of data to assign to the template
113
	 */
114
	protected function display($mode, $data)
115
	{
116
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . strtolower($mode);
117
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
118
119
		$this->template->assign_vars(array_merge($data, [
120
			'U_ACTION'	=> $this->u_action,
121
		]));
122
	}
123
124
	/**
125
	 * Get a list of available sites
126
	 *
127
	 * @return array An array of available sites
128
	 */
129
	protected function get_sites()
130
	{
131
		$sites = [];
132
133
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
134
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
135
		{
136
			if (isset($configurator->BBCodes[$siteId]))
137
			{
138
				continue;
139
			}
140
141
			$sites[] = [
142
				'id'		=> $siteId,
143
				'name'		=> $siteConfig['name'],
144
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
145
			];
146
		}
147
148
		return $sites;
149
	}
150
151
	/**
152
	 * Get enabled media sites stored in the database
153
	 *
154
	 * @return array An array of enabled sites
155
	 */
156
	protected function get_enabled_sites()
157
	{
158
		if ($this->enabled_sites === null)
159
		{
160
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
161
			$this->enabled_sites = is_array($sites) ? $sites : [];
162
		}
163
164
		return $this->enabled_sites;
165
	}
166
167
	/**
168
	 * Save site managed data to the database
169
	 */
170
	protected function save_manage()
171
	{
172
		$this->check_form_key();
173
174
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
175
176
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
177
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
178
179
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
180
181
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
182
	}
183
184
	/**
185
	 * Save settings data to the database
186
	 */
187
	protected function save_settings()
188
	{
189
		$this->check_form_key();
190
191
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
192
193
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
194
195
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
196
	}
197
198
	/**
199
	 * Check the form key, trigger error if invalid
200
	 */
201
	protected function check_form_key()
202
	{
203
		if (!check_form_key($this->form_key))
204
		{
205
			trigger_error('FORM_INVALID');
206
		}
207
	}
208
}
209