anonymous//sources/ElkArte/MembersList.php$0   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
wmc 1
1
<?php
2
3
/**
4
 * @package   ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
7
 *
8
 * @version 2.0 dev
9
 *
10
 */
11
12
namespace ElkArte;
13
14
use BBC\ParserWrapper;
15
use ElkArte\Cache\Cache;
16
use ElkArte\Database\QueryInterface;
17
use ElkArte\Helper\Util;
18
use ElkArte\Helper\ValuesContainer;
19
20
/**
21
 * This class is used as interface to load/get the members from the database.
22
 * And it keeps track of all the members loaded.
23
 *
24
 * It consists mostly of static methods, while the instantiated part is used
25
 * by MemberLoader to access the list of loaded users.
26
 */
27
class MembersList
28
{
29
	/** @var Member[] List of all the members already loaded */
30
	protected static $members = [];
31
32
	/** @var MemberLoader Instance of MemberLoader used to actually get the data out of the database and ready in a Member object */
33
	protected static $loader;
34
35
	/** @var MembersList Instance of this very class */
36
	protected static $instance;
37
38
	/**
39
	 * Protected construct to avoid external access
40
	 */
41
	protected function __construct()
42
	{
43
	}
44
45
	/**
46
	 * Initialize the loader and the instance of this class
47
	 *
48
	 * @param QueryInterface $db The object to query the database
49
	 * @param Cache $cache Cache object used to... well cache content of each member
50
	 * @param ParserWrapper $bbc_parser BBC parser to convert BBC to HTML
51 1
	 */
52
	public static function init($db, $cache, $bbc_parser)
53 1
	{
54
		global $modSettings, $board_info;
55
56
		if (self::$instance === null)
57
		{
58
			self::$instance = new MembersList();
59
		}
60
61
		self::$loader = new MemberLoader($db, $cache, $bbc_parser, self::$instance, [
62 1
			'titlesEnable' => !empty($modSettings['titlesEnable']),
63
			'custom_fields' => featureEnabled('cp'),
64 1
			'load_moderators' => !empty($board_info['moderators']),
65
			'display_fields' => isset($modSettings['displayFields']) ? Util::unserialize($modSettings['displayFields']) : []
66 1
		]);
67
	}
68 1
69
	/**
70
	 * Loads the data of a set of users from the database into a member object.
71 1
	 *
72 1
	 * @param int|int[]|string|string[] $users Name/s or id/s of the members to load
73 1
	 * @param bool $is_name If the data passed with $users is a name or an id (true if names)
74 1
	 * @param string $set The "amount" of data to be loaded (see constants in \ElkArte\MemberLoader for the values)
75 1
	 * @return bool|int[]
76
	 */
77 1
	public static function load($users, $is_name = false, $set = 'normal')
78
	{
79
		$result = $is_name
80
			? self::$loader->loadByName($users, $set)
81
			: self::$loader->loadById($users, $set);
82
83
		return empty($result) ? false : $result;
84
	}
85
86
	/**
87 14
	 * Loads a guest member (i.e. some standard data for guests)
88
	 */
89 14
	public static function loadGuest()
90
	{
91 14
		if (isset(self::$members[0]))
92
		{
93
			return;
94
		}
95
96
		self::$loader->loadGuest();
97
	}
98
99
	/**
100
	 * Returns the Member object of the requested (numeric) $id
101
	 *
102
	 * @param int $id Member id to retrieve and return
103
	 * @return ValuesContainer
104
	 */
105
	public static function get($id)
106
	{
107
		$id = (int) $id;
108
		$member = self::$instance->getById($id);
109
110
		return $member !== false ? $member : new class() extends ValuesContainer {
111
			public function loadContext($display_custom_fields = false)
0 ignored issues
show
Unused Code introduced by
The parameter $display_custom_fields 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

111
			public function loadContext(/** @scrutinizer ignore-unused */ $display_custom_fields = false)

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...
112
			{
113 20
			}
114
		};
115 20
	}
116
117
	/**
118
	 * Returns the \ElkArte\Member object of the requested (numeric) $id
119 2
	 *
120
	 * @param int $id id of the member
121 2
	 * @return false|Member
122
	 */
123
	public function getById($id)
124
	{
125
		return self::$members[$id] ?? false;
126
	}
127
128
	/**
129
	 * Adds a \ElkArte\Member object to the list.
130
	 *
131 20
	 * @param Member $member_data The object representing
132
	 * @param int $id id of the member
133 20
	 */
134
	public static function add($member_data, $id)
135 18
	{
136
		self::$members[$id] = $member_data;
137
	}
138
139 2
	/**
140
	 * Unloads a \ElkArte\Member object from the list to allow to free some memory.
141
	 *
142
	 * @param int $id id of the member
143
	 */
144
	public static function unset($id)
145
	{
146
		if (isset(self::$members[$id]))
147
		{
148
			unset(self::$members[$id]);
149 14
		}
150
	}
151 14
152 14
	/**
153
	 * Adds data to a certain member's object.
154
	 *
155
	 * @param array $data The data to append
156
	 * @param string $type The type of data to append (by default only 'options' for custom fields
157
	 * @param int $id The id of the member
158
	 * @param array $display_fields - Basically the content of $modSettings['displayFields']
159
	 */
160
	public function appendTo($data, $type, $id, $display_fields = [])
161
	{
162
		self::$members[$id]->append($type, $data, $display_fields);
163
	}
164
}
165