Completed
Push — master ( ec05fe...e31ce2 )
by Jeroen
35:33 queued 11s
created

CreateAdminWidgetsHandler::__invoke()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 25
rs 9.4888
1
<?php
2
3
namespace Elgg\Widgets;
4
5
/**
6
 * Creates admin widgets
7
 *
8
 * @since 4.0
9
 */
10
class CreateAdminWidgetsHandler {
11
	
12
	/**
13
	 * Adds default admin widgets to the admin dashboard.
14
	 *
15
	 * @param \Elgg\Event $event 'make_admin', 'user'
16
	 *
17
	 * @return void
18
	 */
19
	public function __invoke(\Elgg\Event $event) {
20
		$user = $event->getObject();
21
	
22
		elgg_call(ELGG_IGNORE_ACCESS, function() use ($user) {
23
			// check if the user already has widgets
24
			if (elgg_get_widgets($user->guid, 'admin')) {
25
				return;
26
			}
27
		
28
			// In the form column => array of handlers in order, top to bottom
29
			$adminWidgets = [
30
				1 => ['control_panel', 'admin_welcome'],
31
				2 => ['online_users', 'new_users', 'content_stats'],
32
			];
33
		
34
			foreach ($adminWidgets as $column => $handlers) {
35
				foreach ($handlers as $position => $handler) {
36
					$guid = elgg_create_widget($user->guid, $handler, 'admin');
37
					if ($guid === false) {
38
						continue;
39
					}
40
					
41
					/* @var \ElggWidget $widget */
42
					$widget = get_entity($guid);
43
					$widget->move($column, $position);
44
				}
45
			}
46
		});
47
	}
48
}
49