listener   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 13
Bugs 2 Features 0
Metric Value
wmc 12
eloc 55
c 13
b 2
f 0
dl 0
loc 151
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate_googleanalytics_id() 0 26 6
A add_googleanalytics_configs() 0 45 3
A getSubscribedEvents() 0 6 1
A load_google_analytics() 0 7 1
1
<?php
2
/**
3
 *
4
 * Google Analytics 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\googleanalytics\event;
12
13
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\template\template;
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventD...ventSubscriberInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Event listener
21
 */
22
class listener implements EventSubscriberInterface
23
{
24
	/** @var config */
25
	protected $config;
26
27
	/** @var language */
28
	protected $language;
29
30
	/** @var template */
31
	protected $template;
32
33
	/** @var user */
34
	protected $user;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param config   $config   Config object
40
	 * @param language $language Language object
41
	 * @param template $template Template object
42
	 * @param user     $user     User object
43
	 * @access public
44
	 */
45
	public function __construct(config $config, language $language, template $template, user $user)
46
	{
47
		$this->config = $config;
48
		$this->language = $language;
49
		$this->template = $template;
50
		$this->user = $user;
51
	}
52
53
	/**
54
	 * Assign functions defined in this class to event listeners in the core
55
	 *
56
	 * @return array
57
	 * @static
58
	 * @access public
59
	 */
60
	public static function getSubscribedEvents()
61
	{
62
		return [
63
			'core.acp_board_config_edit_add'	=> 'add_googleanalytics_configs',
64
			'core.page_header'					=> 'load_google_analytics',
65
			'core.validate_config_variable'		=> 'validate_googleanalytics_id',
66
		];
67
	}
68
69
	/**
70
	 * Load Google Analytics js code
71
	 *
72
	 * @return void
73
	 * @access public
74
	 */
75
	public function load_google_analytics()
76
	{
77
		$this->template->assign_vars([
78
			'GOOGLEANALYTICS_ID'		=> $this->config['googleanalytics_id'],
79
			'GOOGLEANALYTICS_TAG'		=> $this->config['googleanalytics_tag'],
80
			'GOOGLEANALYTICS_USER_ID'	=> $this->user->data['user_id'],
81
			'S_ANONYMIZE_IP'			=> $this->config['ga_anonymize_ip'],
82
		]);
83
	}
84
85
	/**
86
	 * Add config vars to ACP Board Settings
87
	 *
88
	 * @param \phpbb\event\data $event The event object
0 ignored issues
show
Bug introduced by
The type phpbb\event\data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
	 * @return void
90
	 * @access public
91
	 */
92
	public function add_googleanalytics_configs($event)
93
	{
94
		// Add a config to the settings mode, after warnings_expire_days
95
		if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['warnings_expire_days']))
96
		{
97
			// Load language file
98
			$this->language->add_lang('googleanalytics_acp', 'phpbb/googleanalytics');
99
100
			// Store display_vars event in a local variable
101
			$display_vars = $event['display_vars'];
102
103
			// Define the new config vars
104
			$ga_config_vars = [
105
				'legend_googleanalytics' => 'ACP_GOOGLEANALYTICS',
106
				'googleanalytics_id' => [
107
					'lang'		=> 'ACP_GOOGLEANALYTICS_ID',
108
					'validate'	=> 'googleanalytics_id',
109
					'type'		=> 'text:40:20',
110
					'explain'	=> true,
111
				],
112
				'ga_anonymize_ip' => [
113
					'lang'		=> 'ACP_GA_ANONYMIZE_IP',
114
					'validate'	=> 'bool',
115
					'type'		=> 'radio:yes_no',
116
					'explain'	=> true,
117
				],
118
				'googleanalytics_tag' => [
119
					'lang'		=> 'ACP_GOOGLEANALYTICS_TAG',
120
					'validate'	=> 'int',
121
					'type'		=> 'select',
122
					'function'	=> 'build_select',
123
					'params'	=> [[
124
						0	=> 'ACP_GA_ANALYTICS_TAG',
125
						1	=> 'ACP_GA_GTAGS_TAG',
126
					], '{CONFIG_VALUE}'],
127
					'explain'	=> true,
128
				],
129
			];
130
131
			// Add the new config vars after warnings_expire_days in the display_vars config array
132
			$insert_after = ['after' => 'warnings_expire_days'];
133
			$display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
0 ignored issues
show
Bug introduced by
The function phpbb_insert_config_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

133
			$display_vars['vars'] = /** @scrutinizer ignore-call */ phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
Loading history...
134
135
			// Update the display_vars event with the new array
136
			$event['display_vars'] = $display_vars;
137
		}
138
	}
139
140
	/**
141
	 * Validate the Google Analytics ID
142
	 *
143
	 * @param \phpbb\event\data $event The event object
144
	 * @return void
145
	 * @access public
146
	 */
147
	public function validate_googleanalytics_id($event)
148
	{
149
		// Check if the validate test is for google_analytics
150
		if ($event['config_definition']['validate'] !== 'googleanalytics_id' || empty($event['cfg_array']['googleanalytics_id']))
151
		{
152
			return;
153
		}
154
155
		// Store the input and error event data
156
		$input = $event['cfg_array']['googleanalytics_id'];
157
		$error = $event['error'];
158
159
		// Add error message if the input is not a valid Google Analytics ID
160
		if (!preg_match('/^UA-\d{4,9}-\d{1,4}$|^G-[A-Z0-9]{10}$/', $input))
161
		{
162
			$error[] = $this->language->lang('ACP_GOOGLEANALYTICS_ID_INVALID', $input);
163
		}
164
165
		// Add error message if GTAG is not selected for use with a Measurement ID
166
		if ((int) $event['cfg_array']['googleanalytics_tag'] === 0 && preg_match('/^G-[A-Z0-9]{10}$/', $input))
167
		{
168
			$error[] = $this->language->lang('ACP_GOOGLEANALYTICS_TAG_INVALID', $input);
169
		}
170
171
		// Update error event data
172
		$event['error'] = $error;
173
	}
174
}
175