Completed
Pull Request — master (#4)
by Matt
02:07
created

main_module::main()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 15
nc 5
nop 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 string $form_key */
40
	protected $form_key;
41
42
	/** @var string $u_action */
43
	public $u_action;
44
45
	/**
46
	 * Constructor
47
	 */
48
	public function __construct()
49
	{
50
		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...
51
52
		$this->container   = $phpbb_container;
53
		$this->cache       = $this->container->get('cache');
54
		$this->config      = $this->container->get('config');
55
		$this->config_text = $this->container->get('config_text');
56
		$this->request     = $this->container->get('request');
57
		$this->template    = $this->container->get('template');
58
		$this->language    = $this->container->get('language');
59
		$this->form_key    = 'phpbb/mediaembed';
60
61
		$this->language->add_lang('acp', 'phpbb/mediaembed');
62
	}
63
64
	/**
65
	 * Main ACP module
66
	 *
67
	 * @param int    $id   The module ID
68
	 * @param string $mode The module mode
69
	 */
70
	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...
71
	{
72
		add_form_key($this->form_key);
73
		switch ($mode)
74
		{
75
			case 'manage':
76
				if ($this->request->is_set_post('submit'))
77
				{
78
					$this->save_manage();
79
				}
80
				$this->display($mode, [
81
					'MEDIA_SITES' => $this->get_sites(),
82
				]);
83
			break;
84
85
			case 'settings':
86
				if ($this->request->is_set_post('submit'))
87
				{
88
					$this->save_settings();
89
				}
90
				$this->display($mode, [
91
					'S_MEDIA_EMBED_BBCODE' => $this->config['media_embed_bbcode'],
92
				]);
93
			break;
94
		}
95
	}
96
97
	/**
98
	 * Display data in the ACP module
99
	 *
100
	 * @param string $mode The ACP module mode (manage|settings)
101
	 * @param array  $data Array of data to assign to the template
102
	 */
103
	protected function display($mode, $data)
104
	{
105
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . strtolower($mode);
0 ignored issues
show
Bug introduced by
The property tpl_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
106
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
0 ignored issues
show
Bug introduced by
The property page_title does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
108
		$this->template->assign_vars(array_merge($data, [
109
			'U_ACTION'	=> $this->u_action,
110
		]));
111
	}
112
113
	/**
114
	 * Get a list of available sites
115
	 *
116
	 * @return array An array of available sites
117
	 */
118
	protected function get_sites()
119
	{
120
		$sites = [];
121
122
		$checked_sites = $this->config_text->get('media_embed_sites');
123
		$checked_sites = $checked_sites ? json_decode($checked_sites, true) : [];
124
125
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
126
		foreach ($configurator->MediaEmbed->defaultSites->getIds() as $siteId)
127
		{
128
			if (isset($configurator->BBCodes[$siteId]))
129
			{
130
				continue;
131
			}
132
133
			$sites[] = [
134
				'name'		=> $siteId,
135
				'checked'	=> in_array($siteId, $checked_sites),
136
			];
137
		}
138
139
		return $sites;
140
	}
141
142
	/**
143
	 * Save site managed data to the database
144
	 */
145
	protected function save_manage()
146
	{
147
		$this->check_form_key();
148
149
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
150
151
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
152
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
153
154
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
155
	}
156
157
	/**
158
	 * Save settings data to the database
159
	 */
160
	protected function save_settings()
161
	{
162
		$this->check_form_key();
163
164
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
165
166
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
167
	}
168
169
	/**
170
	 * Check the form key, trigger error if invalid
171
	 */
172
	protected function check_form_key()
173
	{
174
		if (!check_form_key($this->form_key))
175
		{
176
			trigger_error('FORM_INVALID');
177
		}
178
	}
179
}
180