viglink_module::main()   F
last analyzed

Complexity

Conditions 15
Paths 289

Size

Total Lines 106
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 47
c 7
b 0
f 0
dl 0
loc 106
rs 3.9708
cc 15
nc 289
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * VigLink extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\viglink\acp;
12
13
use phpbb\request\type_cast_helper;
14
15
/**
16
 * VigLink ACP module
17
 */
18
class viglink_module
19
{
20
	/** @var string $page_title The page title */
21
	public $page_title;
22
23
	/** @var string $tpl_name The page template name */
24
	public $tpl_name;
25
26
	/** @var string $u_action Custom form action */
27
	public $u_action;
28
29
	/**
30
	 * Main ACP module
31
	 *
32
	 * @throws \Exception
33
	 */
34
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
	public function main(/** @scrutinizer ignore-unused */ $id, $mode)

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

Loading history...
35
	{
36
		global $phpbb_container;
37
38
		/** @var \phpbb\config\config $config Config object */
39
		$config = $phpbb_container->get('config');
40
41
		/** @var \phpbb\language\language $language Language object */
42
		$language = $phpbb_container->get('language');
43
44
		/** @var \phpbb\request\request $request Request object */
45
		$request  = $phpbb_container->get('request');
46
47
		/** @var \phpbb\template\template $template Template object */
48
		$template = $phpbb_container->get('template');
49
50
		$language->add_lang('viglink_module_acp', 'phpbb/viglink');
51
52
		$this->tpl_name = 'acp_viglink';
53
		$this->page_title = $language->lang('ACP_VIGLINK_SETTINGS');
54
55
		$submit = $request->is_set_post('submit');
56
57
		if ($mode !== 'settings')
58
		{
59
			return;
60
		}
61
62
		$form_key = 'acp_viglink';
63
		add_form_key($form_key);
64
65
		$error = array();
66
67
		// Get stored config/default values
68
		$cfg_array = array(
69
			'viglink_enabled' => isset($config['viglink_enabled']) ? $config['viglink_enabled'] : 0,
70
		);
71
72
		// Error if the form is invalid
73
		if ($submit && !check_form_key($form_key))
74
		{
75
			$error[] = $language->lang('FORM_INVALID');
76
		}
77
78
		// Do not process form if invalid
79
		if (count($error))
80
		{
81
			$submit = false;
82
		}
83
84
		if ($submit)
85
		{
86
			// Get the VigLink form field values
87
			$cfg_array['viglink_enabled'] = $request->variable('viglink_enabled', 0);
88
89
			// If no errors, set the config values
90
			if (!count($error))
91
			{
92
				foreach ($cfg_array as $cfg => $value)
93
				{
94
					$config->set($cfg, $value);
95
				}
96
97
				trigger_error($language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
98
			}
99
		}
100
101
		if (!isset($config['questionnaire_unique_id']))
102
		{
103
			$config->set('questionnaire_unique_id', unique_id());
104
		}
105
106
		// Set a general error message if VigLink has been disabled by phpBB
107
		if (!$config['allow_viglink_phpbb'])
108
		{
109
			$error[] = $language->lang('ACP_VIGLINK_DISABLED_PHPBB');
110
		}
111
112
		// Try to get convert account key from .com
113
		$sub_id = md5($config['viglink_api_siteid'] . $config['questionnaire_unique_id']);
114
		$convert_account_link = $config->offsetGet('viglink_convert_account_url');
115
116
		if (empty($convert_account_link) || strpos($config['viglink_convert_account_url'], 'subId=' . $sub_id) === false)
117
		{
118
			$convert_account_link = @file_get_contents('https://www.phpbb.com/viglink/convert?domain=' . urlencode($config['server_name']) . '&siteid=' . $config['viglink_api_siteid'] . '&uuid=' . $config['questionnaire_unique_id'] . '&key=' . $config['phpbb_viglink_api_key']);
119
			if (!empty($convert_account_link) && strpos($convert_account_link, 'https://www.viglink.com/users/convertAccount') === 0)
120
			{
121
				$type_caster = new type_cast_helper();
122
				$type_caster->set_var($convert_account_link, $convert_account_link, 'string', false, false);
123
				$config->set('viglink_convert_account_url', $convert_account_link);
124
			}
125
			else
126
			{
127
				$error[] = $language->lang('ACP_VIGLINK_NO_CONVERT_LINK');
128
				$convert_account_link = '';
129
			}
130
		}
131
132
		$template->assign_vars(array(
133
			'S_ERROR'				=> (bool) count($error),
134
			'ERROR_MSG'				=> implode('<br />', $error),
135
136
			'VIGLINK_ENABLED'		=> $cfg_array['viglink_enabled'],
137
138
			'U_VIGLINK_CONVERT'		=> $convert_account_link,
139
			'U_ACTION'				=> $this->u_action,
140
		));
141
	}
142
}
143