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

main_module::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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
74
		if ($this->request->is_set_post('submit'))
75
		{
76
			$this->{'save_' . strtolower($mode)}();
77
		}
78
79
		switch ($mode)
80
		{
81
			case 'manage':
82
				$this->display($mode, ['MEDIA_SITES' => $this->get_sites()]);
83
			break;
84
85
			case 'settings':
86
				$this->display($mode, ['S_MEDIA_EMBED_BBCODE' => $this->config['media_embed_bbcode']]);
87
			break;
88
		}
89
	}
90
91
	/**
92
	 * Display data in the ACP module
93
	 *
94
	 * @param string $mode The ACP module mode (manage|settings)
95
	 * @param array  $data Array of data to assign to the template
96
	 */
97
	protected function display($mode, $data)
98
	{
99
		$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...
100
		$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...
101
102
		$this->template->assign_vars(array_merge($data, [
103
			'U_ACTION'	=> $this->u_action,
104
		]));
105
	}
106
107
	/**
108
	 * Get a list of available sites
109
	 *
110
	 * @return array An array of available sites
111
	 */
112
	protected function get_sites()
113
	{
114
		$sites = [];
115
116
		$checked_sites = $this->config_text->get('media_embed_sites');
117
		$checked_sites = $checked_sites ? json_decode($checked_sites, true) : [];
118
119
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
120
		foreach ($configurator->MediaEmbed->defaultSites->getIds() as $siteId)
121
		{
122
			if (isset($configurator->BBCodes[$siteId]))
123
			{
124
				continue;
125
			}
126
127
			$sites[] = [
128
				'name'		=> $siteId,
129
				'checked'	=> in_array($siteId, $checked_sites),
130
			];
131
		}
132
133
		return $sites;
134
	}
135
136
	/**
137
	 * Save site managed data to the database
138
	 */
139
	protected function save_manage()
140
	{
141
		$this->check_form_key();
142
143
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
144
145
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
146
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
147
148
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
149
	}
150
151
	/**
152
	 * Save settings data to the database
153
	 */
154
	protected function save_settings()
155
	{
156
		$this->check_form_key();
157
158
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
159
160
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
161
	}
162
163
	/**
164
	 * Check the form key, trigger error if invalid
165
	 */
166
	protected function check_form_key()
167
	{
168
		if (!check_form_key($this->form_key))
169
		{
170
			trigger_error('FORM_INVALID');
171
		}
172
	}
173
}
174