operator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
eloc 44
c 10
b 1
f 1
dl 0
loc 174
ccs 46
cts 46
cp 1
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A set_user_categories() 0 22 3
A toggle_array_value() 0 15 2
A __construct() 0 7 1
A set_collapsed_categories() 0 8 2
A set_cookie_categories() 0 10 1
A is_collapsed() 0 8 2
A get_user_categories() 0 12 2
A get_cookie_categories() 0 7 1
A get_collapsible_link() 0 5 1
1
<?php
2
/**
3
 *
4
 * Collapsible Categories extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\collapsiblecategories\operator;
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\controller\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper 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\db\driver\driver_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface 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\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request 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 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...
18
19
/**
20
 * Class operator
21
 */
22
class operator implements operator_interface
23
{
24
	/** @var array An array of collapsed category forum identifiers */
25
	protected $collapsed_categories;
26
27
	/** @var config */
28
	protected $config;
29
30
	/** @var driver_interface */
31
	protected $db;
32
33
	/** @var helper */
34
	protected $helper;
35
36
	/** @var request */
37
	protected $request;
38
39
	/** @var user */
40
	protected $user;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param config           $config  Config object
46
	 * @param driver_interface $db      Database object
47
	 * @param helper           $helper  Controller helper object
48
	 * @param request          $request Request object
49
	 * @param user             $user    User object
50
	 */
51 32
	public function __construct(config $config, driver_interface $db, helper $helper, request $request, user $user)
52
	{
53 32
		$this->config = $config;
54 32
		$this->db = $db;
55 32
		$this->helper = $helper;
56 32
		$this->request = $request;
57 32
		$this->user = $user;
58 32
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 */
63 6
	public function is_collapsed($forum_id)
64
	{
65 6
		if (!isset($this->collapsed_categories))
66
		{
67 6
			$this->collapsed_categories = $this->get_user_categories();
68
		}
69
70 6
		return in_array($forum_id, $this->collapsed_categories);
71
	}
72
73
	/**
74
	 * {@inheritdoc}
75
	 */
76 6
	public function get_collapsible_link($forum_id)
77
	{
78 6
		return $this->helper->route('phpbb_collapsiblecategories_main_controller', array(
79 6
			'forum_id'	=> $forum_id,
80 6
			'hash'		=> generate_link_hash("collapsible_$forum_id")
0 ignored issues
show
Bug introduced by
The function generate_link_hash 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

80
			'hash'		=> /** @scrutinizer ignore-call */ generate_link_hash("collapsible_$forum_id")
Loading history...
81
		));
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87 26
	public function get_user_categories()
88
	{
89
		// Get categories from the user object
90 26
		$collapsible_categories = (array) json_decode($this->user->data['collapsible_categories'], true);
91
92 26
		if (empty($collapsible_categories))
93
		{
94
			// The user object had no categories, check for a cookie
95 5
			$collapsible_categories = $this->get_cookie_categories();
96
		}
97
98 26
		return $collapsible_categories;
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104 9
	public function set_user_categories($forum_id)
105
	{
106
		// Set the collapsed category data array
107 9
		$this->set_collapsed_categories($forum_id);
108
109
		// Update the db with json encoded array of collapsed category data
110 9
		if ($this->user->data['is_registered'])
111
		{
112 9
			$sql = 'UPDATE ' . USERS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant phpbb\collapsiblecategories\operator\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
113 9
				SET collapsible_categories = '" . $this->db->sql_escape(json_encode($this->collapsed_categories)) . "'
114 9
				WHERE user_id = " . (int) $this->user->data['user_id'];
115 9
			$this->db->sql_query($sql);
116
117
			// There was an error updating the user's data
118 9
			if (!$this->db->sql_affectedrows())
119
			{
120 1
				return false;
121
			}
122
		}
123
124
		// Set a cookie with the collapsed category data and return true
125 8
		return $this->set_cookie_categories($forum_id);
126
	}
127
128
	/**
129
	 * {@inheritdoc}
130
	 */
131 8
	public function get_cookie_categories()
132
	{
133
		// Get categories from the cookie and htmlspecialchars decode it
134 8
		$cookie_data = htmlspecialchars_decode($this->request->variable($this->config['cookie_name'] . '_ccat', '', true, \phpbb\request\request_interface::COOKIE), ENT_COMPAT);
0 ignored issues
show
Bug introduced by
The type phpbb\request\request_interface 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...
135
136
		// json decode the cookie data and return an array
137 8
		return (array) json_decode($cookie_data, true);
138
	}
139
140
	/**
141
	 * {@inheritdoc}
142
	 */
143 16
	public function set_cookie_categories($forum_id)
144
	{
145
		// Set the collapsed category data array
146 16
		$this->set_collapsed_categories($forum_id);
147
148
		// Update the cookie with json encoded array of collapsed category data
149 16
		$this->user->set_cookie('ccat', json_encode($this->collapsed_categories), strtotime('+1 year'));
150
151
		// As we are unable to check immediately if the cookie was set, return true anyway
152 16
		return true;
153
	}
154
155
	/**
156
	 * Set the collapsed_categories property
157
	 *
158
	 * @param string $forum_id A forum identifier
159
	 *
160
	 * @return operator_interface $this object
161
	 */
162 17
	protected function set_collapsed_categories($forum_id)
163
	{
164 17
		if (!isset($this->collapsed_categories))
165
		{
166 17
			$this->collapsed_categories = $this->toggle_array_value($forum_id, $this->get_user_categories());
167
		}
168
169 17
		return $this;
170
	}
171
172
	/**
173
	 * Add a value to an array if it is new or
174
	 * remove the value if it exists in the array.
175
	 *
176
	 * @param mixed $value A string or int value
177
	 * @param array $array An array
178
	 *
179
	 * @return array The updated array
180
	 */
181 17
	protected function toggle_array_value($value, $array)
182
	{
183 17
		if (in_array($value, $array))
184
		{
185
			// Remove all matching values from the array using array_diff
186 8
			$array = array_diff($array, array($value));
187
		}
188
		else
189
		{
190
			// Add the new value to the array
191 9
			$array[] = $value;
192
		}
193
194
		// Enforce unique array values
195 17
		return array_keys(array_count_values($array));
196
	}
197
}
198